Browse Source

If we receive a mongojs instance we connect and recreate the instance,

so we can have the collections passed in the call to mongojs.
saintedlama/travis-non-legacy
Eduardo Sorribas 10 years ago
parent
commit
94622bec0c
  1. 11
      index.js
  2. 1
      package.json
  3. 12
      test/test-pass-driver-db.js
  4. 9
      test/test-pass-mongojs-db.js

11
index.js

@ -493,9 +493,10 @@ var isMongojsDb = function(db) {
};
var connect = function(config, collections) {
if (isMongojsDb(config)) return config;
if (isDriverDb(config)) {
var driverDb = config;
} else if (isMongojsDb(config)) {
var mongojsDb = config;
} else {
var connectionString = parseConfig(config);
var dbName = (connectionString.match(/\/([^\/\?]+)(\?|$)/) || [])[1] || 'db';
@ -519,6 +520,14 @@ var connect = function(config, collections) {
callback(null, driverDb);
};
}
if (mongojsDb) {
ondb = function(callback) {
mongojsDb.open(function(err, db) {
if (err) callback(err);
callback(null, db);
});
}
}
var that = new Database(dbName, ondb);
that.bson = mongodb.BSONPure; // backwards compat (require('bson') instead)

1
package.json

@ -33,6 +33,7 @@
"test": "tape test/test-*.js; echo \"Harmony tests\"; node --harmony node_modules/tape/bin/tape test/test-*.js"
},
"devDependencies": {
"each-series": "^0.2.0",
"tape": "^2.13.4"
}
}

12
test/test-pass-driver-db.js

@ -1,16 +1,17 @@
var test = require('tape');
var mongodb = require('mongodb');
var mongojs = require('../');
var each = require('each-series');
test('receive a driver db instance', function(t) {
test('receive a driver db or mongojs instance', function(t) {
mongodb.Db.connect('mongodb://localhost/test', function(err, thedb) {
var db = mongojs(thedb, ['a']);
var doTests = function(db, callback) {
var afterFind = function() {
db.a.remove(function(err) {
t.ok(!err);
t.equal(db.toString(), 'test');
t.end();
callback();
});
};
@ -29,5 +30,10 @@ test('receive a driver db instance', function(t) {
};
db.a.remove(afterRemove);
};
each([mongojs(thedb, ['a']), mongojs(mongojs('test', []), ['a'])], function(db, i, cb) {
doTests(db, cb);
}, t.end.bind(t));
});
});

9
test/test-pass-mongojs-db.js

@ -1,9 +0,0 @@
var test = require('tape');
var mongojs = require('../');
var db = mongojs('test');
test('receive a driver db instance', function(t) {
var thedb = mongojs(db);
t.ok(db === thedb);
t.end();
});
Loading…
Cancel
Save