Skip to content

Commit

Permalink
Merge remote-tracking branch 'mihs/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
kenpratt committed Jan 12, 2013
2 parents 9445612 + 8733e2a commit e0186ed
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ via npm:

## Options

- `db` Database name
- `db` Database name OR fully instantiated node-mongo-native object
- `collection` Collection (optional, default: `sessions`)
- `host` MongoDB server hostname (optional, default: `127.0.0.1`)
- `port` MongoDB server port (optional, default: `27017`)
Expand Down
48 changes: 28 additions & 20 deletions lib/connect-mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,19 @@ module.exports = function(connect) {
throw new Error('Required MongoStore option `db` missing');
}

this.db = new mongo.Db(options.db,
new mongo.Server(options.host || defaultOptions.host,
options.port || defaultOptions.port,
{
auto_reconnect: options.auto_reconnect ||
defaultOptions.auto_reconnect
}));
if (typeof options.db == "object") {
this.db = options.db; // Assume it's an instantiated DB Object
}
else {
this.db = new mongo.Db(options.db,
new mongo.Server(options.host || defaultOptions.host,
options.port || defaultOptions.port,
{
auto_reconnect: options.auto_reconnect ||
defaultOptions.auto_reconnect
}));
}
}

this.db_collection_name = options.collection || defaultOptions.collection;

if (options.hasOwnProperty('stringify') ?
Expand Down Expand Up @@ -137,20 +141,24 @@ module.exports = function(connect) {
});
}
};

this.db.open(function(err, db) {
if (err) {
throw new Error('Error connecting to database');
}
if (this.db.openCalled) {
this._get_collection(callback)
}
else {
this.db.open(function(err, db) {
if (err) {
throw new Error('Error connecting to database');
}

if (options.username && options.password) {
db.authenticate(options.username, options.password, function () {
if (options.username && options.password) {
db.authenticate(options.username, options.password, function () {
self._get_collection(callback);
});
} else {
self._get_collection(callback);
});
} else {
self._get_collection(callback);
}
});
}
});
}
};

/**
Expand Down
171 changes: 171 additions & 0 deletions test/connect-mongo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ var mongoose = require('mongoose');
var testMongooseDb = mongoose.connect('mongodb://127.0.0.1:27017/connect-mongo-test');
var options_with_mongoose_connection = { mongoose_connection: testMongooseDb.connections[0] };

var testMongoNativeDb = new mongo.Db("connect-mongo-test", new mongo.Server('127.0.0.1', 27017, {}));
var options_with_mongo_native_db = {db: testMongoNativeDb}

var auth_or_not = function(store, db, options, callback){
if (options.username && options.password) {
db.authenticate(options.username, options.password, function () {
Expand All @@ -37,6 +40,8 @@ var open_db = function(options, callback) {
options.mongoose_connection.db.serverConfig.port,
options.mongoose_connection.db.serverConfig.options
));
} else if (typeof options.db == "object") {
db = options.db
} else {
db = new mongo.Db(options.db, new mongo.Server('127.0.0.1', 27017, {}));
}
Expand Down Expand Up @@ -396,3 +401,169 @@ exports.test_options_bad_db_with_raw_db = function(done) {

done();
};

/* tests with existing mongodb native db object */

exports.test_set_with_native_db = function(done) {
open_db(options_with_mongo_native_db, function(store, db, collection) {
var sid = 'test_set-sid';
store.set(sid, {foo:'bar'}, function(err, session) {
assert.strictEqual(err, null);

// Verify it was saved
collection.findOne({_id: sid}, function(err, session) {
assert.deepEqual(session,
{
session: JSON.stringify({foo: 'bar'}),
_id: sid
});

cleanup(store, db, collection, function() {
done();
});
});
});
});
};

exports.test_set_no_stringify_with_native_db = function(done) {
open_db({db: options_with_mongo_native_db.db, stringify: false}, function(store, db, collection) {
var sid = 'test_set-sid';
store.set(sid, {foo: 'bar'}, function(err, session) {
assert.strictEqual(err, null);

// Verify it was saved
collection.findOne({_id: sid}, function(err, session) {
assert.deepEqual(session,
{
session: {foo: 'bar'},
_id: sid
});

cleanup(store, db, collection, function() {
done();
});
});
});
});
};

exports.test_set_expires_with_native_db = function(done) {
open_db(options_with_mongo_native_db, function(store, db, collection) {
var sid = 'test_set_expires-sid';
var data = {
foo:'bar',
cookie:
{
_expires: '2011-04-26T03:10:12.890Z'
}
};

store.set(sid, data, function(err, session) {
assert.strictEqual(err, null);

// Verify it was saved
collection.findOne({_id: sid}, function(err, session) {
assert.deepEqual(session.session, JSON.stringify(data));
assert.strictEqual(session._id, sid);
assert.equal(session.expires.toJSON(), new Date(data.cookie._expires).toJSON());

cleanup(store, db, collection, function() {
done();
});
});
});
});
};

exports.test_get_with_native_db = function(done) {
open_db(options_with_mongo_native_db, function(store, db, collection) {
var sid = 'test_get-sid';
collection.insert({_id: sid, session: JSON.stringify({key1: 1, key2: 'two'})}, function(error, ids) {
store.get(sid, function(err, session) {
assert.strictEqual(err, null);
assert.deepEqual(session, {key1: 1, key2: 'two'});

cleanup(store, db, collection, function() {
done();
});
});
});
});
};

exports.test_length_with_native_db = function(done) {
open_db(options_with_mongo_native_db, function(store, db, collection) {
var sid = 'test_length-sid';
collection.insert({_id: sid, session: JSON.stringify({key1: 1, key2: 'two'})}, function(error, ids) {
store.length(function(err, length) {
assert.strictEqual(err, null);
assert.strictEqual(length, 1);

cleanup(store, db, collection, function() {
done();
});
});
});
});
};

exports.test_destroy_ok_with_native_db = function(done) {
open_db(options_with_mongo_native_db, function(store, db, collection) {
var sid = 'test_destroy_ok-sid';
collection.insert({_id: sid, session: JSON.stringify({key1: 1, key2: 'two'})}, function(error, ids) {
store.destroy(sid, function(err) {
assert.strictEqual(err, undefined);

cleanup(store, db, collection, function() {
done();
});
});
});
});
};

exports.test_clear_with_native_db = function(done) {
open_db(options_with_mongo_native_db, function(store, db, collection) {
var sid = 'test_length-sid';
collection.insert({_id: sid, key1: 1, key2: 'two'}, function(error, ids) {
store.clear(function(err) {
collection.count(function(err, count) {
assert.strictEqual(err, null);
assert.strictEqual(count, 0);

cleanup(store, db, collection, function() {
done();
});
});
});
});
});
};

exports.test_clear_expired_with_native_db = function(done) {
open_db({db: options_with_mongo_native_db.db, clear_interval: 0.1}, function(store, db, collection) {
var sid = 'test_clear_expired-sid';
store.set(sid, {foo:'bar', cookie: {_expires: '2011-04-26T03:10:12.890Z'}}, function(err, session) {
setTimeout(function() {
collection.find({_id: sid}).toArray(function(err, results) {
assert.strictEqual(results.length, 0);

cleanup(store, db, collection, function() {
done();
});
});
}, 150);
});
});
};

exports.test_options_bad_db_with_native_db = function(done) {
assert.throws(
function() {
var store = new MongoStore({db: {}}, function() {});
},
Error);

done();
};

0 comments on commit e0186ed

Please sign in to comment.