Browse Source

Implement mongojs and mongodb-native connection passing

saintedlama/travis-non-legacy
saintedlama 9 years ago
parent
commit
60297a3779
  1. 18
      lib/database.js
  2. 39
      test/test-pass-mongodb.js
  3. 4
      test/test-pass-mongojs.js
  4. 8
      test/test-to-string.js

18
lib/database.js

@ -2,7 +2,7 @@ var Collection = require('./collection')
var mongodb = require('mongodb') var mongodb = require('mongodb')
var xtend = require('xtend') var xtend = require('xtend')
var thunky = require('thunky') var thunky = require('thunky')
// var parse = require('parse-mongo-url') var parse = require('parse-mongo-url')
var noop = function () {} var noop = function () {}
@ -11,18 +11,26 @@ var Database = function (connString, cols, options) {
// TODO: Cleanup theses options - including xtend stuff in index.js // TODO: Cleanup theses options - including xtend stuff in index.js
if (typeof connString === 'string') { if (typeof connString === 'string') {
this._dbname = parse(connString).dbName
// TODO: Fix - protocol, host and port should come from but should be 'added' in case they are missing - also take rs, mongos in consideration
if (connString.indexOf('mongodb://') < 0) { if (connString.indexOf('mongodb://') < 0) {
// TODO: Fix - protocol, host and port should come from but should be 'added' in case they are missing connString = 'mongodb://localhost/' + connString
connString = 'mongodb://localhost:27017/' + connString
} }
this._getConnection = thunky(function (cb) { this._getConnection = thunky(function (cb) {
mongodb.connect(connString, options, cb) mongodb.connect(connString, options, cb)
}) })
} else if (connString instanceof Database) { // mongojs } else if (typeof connString._getConnection === 'function') { // mongojs
this._dbname = connString._dbname
this._getConnection = connString._getConnection this._getConnection = connString._getConnection
} else { // try mongodb-native } else { // try mongodb-native
// TODO: Implement mongodb connections // TODO: Make this safe - even for rs connection strings
this._dbname = parse(connString.options.url).dbName
this._getConnection = thunky(function (cb) {
cb(null, connString)
})
} }
this.ObjectId = mongodb.ObjectId this.ObjectId = mongodb.ObjectId

39
test/test-pass-mongodb.js

@ -0,0 +1,39 @@
var test = require('./tape')
var mongojs = require('../')
var MongoClient = require('mongodb').MongoClient
test('receive a mongodb db instance', function (t) {
MongoClient.connect('mongodb://localhost/test', function (err, mongoDb) {
t.error(err)
var db = mongojs(mongoDb, ['a'])
var afterFind = function () {
db.a.remove(function (err) {
t.error(err)
t.equal(db.toString(), 'test', 'should expose database name')
db.close(function (err) {
t.error(err)
t.end()
})
})
}
var afterInsert = function (err) {
t.error(err)
db.a.findOne(function (err, data) {
t.error(err)
t.equal(data.name, 'Pidgey')
afterFind()
})
}
var afterRemove = function (err) {
t.error(err)
db.a.insert({name: 'Pidgey'}, afterInsert)
}
db.a.remove(afterRemove)
})
})

4
test/test-pass-driver-db.js → test/test-pass-mongojs.js

@ -1,13 +1,13 @@
var test = require('./tape') var test = require('./tape')
var mongojs = require('../') var mongojs = require('../')
test('receive a driver db or mongojs instance', function (t) { test('receive a mongojs instance', function (t) {
var db = mongojs(mongojs('test', []), ['a']) var db = mongojs(mongojs('test', []), ['a'])
var afterFind = function () { var afterFind = function () {
db.a.remove(function (err) { db.a.remove(function (err) {
t.error(err) t.error(err)
t.equal(db.toString(), 'test') t.equal(db.toString(), 'test', 'should expose database name')
db.close(function (err) { db.close(function (err) {
t.error(err) t.error(err)

8
test/test-to-string.js

@ -0,0 +1,8 @@
var test = require('./tape')
var mongojs = require('../')
test('db.toString', function (t) {
var db = mongojs('test', ['a'])
t.equal(db.toString(), 'test', 'toString should return database name')
t.end()
})
Loading…
Cancel
Save