mirror of
https://github.com/atlanticbiomedical/biomedjs.git
synced 2025-07-02 00:47:26 -04:00
Updated mongoose
This commit is contained in:
270
node_modules/mongoose/lib/connection.js
generated
vendored
270
node_modules/mongoose/lib/connection.js
generated
vendored
@ -2,17 +2,14 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var url = require('url')
|
||||
, utils = require('./utils')
|
||||
, EventEmitter = require('events').EventEmitter
|
||||
, driver = global.MONGOOSE_DRIVER_PATH || './drivers/node-mongodb-native'
|
||||
, Model = require('./model')
|
||||
, Schema = require('./schema')
|
||||
, Collection = require(driver + '/collection')
|
||||
, STATES = require('./connectionstate')
|
||||
, MongooseError = require('./error')
|
||||
, assert =require('assert')
|
||||
, muri = require('muri')
|
||||
var utils = require('./utils'),
|
||||
EventEmitter = require('events').EventEmitter,
|
||||
driver = global.MONGOOSE_DRIVER_PATH || './drivers/node-mongodb-native',
|
||||
Schema = require('./schema'),
|
||||
Collection = require(driver + '/collection'),
|
||||
STATES = require('./connectionstate'),
|
||||
MongooseError = require('./error'),
|
||||
muri = require('muri');
|
||||
|
||||
/*!
|
||||
* Protocol prefix regexp.
|
||||
@ -22,6 +19,16 @@ var url = require('url')
|
||||
|
||||
var rgxProtocol = /^(?:.)+:\/\//;
|
||||
|
||||
/*!
|
||||
* A list of authentication mechanisms that don't require a password for authentication.
|
||||
* This is used by the authMechanismDoesNotRequirePassword method.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
var authMechanismsWhichDontRequirePassword = [
|
||||
'MONGODB-X509'
|
||||
];
|
||||
|
||||
/**
|
||||
* Connection constructor
|
||||
*
|
||||
@ -41,10 +48,11 @@ var rgxProtocol = /^(?:.)+:\/\//;
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function Connection (base) {
|
||||
function Connection(base) {
|
||||
this.base = base;
|
||||
this.collections = {};
|
||||
this.models = {};
|
||||
this.config = {autoIndex: true};
|
||||
this.replica = false;
|
||||
this.hosts = null;
|
||||
this.host = null;
|
||||
@ -53,10 +61,11 @@ function Connection (base) {
|
||||
this.pass = null;
|
||||
this.name = null;
|
||||
this.options = null;
|
||||
this.otherDbs = [];
|
||||
this._readyState = STATES.disconnected;
|
||||
this._closeCalled = false;
|
||||
this._hasOpened = false;
|
||||
};
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherit from EventEmitter
|
||||
@ -84,21 +93,25 @@ Connection.prototype.__proto__ = EventEmitter.prototype;
|
||||
*/
|
||||
|
||||
Object.defineProperty(Connection.prototype, 'readyState', {
|
||||
get: function(){ return this._readyState; }
|
||||
, set: function (val) {
|
||||
if (!(val in STATES)) {
|
||||
throw new Error('Invalid connection state: ' + val);
|
||||
}
|
||||
|
||||
if (this._readyState !== val) {
|
||||
this._readyState = val;
|
||||
|
||||
if (STATES.connected === val)
|
||||
this._hasOpened = true;
|
||||
|
||||
this.emit(STATES[val]);
|
||||
}
|
||||
get: function() { return this._readyState; },
|
||||
set: function(val) {
|
||||
if (!(val in STATES)) {
|
||||
throw new Error('Invalid connection state: ' + val);
|
||||
}
|
||||
|
||||
if (this._readyState !== val) {
|
||||
this._readyState = val;
|
||||
// loop over the otherDbs on this connection and change their state
|
||||
for (var i = 0; i < this.otherDbs.length; i++) {
|
||||
this.otherDbs[i].readyState = val;
|
||||
}
|
||||
|
||||
if (STATES.connected === val)
|
||||
this._hasOpened = true;
|
||||
|
||||
this.emit(STATES[val]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
@ -117,11 +130,20 @@ Connection.prototype.collections;
|
||||
|
||||
Connection.prototype.db;
|
||||
|
||||
/**
|
||||
* A hash of the global options that are associated with this connection
|
||||
*
|
||||
* @property global
|
||||
*/
|
||||
|
||||
Connection.prototype.config;
|
||||
|
||||
/**
|
||||
* Opens the connection to MongoDB.
|
||||
*
|
||||
* `options` is a hash with the following possible properties:
|
||||
*
|
||||
* config - passed to the connection config instance
|
||||
* db - passed to the connection db instance
|
||||
* server - passed to the connection server instance(s)
|
||||
* replset - passed to the connection ReplSet instance
|
||||
@ -147,15 +169,14 @@ Connection.prototype.db;
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Connection.prototype.open = function (host, database, port, options, callback) {
|
||||
var self = this
|
||||
, parsed
|
||||
, uri;
|
||||
Connection.prototype.open = function(host, database, port, options, callback) {
|
||||
var parsed;
|
||||
|
||||
if ('string' === typeof database) {
|
||||
switch (arguments.length) {
|
||||
case 2:
|
||||
port = 27017;
|
||||
break;
|
||||
case 3:
|
||||
switch (typeof port) {
|
||||
case 'function':
|
||||
@ -219,7 +240,7 @@ Connection.prototype.open = function (host, database, port, options, callback) {
|
||||
}
|
||||
|
||||
// authentication
|
||||
if (options && options.user && options.pass) {
|
||||
if (this.optionsProvideAuthenticationData(options)) {
|
||||
this.user = options.user;
|
||||
this.pass = options.pass;
|
||||
|
||||
@ -239,6 +260,17 @@ Connection.prototype.open = function (host, database, port, options, callback) {
|
||||
this.user = this.pass = undefined;
|
||||
}
|
||||
|
||||
// global configuration options
|
||||
if (options && options.config) {
|
||||
if (options.config.autoIndex === false) {
|
||||
this.config.autoIndex = false;
|
||||
}
|
||||
else {
|
||||
this.config.autoIndex = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this.name = database;
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
@ -266,15 +298,22 @@ Connection.prototype.open = function (host, database, port, options, callback) {
|
||||
* user - username for authentication
|
||||
* pass - password for authentication
|
||||
* auth - options for authentication (see http://mongodb.github.com/node-mongodb-native/api-generated/db.html#authenticate)
|
||||
* mongos - Boolean - if true, enables High Availability support for mongos
|
||||
*
|
||||
* _Options passed take precedence over options included in connection strings._
|
||||
*
|
||||
* ####Notes:
|
||||
*
|
||||
* _If connecting to multiple mongos servers, set the `mongos` option to true._
|
||||
*
|
||||
* conn.open('mongodb://mongosA:27501,mongosB:27501', { mongos: true }, cb);
|
||||
*
|
||||
* Mongoose forces the db option `forceServerObjectId` false and cannot be overridden.
|
||||
* Mongoose defaults the server `auto_reconnect` options to true which can be overridden.
|
||||
* See the node-mongodb-native driver instance for options that it understands.
|
||||
*
|
||||
* _Options passed take precedence over options included in connection strings._
|
||||
*
|
||||
* @param {String} uris comma-separated mongodb:// `URI`s
|
||||
* @param {String} [database] database name if not included in `uris`
|
||||
* @param {Object} [options] passed to the internal driver
|
||||
@ -284,13 +323,11 @@ Connection.prototype.open = function (host, database, port, options, callback) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Connection.prototype.openSet = function (uris, database, options, callback) {
|
||||
Connection.prototype.openSet = function(uris, database, options, callback) {
|
||||
if (!rgxProtocol.test(uris)) {
|
||||
uris = 'mongodb://' + uris;
|
||||
}
|
||||
|
||||
var self = this;
|
||||
|
||||
switch (arguments.length) {
|
||||
case 3:
|
||||
switch (typeof database) {
|
||||
@ -345,7 +382,7 @@ Connection.prototype.openSet = function (uris, database, options, callback) {
|
||||
}
|
||||
|
||||
// authentication
|
||||
if (options && options.user && options.pass) {
|
||||
if (this.optionsProvideAuthenticationData(options)) {
|
||||
this.user = options.user;
|
||||
this.pass = options.pass;
|
||||
|
||||
@ -357,6 +394,17 @@ Connection.prototype.openSet = function (uris, database, options, callback) {
|
||||
this.user = this.pass = undefined;
|
||||
}
|
||||
|
||||
// global configuration options
|
||||
if (options && options.config) {
|
||||
if (options.config.autoIndex === false) {
|
||||
this.config.autoIndex = false;
|
||||
}
|
||||
else {
|
||||
this.config.autoIndex = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this._open(callback);
|
||||
return this;
|
||||
};
|
||||
@ -372,10 +420,10 @@ Connection.prototype.openSet = function (uris, database, options, callback) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Connection.prototype.error = function (err, callback) {
|
||||
Connection.prototype.error = function(err, callback) {
|
||||
if (callback) return callback(err);
|
||||
this.emit('error', err);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles opening the connection with the appropriate method based on connection type.
|
||||
@ -384,7 +432,7 @@ Connection.prototype.error = function (err, callback) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Connection.prototype._open = function (callback) {
|
||||
Connection.prototype._open = function(callback) {
|
||||
this.readyState = STATES.connecting;
|
||||
this._closeCalled = false;
|
||||
|
||||
@ -395,7 +443,7 @@ Connection.prototype._open = function (callback) {
|
||||
: 'doOpen';
|
||||
|
||||
// open connection
|
||||
this[method](function (err) {
|
||||
this[method](function(err) {
|
||||
if (err) {
|
||||
self.readyState = STATES.disconnected;
|
||||
if (self._hasOpened) {
|
||||
@ -408,7 +456,7 @@ Connection.prototype._open = function (callback) {
|
||||
|
||||
self.onOpen(callback);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Called when the connection is opened
|
||||
@ -416,12 +464,12 @@ Connection.prototype._open = function (callback) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Connection.prototype.onOpen = function (callback) {
|
||||
Connection.prototype.onOpen = function(callback) {
|
||||
var self = this;
|
||||
|
||||
function open (err) {
|
||||
function open(err, isAuth) {
|
||||
if (err) {
|
||||
self.readyState = STATES.disconnected;
|
||||
self.readyState = isAuth ? STATES.unauthorized : STATES.disconnected;
|
||||
if (self._hasOpened) {
|
||||
if (callback) callback(err);
|
||||
} else {
|
||||
@ -439,14 +487,16 @@ Connection.prototype.onOpen = function (callback) {
|
||||
|
||||
callback && callback();
|
||||
self.emit('open');
|
||||
};
|
||||
}
|
||||
|
||||
// re-authenticate
|
||||
if (self.user && self.pass) {
|
||||
self.db.authenticate(self.user, self.pass, self.options.auth, open);
|
||||
}
|
||||
else
|
||||
if (this.shouldAuthenticate()) {
|
||||
self.db.authenticate(self.user, self.pass, self.options.auth, function(err) {
|
||||
open(err, true);
|
||||
});
|
||||
} else {
|
||||
open();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@ -457,19 +507,20 @@ Connection.prototype.onOpen = function (callback) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Connection.prototype.close = function (callback) {
|
||||
Connection.prototype.close = function(callback) {
|
||||
var self = this;
|
||||
this._closeCalled = true;
|
||||
|
||||
switch (this.readyState){
|
||||
switch (this.readyState) {
|
||||
case 0: // disconnected
|
||||
callback && callback();
|
||||
break;
|
||||
|
||||
case 1: // connected
|
||||
case 4: // unauthorized
|
||||
this.readyState = STATES.disconnecting;
|
||||
this.doClose(function(err){
|
||||
if (err){
|
||||
this.doClose(function(err) {
|
||||
if (err) {
|
||||
self.error(err, callback);
|
||||
} else {
|
||||
self.onClose();
|
||||
@ -479,14 +530,14 @@ Connection.prototype.close = function (callback) {
|
||||
break;
|
||||
|
||||
case 2: // connecting
|
||||
this.once('open', function(){
|
||||
this.once('open', function() {
|
||||
self.close(callback);
|
||||
});
|
||||
break;
|
||||
|
||||
case 3: // disconnecting
|
||||
if (!callback) break;
|
||||
this.once('close', function () {
|
||||
this.once('close', function() {
|
||||
callback();
|
||||
});
|
||||
break;
|
||||
@ -501,7 +552,7 @@ Connection.prototype.close = function (callback) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Connection.prototype.onClose = function () {
|
||||
Connection.prototype.onClose = function() {
|
||||
this.readyState = STATES.disconnected;
|
||||
|
||||
// avoid having the collection subscribe to our event emitter
|
||||
@ -523,7 +574,7 @@ Connection.prototype.onClose = function () {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Connection.prototype.collection = function (name, options) {
|
||||
Connection.prototype.collection = function(name, options) {
|
||||
if (!(name in this.collections))
|
||||
this.collections[name] = new Collection(name, this, options);
|
||||
return this.collections[name];
|
||||
@ -561,7 +612,7 @@ Connection.prototype.collection = function (name, options) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Connection.prototype.model = function (name, schema, collection) {
|
||||
Connection.prototype.model = function(name, schema, collection) {
|
||||
// collection name discovery
|
||||
if ('string' == typeof schema) {
|
||||
collection = schema;
|
||||
@ -580,12 +631,12 @@ Connection.prototype.model = function (name, schema, collection) {
|
||||
return this.models[name];
|
||||
}
|
||||
|
||||
var opts = { cache: false, connection: this }
|
||||
var opts = { cache: false, connection: this };
|
||||
var model;
|
||||
|
||||
if (schema instanceof Schema) {
|
||||
// compile a model
|
||||
model = this.base.model(name, schema, collection, opts)
|
||||
model = this.base.model(name, schema, collection, opts);
|
||||
|
||||
// only the first model with this name is cached to allow
|
||||
// for one-offs with custom collection names etc.
|
||||
@ -627,7 +678,7 @@ Connection.prototype.model = function (name, schema, collection) {
|
||||
}
|
||||
|
||||
return this.models[name] = model.__subclass(this, schema, collection);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns an array of model names created on this connection.
|
||||
@ -635,68 +686,51 @@ Connection.prototype.model = function (name, schema, collection) {
|
||||
* @return {Array}
|
||||
*/
|
||||
|
||||
Connection.prototype.modelNames = function () {
|
||||
Connection.prototype.modelNames = function() {
|
||||
return Object.keys(this.models);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set profiling level.
|
||||
*
|
||||
* @param {Number|String} level either off (0), slow (1), or all (2)
|
||||
* @param {Number} [ms] the threshold in milliseconds above which queries will be logged when in `slow` mode. defaults to 100.
|
||||
* @param {Function} callback
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Connection.prototype.setProfiling = function (level, ms, callback) {
|
||||
if (STATES.connected !== this.readyState) {
|
||||
return this.on('open', this.setProfiling.bind(this, level, ms, callback));
|
||||
}
|
||||
|
||||
if (!callback) callback = ms, ms = 100;
|
||||
|
||||
var cmd = {};
|
||||
|
||||
switch (level) {
|
||||
case 0:
|
||||
case 'off':
|
||||
cmd.profile = 0;
|
||||
break;
|
||||
case 1:
|
||||
case 'slow':
|
||||
cmd.profile = 1;
|
||||
if ('number' !== typeof ms) {
|
||||
ms = parseInt(ms, 10);
|
||||
if (isNaN(ms)) ms = 100;
|
||||
}
|
||||
cmd.slowms = ms;
|
||||
break;
|
||||
case 2:
|
||||
case 'all':
|
||||
cmd.profile = 2;
|
||||
break;
|
||||
default:
|
||||
return callback(new Error('Invalid profiling level: '+ level));
|
||||
}
|
||||
|
||||
this.db.executeDbCommand(cmd, function (err, resp) {
|
||||
if (err) return callback(err);
|
||||
|
||||
var doc = resp.documents[0];
|
||||
|
||||
err = 1 === doc.ok
|
||||
? null
|
||||
: new Error('Could not set profiling level to: '+ level)
|
||||
|
||||
callback(err, doc);
|
||||
});
|
||||
};
|
||||
|
||||
/*!
|
||||
* Noop.
|
||||
/**
|
||||
* @brief Returns if the connection requires authentication after it is opened. Generally if a
|
||||
* username and password are both provided than authentication is needed, but in some cases a
|
||||
* password is not required.
|
||||
* @api private
|
||||
* @return {Boolean} true if the connection should be authenticated after it is opened, otherwise false.
|
||||
*/
|
||||
Connection.prototype.shouldAuthenticate = function() {
|
||||
return (this.user != null) &&
|
||||
((this.pass != null) || this.authMechanismDoesNotRequirePassword());
|
||||
};
|
||||
|
||||
function noop () {}
|
||||
/**
|
||||
* @brief Returns a boolean value that specifies if the current authentication mechanism needs a
|
||||
* password to authenticate according to the auth objects passed into the open/openSet methods.
|
||||
* @api private
|
||||
* @return {Boolean} true if the authentication mechanism specified in the options object requires
|
||||
* a password, otherwise false.
|
||||
*/
|
||||
Connection.prototype.authMechanismDoesNotRequirePassword = function() {
|
||||
if (this.options && this.options.auth) {
|
||||
return authMechanismsWhichDontRequirePassword.indexOf(this.options.auth.authMechanism) >= 0;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Returns a boolean value that specifies if the provided objects object provides enough
|
||||
* data to authenticate with. Generally this is true if the username and password are both specified
|
||||
* but in some authentication methods, a password is not required for authentication so only a username
|
||||
* is required.
|
||||
* @param {Object} [options] the options object passed into the open/openSet methods.
|
||||
* @api private
|
||||
* @return {Boolean} true if the provided options object provides enough data to authenticate with,
|
||||
* otherwise false.
|
||||
*/
|
||||
Connection.prototype.optionsProvideAuthenticationData = function(options) {
|
||||
return (options) &&
|
||||
(options.user) &&
|
||||
((options.pass) || this.authMechanismDoesNotRequirePassword());
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
|
Reference in New Issue
Block a user