Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* Merge pull request.

  • Loading branch information...
commit 3b5e48ef972efcc51bec8bf1fe251bdde5113c5e 2 parents e4b8d78 + e75692b
@kcbanner authored
View
3  Readme.md
@@ -24,6 +24,7 @@ via npm:
option (optional, default: false).
- `url` Connection url of the form: `mongodb://user:pass@host:port/database/collection`.
If provided, information in the URL takes priority over the other options.
+ - `mongooseConnection` in the form: `someMongooseDb.connections[0]` to use an existing mongoose connection. (optional)
- `clear_interval` Interval in seconds to clear expired sessions (optional, default: `-1`).
Values <= 0 disable expired session clearing.
- `stringify` If true, connect-mongo will serialize sessions using `JSON.stringify` before
@@ -86,4 +87,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
37 lib/connect-mongo.js
@@ -75,19 +75,32 @@ module.exports = function(connect) {
}
}
- if(!options.db) {
- throw new Error('Required MongoStore option `db` missing');
+ if (options.mongooseConnection){
+ if (options.mongooseConnection.user && options.mongooseConnection.pass) {
+ options.username = options.mongooseConnection.user;
+ options.password = options.mongooseConnection.pass;
+ }
+
+ this.db = new mongo.Db(options.mongooseConnection.db.databaseName,
+ new mongo.Server(options.mongooseConnection.db.serverConfig.host,
+ options.mongooseConnection.db.serverConfig.port,
+ options.mongooseConnection.db.serverConfig.options
+ ));
+
+ } else {
+ if(!options.db) {
+ 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
+ }));
}
- 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') ?
@@ -260,4 +273,4 @@ module.exports = function(connect) {
};
return MongoStore;
-};
+};
View
3  package.json
@@ -9,7 +9,8 @@
},
"devDependencies": {
"mocha": "1.x",
- "connect": ">= 1.x"
+ "connect": ">= 1.x",
+ "mongoose": ">= 2.6.x"
},
"scripts" : { "test": "make test" },
"main": "index",
View
203 test/connect-mongo.test.js
@@ -10,15 +10,45 @@ var assert = require('assert');
var options = {db: 'connect-mongo-test'};
var mongo = require('mongodb');
-var open_db = function(options, callback) {
- var store = new MongoStore(options, function() {
- var db = new mongo.Db(options.db, new mongo.Server('127.0.0.1', 27017, {}));
-
- db.open(function(err) {
+var mongoose = require('mongoose');
+var testMongooseDb = mongoose.connect('mongodb://127.0.0.1:27017/connect-mongo-test');
+var options_with_mongoose_connection = { mongooseConnection: testMongooseDb.connections[0] };
+
+var auth_or_not = function(store, db, options, callback){
+ if (options.username && options.password) {
+ db.authenticate(options.username, options.password, function () {
db.collection('sessions', function(err, collection) {
callback(store, db, collection);
});
});
+ } else {
+ db.collection('sessions', function(err, collection) {
+ callback(store, db, collection);
+ });
+ }
+};
+
+var open_db = function(options, callback) {
+ var store = new MongoStore(options, function() {
+ var db;
+ if (options.mongooseConnection) {
+ db = new mongo.Db(options.mongooseConnection.db.databaseName,
+ new mongo.Server(options.mongooseConnection.db.serverConfig.host,
+ options.mongooseConnection.db.serverConfig.port,
+ options.mongooseConnection.db.serverConfig.options
+ ));
+ } else {
+ db = new mongo.Db(options.db, new mongo.Server('127.0.0.1', 27017, {}));
+ }
+
+ if (db.openCalled) {
+ auth_or_not(store, db, options, callback);
+ } else {
+ db.open(function(err) {
+ auth_or_not(store, db, options, callback);
+ });
+ }
+
});
};
@@ -234,4 +264,165 @@ exports.test_options_url_and_db = function(done){
cleanup_store(store);
done();
});
-}
+};
+
+/* options.mongooseConnection tests */
+
+exports.test_set_with_raw_db = function(done) {
+ open_db(options_with_mongoose_connection, 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_raw_db = function(done) {
+ open_db({mongooseConnection: options_with_mongoose_connection.mongooseConnection, 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_raw_db = function(done) {
+ open_db(options_with_mongoose_connection, 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_raw_db = function(done) {
+ open_db(options_with_mongoose_connection, 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.deepEqual(session, {key1: 1, key2: 'two'});
+ cleanup(store, db, collection, function() {
+ done();
+ });
+ });
+ });
+ });
+};
+
+exports.test_length_with_raw_db = function(done) {
+ open_db(options_with_mongoose_connection, 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_raw_db = function(done) {
+ open_db(options_with_mongoose_connection, 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_raw_db = function(done) {
+ open_db(options_with_mongoose_connection, 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(count, 0);
+
+ cleanup(store, db, collection, function() {
+ done();
+ });
+ });
+ });
+ });
+ });
+};
+
+exports.test_clear_expired_with_raw_db = function(done) {
+ open_db({mongooseConnection: options_with_mongoose_connection.mongooseConnection, 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_raw_db = function(done) {
+ assert.throws(
+ function() {
+ var store = new MongoStore({mongooseConnection: 'foobar'}, function() {});
+ },
+ Error);
+
+ done();
+};
Please sign in to comment.
Something went wrong with that request. Please try again.