From ecf5f0730f7c8eb8329207b4d83122d0ee1678d8 Mon Sep 17 00:00:00 2001 From: Ankit Patial Date: Wed, 1 Jan 2014 13:30:57 +0530 Subject: [PATCH 1/2] Allow to accept Db instance, typo fix, better jshint fixes --- lib/express-session-mongo.js | 120 +++++++++++++++++++++-------------- 1 file changed, 72 insertions(+), 48 deletions(-) diff --git a/lib/express-session-mongo.js b/lib/express-session-mongo.js index 87c233f..8624651 100644 --- a/lib/express-session-mongo.js +++ b/lib/express-session-mongo.js @@ -7,51 +7,60 @@ var mongo = require('mongodb'), util = require(process.binding('natives').util ? 'util' : 'sys'), Session = require('express').session, Db = mongo.Db, - Connection = mongo.Connection, - Server = mongo.Server, - BSON = mongo.BSONNative; + Server = mongo.Server; + +var MongoStore = function (options) { + 'use strict'; -var MongoStore = function(options) { options = options || {}; Session.Store.call(this, options); - - var server, - dbName = (options.db) ? options.db : 'express-sessions', - ip = (options.ip) ? options.ip : '127.0.0.1', - port = (options.port) ? options.port : 27017, + + var self = this, + server, + db = options.db || 'express-sessions',// dbName = options.db || 'express-sessions', + ip = options.ip || '127.0.0.1', + port = options.port || 27017, fsync = (typeof options.fsync !== 'undefined') ? options.fsync : false, nativeParser = (typeof options.native_parser !== 'undefined') ? options.native_parser : true; - this._collection = (options.collection) ? options.collection : 'sessions'; + self._collection = options.collection || 'sessions'; - if (options.server) { - server = options.server; - } else { - server= new Server(ip, port, {auto_reconnect: true}, {}); - } + if (db && typeof db === 'string') { //if db is passed in as dbName + if (options.server) { + server = options.server; + } else { + server = new Server(ip, port, {auto_reconnect: true}, {}); + } + // treat db as dbName + self._db = new Db(db, server, {fsync: fsync, native_parser: nativeParser}); + openMongoConn(self._db); + } else if (db && db instanceof Db) { // + self._db = db; - this._db = new Db(dbName, server, {fsync: fsync, native_parser: nativeParser}); - this._db.open(function(db) {}); + if (self._db.state !== 'connected') { + openMongoConn(self._db); + } + } }; util.inherits(MongoStore, Session.Store); -MongoStore.prototype.set = function(sid, sess, fn) { - this._db.collection(this._collection, function(err, collection) { - collection.findOne({ _sessionid: sid }, function(err, session_data) { - if (err) { - fn && fn(error); +MongoStore.prototype.set = function (sid, sess, fn) { + 'use strict'; + this._db.collection(this._collection, function (err, collection) { + collection.findOne({ _sessionid: sid }, function (err, session_data) { + if (err && fn) { + fn(err); } else { sess._sessionid = sid; var method = 'insert'; if (session_data) { - sess.lastAccess = new Date() + sess.lastAccess = new Date(); method = 'save'; } - collection[method](sess, function(err, document) { - if (err) { - } else { - fn && fn(null, sess); + collection[method](sess, function (err, document) { + if (!err && fn) { + fn(null, sess); } }); } @@ -59,12 +68,13 @@ MongoStore.prototype.set = function(sid, sess, fn) { }); }; -MongoStore.prototype.get = function(sid, fn) { - this._db.collection(this._collection, function(err, collection) { - collection.findOne({ _sessionid: sid }, function(err, session_data) { - if (err) { - fn && fn(error); - } else { +MongoStore.prototype.get = function (sid, fn) { + 'use strict'; + this._db.collection(this._collection, function (err, collection) { + collection.findOne({ _sessionid: sid }, function (err, session_data) { + if (err && fn) { + fn(err); + } else { if (session_data) { session_data = cleanSessionData(session_data); } @@ -74,27 +84,30 @@ MongoStore.prototype.get = function(sid, fn) { }); }; -MongoStore.prototype.destroy = function(sid, fn) { - this._db.collection(this._collection, function(err, collection) { - collection.remove({ _sessionid: sid }, function() { +MongoStore.prototype.destroy = function (sid, fn) { + 'use strict'; + this._db.collection(this._collection, function (err, collection) { + collection.remove({ _sessionid: sid }, function () { fn && fn(); }); }); }; -MongoStore.prototype.length = function(fn) { - this._db.collection(this._collection, function(err, collection) { - collection.count(function(count) { +MongoStore.prototype.length = function (fn) { + 'use strict'; + this._db.collection(this._collection, function (err, collection) { + collection.count(function (count) { fn && fn(null, count); }); }); }; -MongoStore.prototype.all = function() { +MongoStore.prototype.all = function () { + 'use strict'; var arr = []; - this._db.collection(this._collection, function(err, collection) { - collection.find(function(err, cursor) { - cursor.each(function(d) { + this._db.collection(this._collection, function (err, collection) { + collection.find(function (err, cursor) { + cursor.each(function (d) { d = cleanSessionData(d); arr.push(d); }); @@ -103,7 +116,8 @@ MongoStore.prototype.all = function() { }); }; -MongoStore.prototype.clear = function() { +MongoStore.prototype.clear = function () { + 'use strict'; this._db.collection(this._collection, function(err, collection) { collection.remove(function() { fn && fn(); @@ -111,18 +125,28 @@ MongoStore.prototype.clear = function() { }); }; -var cleanSessionData = function(json) { - var data = {}; - for (var i in json) { +var cleanSessionData = function (json) { + 'use strict'; + var data = {}, + i = 0; + for (i in json) { data[i] = json[i]; if (data[i] instanceof Object) { if ('low_' in data[i] || 'high_' in data[i]) { data[i] = data[i].toNumber(); } } - } return data; }; +function openMongoConn(db) { + 'use strict'; + db.open(function (err) { + if (err) { + throw err; + } + }); +} + module.exports = MongoStore; From a55555551a8a48c35b2f4a8f70c7507a01ac6754 Mon Sep 17 00:00:00 2001 From: Ankit Patial Date: Wed, 1 Jan 2014 13:35:38 +0530 Subject: [PATCH 2/2] using existing mongo.Db instance --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 152d2e4..2294b7c 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ The standard usage, is to just pass an instantiated `MongoStore` instance to the You can also pass several options to the constructor to tweak your session store: -* db - The name of the db to use, defaults to: `express-sessions` +* db - The name of the db to use, defaults to: `express-sessions` Or you can assign an existing mongo.Db instance * ip - The IP address of the server to connect to, defaults to: `127.0.0.1` * port - The Port to connect to, defaults to: `27017` * collection - The collection to save it's data to, defaults to: `sessions`