Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consume existing mongo.Db, typo fix and some jsHint fixes #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

README change to highlight the functionality to consume 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`
Expand Down
120 changes: 72 additions & 48 deletions lib/express-session-mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,74 @@ 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);
}
});
}
});
});
};

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);
}
Expand All @@ -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);
});
Expand All @@ -103,26 +116,37 @@ 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();
});
});
};

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;