diff --git a/README.md b/README.md index 79ae7e1..b0b9356 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index 2f66dcf..e5bbcb4 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Charles Davison ", "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" @@ -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" diff --git a/src/index.js b/src/index.js index e0ca102..e2e9a73 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ //Dependencies var fs = require('fs'), + url = require('url'), path = require('path'), mongo = require('mongodb'), ObjectID = require('bson').ObjectID, @@ -26,11 +27,11 @@ 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); } @@ -38,7 +39,7 @@ exports.connect = function(dbName, 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 @@ -46,15 +47,33 @@ exports.connect = function(dbName, options) { * @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 = []; @@ -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(''); +} \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index b598908..b74c18f 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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:password@example.com: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:';