摘要:的簡(jiǎn)單封裝近期項(xiàng)目中需要用到來(lái)操作數(shù)據(jù)庫(kù),在上面找了給開(kāi)源的發(fā)現(xiàn)支持不了的認(rèn)證,后來(lái)發(fā)現(xiàn)對(duì)其改進(jìn)了一下,能夠支持認(rèn)證,因?yàn)闃I(yè)務(wù)中經(jīng)常用到來(lái)進(jìn)行操作,所以參照的方式對(duì)其進(jìn)行了一下簡(jiǎn)單的封裝,方便以后使用。
resty-mongol3的簡(jiǎn)單封裝
近期項(xiàng)目中需要用到 openresty 來(lái)操作 mongodb 數(shù)據(jù)庫(kù),在 github 上面找了給開(kāi)源的 resty-mongol 發(fā)現(xiàn)支持不了 mongo3.0 的認(rèn)證,后來(lái)發(fā)現(xiàn) resty-mongol3 對(duì)其改進(jìn)了一下,能夠支持 mongo3.0 認(rèn)證,因?yàn)闃I(yè)務(wù)中經(jīng)常用到 mongo 來(lái)進(jìn)行操作,所以參照 mongo shell 的方式對(duì)其進(jìn)行了一下簡(jiǎn)單的封裝,方便以后使用。(因?yàn)闃I(yè)務(wù)需要,有的方法不要被支持,固沒(méi)有封裝)如有 bug ,望大家批評(píng)指正。
local mongo = require("resty.mongol") local object_id = require("resty.mongol.object_id") local _M = { _VERSION = "0.0.1" } local metatable = { __index = _M } -- 創(chuàng)建objectId _M.objectId = function(str) local buf = (str:gsub("..", function (cc) return string.char(tonumber(cc, 16)) end)) return object_id.new(buf) end --[[ @desc Creates a MongoClient instance. @params opts @type table @return table @type table A MongoClient instance ]] function _M.new(self, opts) opts = opts or {} local timeout = opts.timeout or 3000 local host = opts.host or "localhost" local port = opts.port or 27017 local passwd = opts.passwd or "" local user = opts.user or "" local database = opts.database or "admin" local keepalive = (opts.keepalive and opts.keepalive * 1000) or 60000 local poolSize = opts.poolSize or 1000 return setmetatable({ timeout = timeout, host = host, port = tostring(port), user = user, passwd = passwd, database = database, keepalive = keepalive, poolSize = poolSize, _db = database, _user = user, _passwd = passwd, _sort = {}, _limit = 100, _skip = 0, }, metatable) end --[[ @desc get mongodb"s connection objects. ]] local function getMgoConn(mgoConfig) -- 獲取連接對(duì)象 local mgoConn = mongo:new() if not mgoConn then return nil, "get mongo connection occur error" end -- 設(shè)置鏈接超時(shí) mgoConn:set_timeout(mgoConfig.timeout) --獲取連接客戶端 local ok, err =mgoConn:connect(mgoConfig.host, mgoConfig.port) if not ok then return nil, err end return mgoConn, nil end --[[ @desc pack connection commands. ]] local function packConnCmd(self, mgoConn, cmd, ... ) local result, err = mgoConn[cmd](mgoConn, ... ) mgoConn:set_keepalive(self.keepalive, self.poolSize) return result, err end --[[ @desc this is a map of mongol.conn"s command. ]] local connCmd = { isMaster = "ismaster", getPrimary = "getprimary", getReusedTime = "get_reused_times", dbs = "databases", } for k,v in pairs(connCmd) do _M[k] = function (self, ...) --獲取連接客戶端 local mgoConn, err = getMgoConn(self) if not mgoConn then return nil, err end return packConnCmd(self, mgoConn, v, ...) end end --[[ @desc switch db by dbName and auth your id @params @return Returns a database object, or nil. ]] function _M.useDatabase(self, dbName, user, passwd) --獲取連接客戶端 self._db = dbName self._user = user or self._user self._passwd = passwd or self._psswd return string.format("%s %s","current database is", dbName) end function _M.ping( self ) --獲取連接客戶端 local mgoConn, err = getMgoConn(self) if not mgoConn then return nil, err end local db = mgoConn:new_db_handle(self._db) if not db then return nil, "get database occur error" end --用戶授權(quán) local count, err = mgoConn:get_reused_times() if (count == 0) or err then if self._user and self._passwd then local ok, err = db:auth_scram_sha1(self._user, self._passwd) if not ok then return nil, err end end end return "ok", nil end --[[ @desc switch db by self.dbName and auth your id @params dbName @type table @default self.database user @type string @default self.user passwd @type string @default self.passwd @return Returns a database object, or nil. ]] local function getDB(self) --獲取連接客戶端 local mgoConn, err = getMgoConn(self) if not mgoConn then return nil, nil, err end local db = mgoConn:new_db_handle(self._db) if not db then return nil, nil, "get database occur error" end --用戶授權(quán) local count, err = mgoConn:get_reused_times() if (count == 0) or err then if self._user and self._passwd then local ok, err = db:auth_scram_sha1(self._user, self._passwd) if not ok then return nil, nil, err end end end return db, mgoConn, nil end local dbCmd = { addUser = "add_user", -- getColl = "get_col", -- getGrid = "get_grid", dropDatabase = "dropDatabase", } --[[ @desc pack database commands. ]] local function packDBCmd(self, db, mgoConn, cmd, ... ) local result, err = db[cmd](db, ... ) mgoConn:set_keepalive(self.keepalive, self.poolSize) return result, err end for k,v in pairs(dbCmd) do _M[k] = function (self, ...) --獲取連接客戶端 local db, mgoConn, err = getDB(self) if not db then return nil, "get current database occur error " .. err end return packDBCmd(self, db, mgoConn, v, ...) end end function _M.list( self ) --獲取連接客戶端 local db, mgoConn, err = getDB(self) if not db then return nil, "get current database occur error " .. err end -- return packDBCmd(self, db, mgoConn, v, ...) local cursor, err = db:listcollections() if not cursor then return nil, string.format("%s %s %s", "system.namespaces", "find occur error", err) end local results = {} for index, item in cursor:pairs() do table.insert(results, item) end mgoConn:set_keepalive(self.keepalive, self.poolSize) return result, err end function _M.getCollection( self, collName ) self.collName = collName return self end local collCmd = { count = "count", drop = "drop", update = "update", insert = "insert", delete = "delete", } --[[ @desc pack collection"s commands. ]] local function packCollCmd(self, db, mgoConn, cmd, ... ) --獲取集合 local coll = db:get_col(self.collName) if not coll then return nil, "get collection occur error" end local result, err = coll[cmd](coll, ... ) mgoConn:set_keepalive(self.keepalive, self.poolSize) return result, err end for k,v in pairs(collCmd) do _M[k] = function (self, ...) --獲取連接客戶端 local db, mgoConn, err = getDB(self) if not db then return nil, "get current database occur error " .. err end return packCollCmd(self, db, mgoConn, v, ...) end end function _M.sort( self, fields ) self._sort = fields return self end function _M.limit( self, num ) self._limit = num return self end function _M.skip( self, num ) self._skip = num return self end --[[ find 方法要在用戶每次操作后直接幫他關(guān)閉鏈接,所以沒(méi)有返回游標(biāo)給用戶,而是自己內(nèi)部遍歷了下,直接返回結(jié)果 ]] function _M.find( self, ... ) --獲取連接客戶端 local db, mgoConn, err = getDB(self) if not db then return nil, "get current database occur error " .. err end --獲取集合 local coll, err = db:get_col(self.collName) if not coll then return nil, "get collection occur error" end local cursor, err = coll["find"](coll, ... ) if not cursor then return nil, string.format("%s %s %s", self.collName, "find occur error", err) end cursor:limit(self._limit) cursor:skip(self._skip) cursor = cursor:sort(self._sort) local results = {} for index, item in cursor:pairs() do table.insert(results, item) end mgoConn:set_keepalive(self.keepalive, self.poolSize) return results, err end return _M
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/18988.html
摘要:函數(shù)被轉(zhuǎn)化之后得到柯里化函數(shù),能夠處理的所有剩余參數(shù)。因此柯里化也被稱為部分求值。那么函數(shù)的柯里化函數(shù)則可以如下因此下面的運(yùn)算方式是等價(jià)的。而這里對(duì)于函數(shù)參數(shù)的自由處理,正是柯里化的核心所在。額外知識(shí)補(bǔ)充無(wú)限參數(shù)的柯里化。 showImg(https://segmentfault.com/img/remote/1460000008493346); 柯里化是函數(shù)的一個(gè)比較高級(jí)的應(yīng)用,想要...
摘要:工作之余簡(jiǎn)單封裝了的請(qǐng)求,但是工作中還是用,,內(nèi)部封裝好了模塊笑。代碼解析參數(shù)創(chuàng)建高級(jí)瀏覽器和以上連接發(fā)送請(qǐng)求頭發(fā)送接收完成清除定時(shí)器成功超時(shí)超時(shí)了終止轉(zhuǎn) 工作之余簡(jiǎn)單封裝了ajax的請(qǐng)求,但是工作中還是用jquery,axios,angular內(nèi)部封裝好了http模塊(笑)。 ajax一般分為簡(jiǎn)單的四部: 創(chuàng)建ajax對(duì)象(這里兼容ie的話要做一下處理) 連接,即請(qǐng)求對(duì)象的open...
摘要:最近我準(zhǔn)備寫(xiě)一個(gè)這樣的庫(kù),基于前期自己對(duì)的封裝是我使用過(guò)的一個(gè)框架,對(duì)它的模型模塊調(diào)用的方式很喜歡因此決定參考其,用實(shí)現(xiàn)一次。 在我自己的平常開(kāi)發(fā)中很少有見(jiàn)到j(luò)avascript對(duì)sql的封裝比較好的庫(kù)(找了一圈也沒(méi)找到、應(yīng)該是暫時(shí)我沒(méi)發(fā)現(xiàn)),因此前期的項(xiàng)目中根據(jù)自己的項(xiàng)目情況實(shí)現(xiàn)了一套封裝方法。 最近我準(zhǔn)備寫(xiě)一個(gè)這樣的庫(kù),基于前期自己對(duì)mysql的封裝(ThinkPHP是我使用過(guò)的一...
摘要:總結(jié)本文簡(jiǎn)要介紹了函數(shù)的一些特性,重點(diǎn)在于說(shuō)明如何使用函數(shù)對(duì)異步任務(wù)進(jìn)行封裝,從而能夠讓異步代碼編寫(xiě)的更加清晰。 在之前的文章介紹了傳統(tǒng)異步的實(shí)現(xiàn)方案,本文將介紹ES6中的一種全新的異步方案--Generator函數(shù)。 generator簡(jiǎn)介 簡(jiǎn)單介紹一下generator的原理和語(yǔ)法,(更詳細(xì)內(nèi)容請(qǐng)看ECMAScript 6 入門,本文只介紹和異步相關(guān)的核心內(nèi)容) 基本語(yǔ)法 通過(guò)一個(gè)...
閱讀 1218·2019-08-30 15:55
閱讀 964·2019-08-30 15:55
閱讀 2164·2019-08-30 15:44
閱讀 2895·2019-08-29 14:17
閱讀 1141·2019-08-29 12:45
閱讀 3317·2019-08-26 10:48
閱讀 3145·2019-08-23 18:18
閱讀 2614·2019-08-23 16:47