Skip to content

Commit b9a8cdd

Browse files
committed
Session is now a string. Upgraded mongodb package
1 parent 3a5d513 commit b9a8cdd

File tree

4 files changed

+25
-33
lines changed

4 files changed

+25
-33
lines changed

Readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ connect-mongodb is a mongoDB session store backed by [node-mongodb-native](http:
44

55
Originally written by [dvv](http://github.com/dvv)
66

7-
## Version 1.0
7+
## Version 1.*
88

99
This version is not compatible with `0.*` versions. Now you must pass a mongodb connection, or server configuration.
1010
On updating, i recomment to delete your current sessions collection data.

lib/connect-mongodb.js

+15-23
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
var Store = require('connect').session.Store
99
, mongo = require('mongodb')
1010
, _collection = null
11-
, _default = function (callback) {
12-
callback = typeof(callback) === 'function' ? callback : function () { };
13-
return callback;
14-
}
1511
, _defaults = {host: '127.0.0.1', port: 27017, dbname: 'dev', collection: 'sessions', reapInterval: 60 * 1000};
1612

13+
function _default(callback) {
14+
callback = typeof(callback) === 'function' ? callback : function () { };
15+
return callback;
16+
}
17+
1718
/**
1819
* Initialize MongoStore with the given `options`.
1920
*
@@ -81,7 +82,7 @@ var MONGOSTORE = module.exports = function MongoStore(options, callback) {
8182
}, options.reapInterval || _defaults.reapInterval, this); // _defaults to each minute
8283
}
8384

84-
if (server_config.isConnected()) {
85+
if (server_config.connected) {
8586
authenticateAndGetCollection(callback);
8687
} else {
8788
server_config.connect(db, function (err) {
@@ -109,9 +110,8 @@ MONGOSTORE.prototype.get = function (sid, cb) {
109110
_collection.findOne({_id: sid}, function (err, data) {
110111
try {
111112
if (data) {
112-
var sess =
113-
typeof data.session === 'string' ? JSON.parse(data.session)
114-
: data.session;
113+
var sess = typeof data.session === 'string' ? JSON.parse(data.session)
114+
: data.session;
115115
cb(null, sess);
116116
} else {
117117
cb();
@@ -126,29 +126,21 @@ MONGOSTORE.prototype.get = function (sid, cb) {
126126
/**
127127
* Commit the given `sess` object associated with the given `sid`.
128128
*
129-
* With connect-session, `sess.cookie` has a getter `data`, and if we
130-
* store sess.cookie.data in the DB, problems occur. Run sess through
131-
* JSON.stringify(), which strips out the getter.
132-
*
133129
* @param {String} sid
134130
* @param {Session} sess
135131
* @param {Function} cb
136132
* @api public
137133
*/
138134
MONGOSTORE.prototype.set = function (sid, sess, cb) {
139135
cb = _default(cb);
140-
try {
141-
var update = {_id: sid, session: JSON.parse(JSON.stringify(sess))};
142-
if (sess && sess.cookie && sess.cookie.expires) {
143-
update.expires = Date.parse(sess.cookie.expires);
144-
}
145-
146-
_collection.update({_id: sid}, update, {upsert: true}, function (err, data) {
147-
cb.apply(this, arguments);
148-
});
149-
} catch (exc) {
150-
cb(exc);
136+
var update = {_id: sid, session: JSON.stringify(sess)};
137+
if (sess && sess.cookie && sess.cookie.expires) {
138+
update.expires = Date.parse(sess.cookie.expires);
151139
}
140+
141+
_collection.update({_id: sid}, update, {upsert: true}, function (err, data) {
142+
cb.apply(this, arguments);
143+
});
152144
};
153145

154146
/**

package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
{
22
"name": "connect-mongodb",
33
"description": "mongodb session store for connect",
4-
"version": "1.0.0",
4+
"version": "1.1.0",
55
"author": "Vladimir Dronnikov <[email protected]>",
66
"contributors": ["Pau Ramon <[email protected]>", "ramv"],
77
"repository": {"type": "git", "url": "git://github.com/masylum/connect-mongodb.git"},
8-
"dependencies": {"mongodb": "0.9.6-7", "connect": "1.5.1"},
8+
"dependencies": {"mongodb": "0.9.7", "connect": "1.5.1"},
99
"devDependencies": {"testosterone": "1.2.0", "gently": "0.9.1", "funk": "1.5.0"},
10-
"main": "./lib/connect-mongodb",
11-
"engines": {"node": ">= 0.4.0"}
10+
"main": "./lib/connect-mongodb"
1211
}

tests/core.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var testosterone = require('testosterone')({title: 'models/advertiser'})
55
, Server = require('mongodb').Server
66
, server_config = new Server('localhost', 27017, {auto_reconnect: true, native_parser: true})
77
, url = 'mongodb://localhost:27017/test'
8+
, connect_mongodb = require('..')
89
, db = new Db('test', server_config, {});
910

1011
testosterone
@@ -13,11 +14,11 @@ testosterone
1314
var funk = require('funk')('parallel');
1415

1516
db.open(function () {
16-
require('..')(null, funk.add(assert.ok));
17-
require('..')({db: null}, funk.add(assert.ok));
18-
require('..')({db: db, setInterval: -1}, funk.add(assert.ifError));
19-
require('..')({server_config: server_config, setInterval: -1}, funk.add(assert.ifError));
20-
require('..')({url: url, setInterval: -1}, funk.add(assert.ifError));
17+
connect_mongodb(null, funk.add(assert.ok));
18+
connect_mongodb({db: null}, funk.add(assert.ok));
19+
connect_mongodb({db: db, setInterval: -1}, funk.add(assert.ifError));
20+
connect_mongodb({server_config: server_config, setInterval: -1}, funk.add(assert.ifError));
21+
connect_mongodb({url: url, setInterval: -1}, funk.add(assert.ifError));
2122
funk.run(done);
2223
});
2324
})

0 commit comments

Comments
 (0)