Comparing version 1.2.1 to 1.3.0
41
index.js
@@ -1,41 +0,13 @@ | ||
var thunky = require('thunky') | ||
var toMongodbCore = require('to-mongodb-core') | ||
var parse = require('parse-mongo-url') | ||
var Database = require('./lib/database') | ||
var getTopology = require('./lib/get-topology') | ||
var bson = require('mongodb-core').BSON | ||
var xtend = require('xtend') | ||
var getDbName = function (connString) { | ||
if (typeof connString !== 'string') return null | ||
var config = parse(connString) | ||
return config.dbName | ||
} | ||
module.exports = function (connString, cols, options) { | ||
var dbname = getDbName(connString) | ||
if (!options) options = {} | ||
var onserver = thunky(function (cb) { | ||
getTopology(connString, options, function (err, topology) { | ||
if (err) return cb(err) | ||
cb(null, topology) | ||
}) | ||
}) | ||
if (!dbname) { | ||
dbname = connString._dbname | ||
onserver = thunky(function (cb) { | ||
toMongodbCore(connString, function (err, server) { | ||
if (err) cb(new Error('You must pass a connection string or a mongojs instance.')) | ||
cb(null, server) | ||
}) | ||
}) | ||
} | ||
var that = new Database({name: dbname, cols: cols}, onserver) | ||
var db = new Database(xtend({connString: connString, cols: cols}, options)) | ||
if (typeof Proxy !== 'undefined') { | ||
var p = Proxy.create({ | ||
get: function (obj, prop) { | ||
if (that[prop]) return that[prop] | ||
that[prop] = that.collection(prop) | ||
return that[prop] | ||
if (db[prop]) return db[prop] | ||
db[prop] = db.collection(prop) | ||
return db[prop] | ||
} | ||
@@ -46,5 +18,6 @@ }) | ||
} | ||
return that | ||
return db | ||
} | ||
module.exports.ObjectId = bson.ObjectId |
@@ -84,4 +84,5 @@ var mongodb = require('mongodb-core') | ||
} | ||
server.insert(self._fullColName(), docs, writeOpts, function (err) { | ||
server.insert(self._fullColName(), docs, writeOpts, function (err, res) { | ||
if (err) return cb(err) | ||
if (res && res.result && res.result.writeErrors && res.result.writeErrors.length > 0) return cb(res.result.writeErrors[0]) | ||
cb(null, docOrDocs) | ||
@@ -88,0 +89,0 @@ }) |
var Collection = require('./collection') | ||
var bson = require('mongodb-core').BSON | ||
var xtend = require('xtend') | ||
var thunky = require('thunky') | ||
var toMongodbCore = require('to-mongodb-core') | ||
var parse = require('parse-mongo-url') | ||
var getTopology = require('./get-topology') | ||
var connect = require('./connect') | ||
var noop = function () {} | ||
var Database = function (opts, onserver) { | ||
this._getServer = onserver | ||
this._dbname = opts.name | ||
var Database = function (options) { | ||
var self = this | ||
var self = this | ||
if (typeof options.connString === 'string') { | ||
var config = parse(options.connString) | ||
this._dbname = config.dbName | ||
this._server = getTopology(config, options) | ||
this._getServer = thunky(function (cb) { | ||
connect(self._server, config, options, cb) | ||
}) | ||
} else { | ||
this._dbname = options.connString._dbname | ||
this._server = options.connString | ||
this._getServer = thunky(function (cb) { | ||
toMongodbCore(options.connString, function (err, server) { | ||
if (err) return cb(new Error('You must pass a connection string or a mongojs instance.')) | ||
cb(null, server) | ||
}) | ||
}) | ||
} | ||
this.ObjectId = bson.ObjectId | ||
opts.cols = opts.cols || [] | ||
opts.cols.forEach(function (colName) { | ||
options.cols = options.cols || [] | ||
options.cols.forEach(function (colName) { | ||
self[colName] = self.collection(colName) | ||
@@ -29,2 +53,7 @@ | ||
Database.prototype.on = function (event, handler) { | ||
this._server.on(event, handler) | ||
return this | ||
} | ||
Database.prototype.collection = function (colName) { | ||
@@ -38,3 +67,3 @@ return new Collection({name: colName, dbname: this._dbname}, this._getServer) | ||
if (err) return cb(err) | ||
server.destroy() | ||
server.destroy(true, true) | ||
cb() | ||
@@ -41,0 +70,0 @@ }) |
@@ -1,4 +0,3 @@ | ||
var once = require('once') | ||
var parse = require('parse-mongo-url') | ||
var mongodb = require('mongodb-core') | ||
var xtend = require('xtend') | ||
@@ -8,23 +7,5 @@ var Server = mongodb.Server | ||
var authMechanisms = { | ||
MongoCR: mongodb.MongoCR, | ||
ScramSHA1: mongodb.ScramSHA1 | ||
} | ||
module.exports = function (connString, options, cb) { | ||
cb = once(cb) | ||
var config = parse(connString) | ||
var srv | ||
var authMechanism = 'MongoCR' | ||
if (options && options.authMechanism) { | ||
if (!authMechanisms[options.authMechanism]) { | ||
return cb(new Error(options.authMechanism + ' is not a supported authentication mechanism')) | ||
} | ||
authMechanism = options.authMechanism | ||
} | ||
module.exports = function (config, options) { | ||
if (config.servers.length === 1) { | ||
var opts = config.server_options | ||
var opts = xtend(config.server_options, options) | ||
opts.host = config.servers[0].host || 'localhost' | ||
@@ -34,30 +15,10 @@ opts.port = config.servers[0].port || 27017 | ||
opts.reconnectInterval = 50 | ||
srv = new Server(opts) | ||
return new Server(opts) | ||
} else { | ||
var rsopts = config.rs_options | ||
var rsopts = xtend(config.rs_options, options) | ||
rsopts.setName = rsopts.rs_name | ||
rsopts.reconnect = true | ||
rsopts.reconnectInterval = 50 | ||
srv = new ReplSet(config.servers, rsopts) | ||
return new ReplSet(config.servers, rsopts) | ||
} | ||
if (config.auth) { | ||
srv.addAuthProvider(authMechanism, new authMechanisms[authMechanism]()) | ||
srv.on('connect', function (server) { | ||
server.auth(authMechanism, config.dbName, config.auth.user, config.auth.password, function (err, r) { | ||
if (err) return cb(err) | ||
cb(null, r) | ||
}) | ||
}) | ||
} else { | ||
srv.on('connect', function (server) { | ||
cb(null, server) | ||
}) | ||
} | ||
srv.on('error', function (err) { | ||
cb(err) | ||
}) | ||
srv.connect() | ||
} |
@@ -9,3 +9,3 @@ { | ||
], | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"repository": "git://github.com/mafintosh/mongojs.git", | ||
@@ -54,5 +54,5 @@ "author": "Mathias Buus Madsen <mathiasbuus@gmail.com>", | ||
"coordinates": [ | ||
55.6876775, | ||
12.5956158 | ||
48.2286896, | ||
16.3953734 | ||
] | ||
} |
@@ -238,3 +238,3 @@ # mongojs | ||
## Upgrading from 0.x.x to 1.0.x | ||
## Upgrading from 0.x.x to 1.2.x | ||
@@ -244,4 +244,2 @@ Version 1.0.x is a major rewrite of mongojs using mongodb-core driver. So expect some things not to work the same as in mongojs 0.x.x versions. Breaking changes include: | ||
* __Removed__ `mongojs.connect` use `mongojs()` directly instead | ||
* __Not Working__ Currently events are not working. If your code depends on event listeners don't upgrade right now. Will be fixed! | ||
* Collection.aggregate does currently not support an aggregation pipeline passed as array but as parameters. Instead of `db.reviews.aggregate([step1, step2], function() { ... })` use `db.reviews.aggregate(step1, step2, function() { ... })` | ||
@@ -248,0 +246,0 @@ # API |
Sorry, the diff of this file is not supported yet
100062
73
1756
374