Updated mongoose

This commit is contained in:
Dobie Wollert
2015-11-24 22:08:58 -08:00
parent 71a05fb732
commit 8b5827c970
618 changed files with 122299 additions and 37754 deletions

View File

@ -2,13 +2,13 @@
* Module dependencies.
*/
var MongooseConnection = require('../../connection')
, mongo = require('mongodb')
, Db = mongo.Db
, Server = mongo.Server
, Mongos = mongo.Mongos
, STATES = require('../../connectionstate')
, ReplSetServers = mongo.ReplSetServers;
var MongooseConnection = require('../../connection'),
mongo = require('mongodb'),
Db = mongo.Db,
Server = mongo.Server,
Mongos = mongo.Mongos,
STATES = require('../../connectionstate'),
ReplSetServers = mongo.ReplSet;
/**
* A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) connection implementation.
@ -20,7 +20,14 @@ var MongooseConnection = require('../../connection')
function NativeConnection() {
MongooseConnection.apply(this, arguments);
this._listening = false;
};
}
/**
* Expose the possible connection states.
* @api public
*/
NativeConnection.STATES = STATES;
/*!
* Inherits from Connection.
@ -36,16 +43,18 @@ NativeConnection.prototype.__proto__ = MongooseConnection.prototype;
* @api private
*/
NativeConnection.prototype.doOpen = function (fn) {
if (this.db) {
mute(this);
NativeConnection.prototype.doOpen = function(fn) {
var server = new Server(this.host, this.port, this.options.server);
if (this.options && this.options.mongos) {
var mongos = new Mongos([server], this.options.mongos);
this.db = new Db(this.name, mongos, this.options.db);
} else {
this.db = new Db(this.name, server, this.options.db);
}
var server = new Server(this.host, this.port, this.options.server);
this.db = new Db(this.name, server, this.options.db);
var self = this;
this.db.open(function (err) {
this.db.open(function(err) {
if (err) return fn(err);
listen(self);
fn();
@ -54,15 +63,75 @@ NativeConnection.prototype.doOpen = function (fn) {
return this;
};
/**
* Switches to a different database using the same connection pool.
*
* Returns a new connection object, with the new db.
*
* @param {String} name The database name
* @return {Connection} New Connection Object
* @api public
*/
NativeConnection.prototype.useDb = function(name) {
// we have to manually copy all of the attributes...
var newConn = new this.constructor();
newConn.name = name;
newConn.base = this.base;
newConn.collections = {};
newConn.models = {};
newConn.replica = this.replica;
newConn.hosts = this.hosts;
newConn.host = this.host;
newConn.port = this.port;
newConn.user = this.user;
newConn.pass = this.pass;
newConn.options = this.options;
newConn._readyState = this._readyState;
newConn._closeCalled = this._closeCalled;
newConn._hasOpened = this._hasOpened;
newConn._listening = false;
// First, when we create another db object, we are not guaranteed to have a
// db object to work with. So, in the case where we have a db object and it
// is connected, we can just proceed with setting everything up. However, if
// we do not have a db or the state is not connected, then we need to wait on
// the 'open' event of the connection before doing the rest of the setup
// the 'connected' event is the first time we'll have access to the db object
var self = this;
if (this.db && this._readyState === STATES.connected) {
wireup();
} else {
this.once('connected', wireup);
}
function wireup() {
newConn.db = self.db.db(name);
newConn.onOpen();
// setup the events appropriately
listen(newConn);
}
newConn.name = name;
// push onto the otherDbs stack, this is used when state changes
this.otherDbs.push(newConn);
newConn.otherDbs.push(this);
return newConn;
};
/*!
* Register listeners for important events and bubble appropriately.
*/
function listen (conn) {
function listen(conn) {
if (conn._listening) return;
conn._listening = true;
conn.db.on('close', function(){
conn.db.on('close', function() {
if (conn._closeCalled) return;
// the driver never emits an `open` event. auto_reconnect still
@ -75,33 +144,26 @@ function listen (conn) {
}
conn.onClose();
});
conn.db.on('error', function(err){
conn.db.on('error', function(err) {
conn.emit('error', err);
});
conn.db.on('timeout', function(err){
conn.db.on('reconnect', function() {
conn.readyState = STATES.connected;
conn.emit('reconnected');
});
conn.db.on('timeout', function(err) {
var error = new Error(err && err.err || 'connection timeout');
conn.emit('error', error);
});
conn.db.on('open', function (err, db) {
conn.db.on('open', function(err, db) {
if (STATES.disconnected === conn.readyState && db && db.databaseName) {
conn.readyState = STATES.connected;
conn.emit('reconnected')
conn.emit('reconnected');
}
})
}
/*!
* Remove listeners registered in `listen`
*/
function mute (conn) {
if (!conn.db) throw new Error('missing db');
conn.db.removeAllListeners("close");
conn.db.removeAllListeners("error");
conn.db.removeAllListeners("timeout");
conn.db.removeAllListeners("open");
conn.db.removeAllListeners("fullsetup");
conn._listening = false;
});
conn.db.on('parseError', function(err) {
conn.emit('parseError', err);
});
}
/**
@ -114,30 +176,26 @@ function mute (conn) {
* @return {Connection} this
*/
NativeConnection.prototype.doOpenSet = function (fn) {
if (this.db) {
mute(this);
}
NativeConnection.prototype.doOpenSet = function(fn) {
var servers = [],
self = this;
var servers = []
, self = this;
this.hosts.forEach(function (server) {
this.hosts.forEach(function(server) {
var host = server.host || server.ipc;
var port = server.port || 27017;
servers.push(new Server(host, port, self.options.server));
})
});
var server = this.options.mongos
? new Mongos(servers, this.options.mongos)
: new ReplSetServers(servers, this.options.replset);
this.db = new Db(this.name, server, this.options.db);
this.db.on('fullsetup', function () {
self.emit('fullsetup')
this.db.on('fullsetup', function() {
self.emit('fullsetup');
});
this.db.open(function (err) {
this.db.open(function(err) {
if (err) return fn(err);
fn();
listen(self);
@ -154,11 +212,11 @@ NativeConnection.prototype.doOpenSet = function (fn) {
* @api private
*/
NativeConnection.prototype.doClose = function (fn) {
NativeConnection.prototype.doClose = function(fn) {
this.db.close();
if (fn) fn();
return this;
}
};
/**
* Prepares default connection options for the node-mongodb-native driver.
@ -170,7 +228,7 @@ NativeConnection.prototype.doClose = function (fn) {
* @api private
*/
NativeConnection.prototype.parseOptions = function (passed, connStrOpts) {
NativeConnection.prototype.parseOptions = function(passed, connStrOpts) {
var o = passed || {};
o.db || (o.db = {});
o.auth || (o.auth = {});
@ -180,11 +238,12 @@ NativeConnection.prototype.parseOptions = function (passed, connStrOpts) {
o.replset.socketOptions || (o.replset.socketOptions = {});
var opts = connStrOpts || {};
Object.keys(opts).forEach(function (name) {
Object.keys(opts).forEach(function(name) {
switch (name) {
case 'ssl':
case 'poolSize':
if ('undefined' == typeof o.server.poolSize) {
o.server.poolSize = o.replset.poolSize = opts[name];
if ('undefined' == typeof o.server[name]) {
o.server[name] = o.replset[name] = opts[name];
}
break;
case 'slaveOk':
@ -197,7 +256,6 @@ NativeConnection.prototype.parseOptions = function (passed, connStrOpts) {
o.server.auto_reconnect = opts[name];
}
break;
case 'ssl':
case 'socketTimeoutMS':
case 'connectTimeoutMS':
if ('undefined' == typeof o.server.socketOptions[name]) {
@ -256,7 +314,7 @@ NativeConnection.prototype.parseOptions = function (passed, connStrOpts) {
}
break;
}
})
});
if (!('auto_reconnect' in o.server)) {
o.server.auto_reconnect = true;
@ -278,7 +336,7 @@ NativeConnection.prototype.parseOptions = function (passed, connStrOpts) {
validate(o);
return o;
}
};
/*!
* Validates the driver db options.
@ -286,7 +344,7 @@ NativeConnection.prototype.parseOptions = function (passed, connStrOpts) {
* @param {Object} o
*/
function validate (o) {
function validate(o) {
if (-1 === o.db.w || 0 === o.db.w) {
if (o.db.journal || o.db.fsync || o.db.safe) {
throw new Error(