east-mongo
Advanced tools
Comparing version 0.3.5 to 0.3.6
@@ -5,2 +5,3 @@ 'use strict'; | ||
path = require('path'), | ||
mongodbUri = require('mongodb-uri'), | ||
helpers = require('./helpers'); | ||
@@ -10,5 +11,8 @@ | ||
this.params = params || {}; | ||
if (!this.params.url) throw new Error('`url` parameter required'); | ||
this.helpers= helpers; | ||
if (!this.params.url) { | ||
throw new Error('`url` parameter required'); | ||
} | ||
this.helpers = helpers; | ||
} | ||
@@ -20,13 +24,37 @@ | ||
// mongodb driver 2.x passes `db`, 3.x passes `client` to the | ||
// `MongoClient.connect` callback this method sets clitnt and db | ||
// parsed from this polymorphic parameter | ||
Adapter.prototype._setDbAndClient = function(dbOrClient) { | ||
if (dbOrClient.collection) { | ||
this.client = null; | ||
this.db = dbOrClient; | ||
} else { | ||
this.client = dbOrClient; | ||
var parsedUrl = mongodbUri.parse(this.params.url); | ||
var dbName = parsedUrl.database || 'admin'; | ||
this.db = this.client.db(dbName); | ||
} | ||
}; | ||
Adapter.prototype.connect = function(callback) { | ||
var self = this; | ||
MongoClient.connect( | ||
self.params.url, | ||
self.params.options || {}, | ||
function(err, db) { | ||
if (err) {callback(err); return;} | ||
self.db = db; | ||
self.collection = db.collection('_migrations'); | ||
function(err, dbOrClient) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
self._setDbAndClient(dbOrClient); | ||
self.collection = self.db.collection('_migrations'); | ||
callback(null, { | ||
db: db, | ||
db: self.db, | ||
dropIndexIfExists: self.helpers.dropIndexIfExists | ||
@@ -39,3 +67,5 @@ }); | ||
Adapter.prototype.disconnect = function(callback) { | ||
if (this.db) { | ||
if (this.client) { | ||
this.client.close(callback); | ||
} else if (this.db) { | ||
this.db.close(callback); | ||
@@ -49,3 +79,6 @@ } else { | ||
this.collection.find({}).toArray(function(err, docs) { | ||
if (err) {callback(err); return;} | ||
if (err) { | ||
return callback(err); | ||
} | ||
callback(null, docs.map(function(doc) { return doc._id; })); | ||
@@ -52,0 +85,0 @@ }); |
{ | ||
"name": "east-mongo", | ||
"description": "mongodb adapter for east (node.js database migration tool)", | ||
"version": "0.3.5", | ||
"version": "0.3.6", | ||
"author": "Oleg Korobenko <oleg.korobenko@gmail.com>", | ||
@@ -24,3 +24,6 @@ "license": "MIT", | ||
"mongodb": "2.x.x" | ||
}, | ||
"dependencies": { | ||
"mongodb-uri": "0.9.7" | ||
} | ||
} |
7753
100
2
+ Addedmongodb-uri@0.9.7
+ Addedmongodb-uri@0.9.7(transitive)