Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…native server configuration
  • Loading branch information...
commit 9caca15ea89096cad6813b82dcfad3f037c8edc4 1 parent 09205d1
Tedde Lundgren authored June 13, 2011

Showing 1 changed file with 43 additions and 27 deletions. Show diff stats Hide diff stats

  1. 70  lib/connect-mongodb.js
70  lib/connect-mongodb.js
@@ -98,13 +98,10 @@ var Store = require('connect').session.Store,
98 98
  */
99 99
 
100 100
 var MONGOSTORE = module.exports = function MongoStore(options) {
101  
-
102 101
   options = options || {};
103  
-
104  
-  var _url = getConnectionURL(options),
105  
-      _details = parseConnectionURL(_url), // mongodb 0.7.9 parser is broken, this fixes it
106  
-      _db,
107  
-
  102
+  
  103
+  var _db,
  104
+  
108 105
       _getCollection = function (_db) {
109 106
         _db.collection(options.collection || defaults.collection, function (err, col) {
110 107
           if (err) {
@@ -114,37 +111,56 @@ var MONGOSTORE = module.exports = function MongoStore(options) {
114 111
         });
115 112
       },
116 113
       servers = [], i, len;
117  
-
118  
-  if (!_details.length) {
119  
-    _db = new mongo.Db(_details.dbname, new mongo.Server(_details.host, _details.port, options));
120  
-  } else {
121  
-    for (i = 0, len = _details.length; i < len; i++) {
122  
-      servers.push(new mongo.Server(_details[i].host, _details[i].port, {}));
  114
+  
  115
+  // If options.serverConfig instance of either, true
  116
+  if ([mongo.Server, mongo.ServerPair, mongo.ServerCluster, mongo.ReplSetServers].some(function(i) { return options.serverConfig instanceof i; })) {
  117
+    _db = new mongo.Db(options.dbname || defaults.dbname, options.serverConfig);
  118
+  }
  119
+  
  120
+  if (options.handle instanceof mongo.Db) {
  121
+    _db = options.handle;
  122
+  }
  123
+  
  124
+  if (!_db) {
  125
+    var _url = getConnectionURL(options),
  126
+        _details = parseConnectionURL(_url); // mongodb 0.7.9 parser is broken, this fixes it
  127
+      
  128
+    if (!_details.length) {
  129
+      _db = new mongo.Db(_details.dbname, new mongo.Server(_details.host, _details.port, options));
  130
+    } else {
  131
+      for (i = 0, len = _details.length; i < len; i++) {
  132
+        servers.push(new mongo.Server(_details[i].host, _details[i].port, {}));
  133
+      }
  134
+      _db = new mongo.Db(_details[0].dbname, new mongo.ReplSetServers(servers));
123 135
     }
124  
-    _db = new mongo.Db(_details[0].dbname, new mongo.ReplSetServers(servers));
125 136
   }
126  
-
  137
+  
127 138
   Store.call(this, options);
128  
-
  139
+  
129 140
   if (options.reapInterval !== -1) {
130 141
     setInterval(function () {
131 142
       _collection.remove({expires: {'$lte': Date.now()}}, function () { });
132 143
     }, options.reapInterval || 60 * 1000, this); // defaults to each minute
133 144
   }
  145
+  
  146
+  if (_db.serverConfig && !_db.serverConfig.isConnected()) {
  147
+    _db.open(function (err) {
  148
+      if (err) {
  149
+        throw Error("Error connecting to " + _url + " (" + (err instanceof Error ? err.message : err) + ")");
  150
+      }
134 151
 
135  
-  _db.open(function (err) {
136  
-    if (err) {
137  
-      throw Error("Error connecting to " + _url);
138  
-    }
139  
-
140  
-    if (_details.username && _details.password) {
141  
-      _db.authenticate(_details.username, _details.password, function () {
  152
+      if (_details.username && _details.password) {
  153
+        _db.authenticate(_details.username, _details.password, function () {
  154
+          _getCollection(_db);
  155
+        });
  156
+      } else {
142 157
         _getCollection(_db);
143  
-      });
144  
-    } else {
145  
-      _getCollection(_db);
146  
-    }
147  
-  });
  158
+      }
  159
+    });
  160
+  } else {
  161
+    _getCollection(_db);
  162
+  }
  163
+  
148 164
 };
149 165
 
150 166
 MONGOSTORE.prototype.__proto__ = Store.prototype;

0 notes on commit 9caca15

Please sign in to comment.
Something went wrong with that request. Please try again.