Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #35 from visnup/url

Support passing a URL option.
  • Loading branch information...
commit 3a5d513846484840230626b822004d48d72047e3 2 parents df60c2b + eefe977
@masylum masylum authored
Showing with 17 additions and 1 deletion.
  1. +15 −1 lib/connect-mongodb.js
  2. +2 −0  tests/core.js
View
16 lib/connect-mongodb.js
@@ -25,7 +25,7 @@ var MONGOSTORE = module.exports = function MongoStore(options, callback) {
options = options || {};
callback = _default(callback);
- var db, server_config;
+ var db, server_config, url, auth;
function getCollection(db, callback) {
db.collection(options.collection || _defaults.collection, function (err, col) {
@@ -45,6 +45,20 @@ var MONGOSTORE = module.exports = function MongoStore(options, callback) {
}
}
+ if (options.url) {
+ url = require('url').parse(options.url);
+
+ if (url.auth) {
+ auth = url.auth.split(':', 2);
+ options.username = auth[0];
+ options.password = auth[1];
+ }
+
+ options.db = new mongo.Db(url.pathname.replace(/^\//, ''),
+ new mongo.Server(url.hostname || _defaults.host,
+ url.port || _defaults.port));
+ }
+
if (options.server_config) {
server_config = options.server_config;
db = new mongo.Db(options.dbname || _defaults.dbname, server_config);
View
2  tests/core.js
@@ -4,6 +4,7 @@ var testosterone = require('testosterone')({title: 'models/advertiser'})
, Db = require('mongodb').Db
, Server = require('mongodb').Server
, server_config = new Server('localhost', 27017, {auto_reconnect: true, native_parser: true})
+ , url = 'mongodb://localhost:27017/test'
, db = new Db('test', server_config, {});
testosterone
@@ -16,6 +17,7 @@ testosterone
require('..')({db: null}, funk.add(assert.ok));
require('..')({db: db, setInterval: -1}, funk.add(assert.ifError));
require('..')({server_config: server_config, setInterval: -1}, funk.add(assert.ifError));
+ require('..')({url: url, setInterval: -1}, funk.add(assert.ifError));
funk.run(done);
});
})
Please sign in to comment.
Something went wrong with that request. Please try again.