Skip to content

Commit

Permalink
Add ability to connect with URI
Browse files Browse the repository at this point in the history
  • Loading branch information
powmedia committed Aug 21, 2013
1 parent 1259b6d commit 415c1d8
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 16 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ Installation
Changelog
---------

###0.10.0
- Update mongodb driver to 1.3.x
- Add ability to connect with URI
- Make safe mode the default

###0.8.1
- Add mongofixtures CLI program

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Charles Davison <[email protected]>",
"name": "pow-mongodb-fixtures",
"description": "Easy JSON fixture loading for MongoDB. Makes managing document relationships easier.",
"version": "0.9.0",
"version": "0.10.0",
"repository": {
"type" : "git",
"url" : "http://github.com/powmedia/pow-mongodb-fixtures.git"
Expand All @@ -15,7 +15,7 @@
},
"dependencies": {
"async": "0.1.15",
"mongodb": ">=0.9.9-8",
"mongodb": ">=1.3.0",
"underscore": ">=1.2.2",
"bson": ">=0.0.4",
"optimist": "0.3.5"
Expand Down
78 changes: 64 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//Dependencies
var fs = require('fs'),
url = require('url'),
path = require('path'),
mongo = require('mongodb'),
ObjectID = require('bson').ObjectID,
Expand All @@ -26,35 +27,53 @@ exports.createObjectId = function(id) {
/**
* Main method for connecting to the database and returning the fixture loader (Loader)
*
* @param {String} Database name
* @param {Object} Connection options: host ('localhost'), port (27017)
* @param {String} dbOrUri Database name or connection URI
* @param {Object} [options] Connection options: host ('localhost'), port (27017)
*/
exports.connect = function(dbName, options) {
return new Loader(dbName, options);
exports.connect = function(db, options) {
return new Loader(db, options);
}



/**
* Loader constructor
*
* @param {String} dbName Database name
* @param {String} dbOrUri Database name or connection URI
* @param {Object} [options] Connection options
* @param {String} [options.host] Default: 'localhost'
* @param {Number} [options.port] Default: 27017
* @param {String} [options.user] Username
* @param {String} [options.pass] Password
* @param {Boolean} [options.safe] Default: false
*/
var Loader = exports.Loader = function(dbName, options) {
options = _.extend({
db: dbName,
host: 'localhost',
port: 27017,
user: null,
pass: null,
safe: false
}, options);
var Loader = exports.Loader = function(dbOrUri, options) {
//Try parsing uri
var parts = url.parse(dbOrUri);

//Using connection URI
if (parts.protocol) {
options = _.extend({
db: parts.path.replace('/', ''),
host: parts.hostname,
port: parseInt(parts.port, 10),
user: parts.auth ? parts.auth.split(':')[0] : null,
pass: parts.auth ? parts.auth.split(':')[1] : null,
safe: true
}, options);
}

//Using DB name
else {
options = _.extend({
db: dbOrUri,
host: 'localhost',
port: 27017,
user: null,
pass: null,
safe: true
}, options);
}

this.options = options;
this.modifiers = [];
Expand Down Expand Up @@ -456,3 +475,34 @@ var _dirToObject = function(dir, cb) {
cb(null, combinedData);
});
};


/**
* Builds the full connection URI
*
* @param {Object} options
*
* @return {String}
*/
var _buildConnectionUri = function(options) {
var parts = ['mongodb://'];

if (options.user) parts.push(options.user);

if (options.pass) {
parts.push(':');
parts.push(options.pass);
}

if (options.user) {
parts.push('@');
}

parts.push(options.host);
parts.push(':');
parts.push(options.port);
parts.push('/');
parts.push(options.db);

return parts.join('');
}
32 changes: 32 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,38 @@ exports['createObjectId'] = {
};


exports['connect with dbName'] = function(test) {
var loader = fixtures.connect(dbName);

var options = loader.options;

test.same(options.db, 'pow-mongodb-fixtures-test');
test.same(options.host, 'localhost');
test.same(options.port, 27017);
test.same(options.user, null);
test.same(options.pass, null);
test.same(options.safe, true);

test.done();
}


exports['connect with uri'] = function(test) {
var loader = fixtures.connect('mongodb://username:[email protected]:9191/dbname');

var options = loader.options;

test.same(options.db, 'dbname');
test.same(options.host, 'example.com');
test.same(options.port, 9191);
test.same(options.user, 'username');
test.same(options.pass, 'password');
test.same(options.safe, true);

test.done();
}


exports['works when referencing an objectID in different scope'] = {
'when using this': function(test) {
var todo = 'TODO: Havent been able to replicate error yet:';
Expand Down

0 comments on commit 415c1d8

Please sign in to comment.