Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Now accepts either an existing connection, or a regular node-mongodb-…

…native server configuration
  • Loading branch information...
commit 9caca15ea89096cad6813b82dcfad3f037c8edc4 1 parent 09205d1
@tedeh authored
Showing with 43 additions and 27 deletions.
  1. +43 −27 lib/connect-mongodb.js
View
70 lib/connect-mongodb.js
@@ -98,13 +98,10 @@ var Store = require('connect').session.Store,
*/
var MONGOSTORE = module.exports = function MongoStore(options) {
-
options = options || {};
-
- var _url = getConnectionURL(options),
- _details = parseConnectionURL(_url), // mongodb 0.7.9 parser is broken, this fixes it
- _db,
-
+
+ var _db,
+
_getCollection = function (_db) {
_db.collection(options.collection || defaults.collection, function (err, col) {
if (err) {
@@ -114,37 +111,56 @@ var MONGOSTORE = module.exports = function MongoStore(options) {
});
},
servers = [], i, len;
-
- if (!_details.length) {
- _db = new mongo.Db(_details.dbname, new mongo.Server(_details.host, _details.port, options));
- } else {
- for (i = 0, len = _details.length; i < len; i++) {
- servers.push(new mongo.Server(_details[i].host, _details[i].port, {}));
+
+ // If options.serverConfig instance of either, true
+ if ([mongo.Server, mongo.ServerPair, mongo.ServerCluster, mongo.ReplSetServers].some(function(i) { return options.serverConfig instanceof i; })) {
+ _db = new mongo.Db(options.dbname || defaults.dbname, options.serverConfig);
+ }
+
+ if (options.handle instanceof mongo.Db) {
+ _db = options.handle;
+ }
+
+ if (!_db) {
+ var _url = getConnectionURL(options),
+ _details = parseConnectionURL(_url); // mongodb 0.7.9 parser is broken, this fixes it
+
+ if (!_details.length) {
+ _db = new mongo.Db(_details.dbname, new mongo.Server(_details.host, _details.port, options));
+ } else {
+ for (i = 0, len = _details.length; i < len; i++) {
+ servers.push(new mongo.Server(_details[i].host, _details[i].port, {}));
+ }
+ _db = new mongo.Db(_details[0].dbname, new mongo.ReplSetServers(servers));
}
- _db = new mongo.Db(_details[0].dbname, new mongo.ReplSetServers(servers));
}
-
+
Store.call(this, options);
-
+
if (options.reapInterval !== -1) {
setInterval(function () {
_collection.remove({expires: {'$lte': Date.now()}}, function () { });
}, options.reapInterval || 60 * 1000, this); // defaults to each minute
}
+
+ if (_db.serverConfig && !_db.serverConfig.isConnected()) {
+ _db.open(function (err) {
+ if (err) {
+ throw Error("Error connecting to " + _url + " (" + (err instanceof Error ? err.message : err) + ")");
+ }
- _db.open(function (err) {
- if (err) {
- throw Error("Error connecting to " + _url);
- }
-
- if (_details.username && _details.password) {
- _db.authenticate(_details.username, _details.password, function () {
+ if (_details.username && _details.password) {
+ _db.authenticate(_details.username, _details.password, function () {
+ _getCollection(_db);
+ });
+ } else {
_getCollection(_db);
- });
- } else {
- _getCollection(_db);
- }
- });
+ }
+ });
+ } else {
+ _getCollection(_db);
+ }
+
};
MONGOSTORE.prototype.__proto__ = Store.prototype;
Please sign in to comment.
Something went wrong with that request. Please try again.