Software /
code /
prosody-modules
File
mod_storage_mongodb/mod_storage_mongodb.lua @ 506:0e07810550c8
mod_storage_mongodb: Use _global as host when none provided
author | James Callahan <james@chatid.com> |
---|---|
date | Tue, 13 Dec 2011 12:30:01 +1100 |
parent | 505:b6d2ac386120 |
child | 507:46f578da4ff0 |
line wrap: on
line source
local next = next; local setmetatable = setmetatable; local log = require "util.logger".init("mongodb"); local params = module:get_option("mongodb"); local mongo = require "mongo"; local conn = mongo.Connection.New ( true ); conn:connect ( params.server ); conn:auth ( params ); local keyval_store = {}; keyval_store.__index = keyval_store; function keyval_store:get(username) local host = module.host or "_global"; local store = self.store; local namespace = params.dbname .. "." .. host; local v = { _id = { store = store ; username = username } }; local cursor , err = conn:query ( namespace , v ); if not cursor then return nil , err end; local r , err = cursor:next ( ); if not r then return nil , err end; return r.data; end function keyval_store:set(username, data) local host = module.host or "_global"; local store = self.store; local namespace = params.dbname .. "." .. host; local v = { _id = { store = store ; username = username } }; if next(data) ~= nil then -- set data v.data = data; return conn:insert ( namespace , v ); else -- delete data return conn:remove ( namespace , v ); end; end local driver = { name = "mongodb" }; function driver:open(store, typ) if not typ then -- default key-value store return setmetatable({ store = store }, keyval_store); end; return nil, "unsupported-store"; end module:add_item("data-driver", driver);