mirror of
https://github.com/atlanticbiomedical/biomedjs.git
synced 2025-07-02 00:47:26 -04:00
Updated mongoose
This commit is contained in:
209
node_modules/mongoose/lib/index.js
generated
vendored
209
node_modules/mongoose/lib/index.js
generated
vendored
@ -1,21 +1,25 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var Schema = require('./schema')
|
||||
, SchemaType = require('./schematype')
|
||||
, VirtualType = require('./virtualtype')
|
||||
, SchemaTypes = Schema.Types
|
||||
, SchemaDefaults = require('./schemadefault')
|
||||
, Types = require('./types')
|
||||
, Query = require('./query')
|
||||
, Promise = require('./promise')
|
||||
, Model = require('./model')
|
||||
, Document = require('./document')
|
||||
, utils = require('./utils')
|
||||
, format = utils.toCollectionName
|
||||
, mongodb = require('mongodb')
|
||||
var Schema = require('./schema'),
|
||||
SchemaType = require('./schematype'),
|
||||
VirtualType = require('./virtualtype'),
|
||||
STATES = require('./connectionstate'),
|
||||
Types = require('./types'),
|
||||
Query = require('./query'),
|
||||
Model = require('./model'),
|
||||
Document = require('./document'),
|
||||
utils = require('./utils'),
|
||||
format = utils.toCollectionName,
|
||||
pkg = require('../package.json');
|
||||
|
||||
var querystring = require('querystring');
|
||||
|
||||
var Aggregate = require('./aggregate');
|
||||
var PromiseProvider = require('./promise_provider');
|
||||
|
||||
/**
|
||||
* Mongoose constructor.
|
||||
@ -26,14 +30,24 @@ var Schema = require('./schema')
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function Mongoose () {
|
||||
function Mongoose() {
|
||||
this.connections = [];
|
||||
this.plugins = [];
|
||||
this.models = {};
|
||||
this.modelSchemas = {};
|
||||
this.options = {};
|
||||
this.createConnection(); // default connection
|
||||
};
|
||||
// default global options
|
||||
this.options = {
|
||||
pluralization: true
|
||||
};
|
||||
var conn = this.createConnection(); // default connection
|
||||
conn.models = this.models;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expose connection states for user-land
|
||||
*
|
||||
*/
|
||||
Mongoose.prototype.STATES = STATES;
|
||||
|
||||
/**
|
||||
* Sets mongoose options
|
||||
@ -49,9 +63,11 @@ function Mongoose () {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.set = function (key, value) {
|
||||
if (arguments.length == 1)
|
||||
Mongoose.prototype.set = function(key, value) {
|
||||
if (arguments.length == 1) {
|
||||
return this.options[key];
|
||||
}
|
||||
|
||||
this.options[key] = value;
|
||||
return this;
|
||||
};
|
||||
@ -76,12 +92,45 @@ Mongoose.prototype.get = Mongoose.prototype.set;
|
||||
|
||||
var rgxReplSet = /^.+,.+$/;
|
||||
|
||||
/**
|
||||
* Checks if ?replicaSet query parameter is specified in URI
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* checkReplicaSetInUri('localhost:27000?replicaSet=rs0'); // true
|
||||
*
|
||||
* @param {String} uri
|
||||
* @return {boolean}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
var checkReplicaSetInUri = function(uri) {
|
||||
if (!uri) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var queryStringStart = uri.indexOf('?');
|
||||
var isReplicaSet = false;
|
||||
if (queryStringStart !== -1) {
|
||||
try {
|
||||
var obj = querystring.parse(uri.substr(queryStringStart + 1));
|
||||
if (obj && obj.replicaSet) {
|
||||
isReplicaSet = true;
|
||||
}
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return isReplicaSet;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a Connection instance.
|
||||
*
|
||||
* Each `connection` instance maps to a single database. This method is helpful when mangaging multiple db connections.
|
||||
*
|
||||
* If arguments are passed, they are proxied to either [Connection#open](#connection_Connection-open) or [Connection#openSet](#connection_Connection-openSet) appropriately. This means we can pass `db`, `server`, and `replset` options to the driver.
|
||||
* If arguments are passed, they are proxied to either [Connection#open](#connection_Connection-open) or [Connection#openSet](#connection_Connection-openSet) appropriately. This means we can pass `db`, `server`, and `replset` options to the driver. _Note that the `safe` option specified in your schema will overwrite the `safe` db option specified here unless you set your schemas `safe` option to `undefined`. See [this](/docs/guide.html#safe) for more information._
|
||||
*
|
||||
* _Options passed take precedence over options included in connection strings._
|
||||
*
|
||||
@ -95,11 +144,11 @@ var rgxReplSet = /^.+,.+$/;
|
||||
* db = mongoose.createConnection('mongodb://user:pass@localhost:port/database', opts);
|
||||
*
|
||||
* // replica sets
|
||||
* db = mongoose.createConnection('mongodb://user:pass@localhost:port/database,mongodb://anotherhost:port,mongodb://yetanother:port');
|
||||
* db = mongoose.createConnection('mongodb://user:pass@localhost:port,anotherhost:port,yetanother:port/database');
|
||||
*
|
||||
* // and options
|
||||
* var opts = { replset: { strategy: 'ping', rs_name: 'testSet' }}
|
||||
* db = mongoose.createConnection('mongodb://user:pass@localhost:port/database,mongodb://anotherhost:port,mongodb://yetanother:port', opts);
|
||||
* db = mongoose.createConnection('mongodb://user:pass@localhost:port,anotherhost:port,yetanother:port/database', opts);
|
||||
*
|
||||
* // with [host, database_name[, port] signature
|
||||
* db = mongoose.createConnection('localhost', 'database', port)
|
||||
@ -120,12 +169,15 @@ var rgxReplSet = /^.+,.+$/;
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.createConnection = function () {
|
||||
Mongoose.prototype.createConnection = function(uri, options) {
|
||||
var conn = new Connection(this);
|
||||
this.connections.push(conn);
|
||||
|
||||
if (arguments.length) {
|
||||
if (rgxReplSet.test(arguments[0])) {
|
||||
if (rgxReplSet.test(arguments[0]) || checkReplicaSetInUri(arguments[0])) {
|
||||
conn.openSet.apply(conn, arguments);
|
||||
} else if (options && options.replset &&
|
||||
(options.replset.replicaSet || options.replset.rs_name)) {
|
||||
conn.openSet.apply(conn, arguments);
|
||||
} else {
|
||||
conn.open.apply(conn, arguments);
|
||||
@ -142,15 +194,34 @@ Mongoose.prototype.createConnection = function () {
|
||||
*
|
||||
* _Options passed take precedence over options included in connection strings._
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* mongoose.connect('mongodb://user:pass@localhost:port/database');
|
||||
*
|
||||
* // replica sets
|
||||
* var uri = 'mongodb://user:pass@localhost:port,anotherhost:port,yetanother:port/mydatabase';
|
||||
* mongoose.connect(uri);
|
||||
*
|
||||
* // with options
|
||||
* mongoose.connect(uri, options);
|
||||
*
|
||||
* // connecting to multiple mongos
|
||||
* var uri = 'mongodb://hostA:27501,hostB:27501';
|
||||
* var opts = { mongos: true };
|
||||
* mongoose.connect(uri, opts);
|
||||
*
|
||||
* @param {String} uri(s)
|
||||
* @param {Object} [options]
|
||||
* @param {Function} [callback]
|
||||
* @see Mongoose#createConnection #index_Mongoose-createConnection
|
||||
* @api public
|
||||
* @return {Mongoose} this
|
||||
*/
|
||||
|
||||
Mongoose.prototype.connect = function () {
|
||||
Mongoose.prototype.connect = function() {
|
||||
var conn = this.connection;
|
||||
|
||||
if (rgxReplSet.test(arguments[0])) {
|
||||
if (rgxReplSet.test(arguments[0]) || checkReplicaSetInUri(arguments[0])) {
|
||||
conn.openSet.apply(conn, arguments);
|
||||
} else {
|
||||
conn.open.apply(conn, arguments);
|
||||
@ -167,12 +238,12 @@ Mongoose.prototype.connect = function () {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.disconnect = function (fn) {
|
||||
var count = this.connections.length
|
||||
, error
|
||||
Mongoose.prototype.disconnect = function(fn) {
|
||||
var count = this.connections.length,
|
||||
error;
|
||||
|
||||
this.connections.forEach(function(conn){
|
||||
conn.close(function(err){
|
||||
this.connections.forEach(function(conn) {
|
||||
conn.close(function(err) {
|
||||
if (error) return;
|
||||
|
||||
if (err) {
|
||||
@ -228,7 +299,7 @@ Mongoose.prototype.disconnect = function (fn) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.model = function (name, schema, collection, skipInit) {
|
||||
Mongoose.prototype.model = function(name, schema, collection, skipInit) {
|
||||
if ('string' == typeof schema) {
|
||||
collection = schema;
|
||||
schema = false;
|
||||
@ -252,13 +323,8 @@ Mongoose.prototype.model = function (name, schema, collection, skipInit) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
// look up schema for the collection. this might be a
|
||||
// default schema like system.indexes stored in SchemaDefaults.
|
||||
// look up schema for the collection.
|
||||
if (!this.modelSchemas[name]) {
|
||||
if (!schema && name in SchemaDefaults) {
|
||||
schema = SchemaDefaults[name];
|
||||
}
|
||||
|
||||
if (schema) {
|
||||
// cache it so we only apply plugins once
|
||||
this.modelSchemas[name] = schema;
|
||||
@ -298,8 +364,12 @@ Mongoose.prototype.model = function (name, schema, collection, skipInit) {
|
||||
}
|
||||
}
|
||||
|
||||
// Apply relevant "global" options to the schema
|
||||
if (!('pluralization' in schema.options)) schema.options.pluralization = this.options.pluralization;
|
||||
|
||||
|
||||
if (!collection) {
|
||||
collection = schema.get('collection') || format(name);
|
||||
collection = schema.get('collection') || format(name, schema.options);
|
||||
}
|
||||
|
||||
var connection = options.connection || this.connection;
|
||||
@ -314,7 +384,7 @@ Mongoose.prototype.model = function (name, schema, collection, skipInit) {
|
||||
}
|
||||
|
||||
return this.models[name] = model;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns an array of model names created on this instance of Mongoose.
|
||||
@ -327,10 +397,10 @@ Mongoose.prototype.model = function (name, schema, collection, skipInit) {
|
||||
* @return {Array}
|
||||
*/
|
||||
|
||||
Mongoose.prototype.modelNames = function () {
|
||||
Mongoose.prototype.modelNames = function() {
|
||||
var names = Object.keys(this.models);
|
||||
return names;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Applies global plugins to `schema`.
|
||||
@ -339,11 +409,11 @@ Mongoose.prototype.modelNames = function () {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Mongoose.prototype._applyPlugins = function (schema) {
|
||||
Mongoose.prototype._applyPlugins = function(schema) {
|
||||
for (var i = 0, l = this.plugins.length; i < l; i++) {
|
||||
schema.plugin(this.plugins[i][0], this.plugins[i][1]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Declares a global plugin executed on all Schemas.
|
||||
@ -357,7 +427,7 @@ Mongoose.prototype._applyPlugins = function (schema) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.plugin = function (fn, opts) {
|
||||
Mongoose.prototype.plugin = function(fn, opts) {
|
||||
this.plugins.push([fn, opts]);
|
||||
return this;
|
||||
};
|
||||
@ -378,7 +448,7 @@ Mongoose.prototype.plugin = function (fn, opts) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.__defineGetter__('connection', function(){
|
||||
Mongoose.prototype.__defineGetter__('connection', function() {
|
||||
return this.connections[0];
|
||||
});
|
||||
|
||||
@ -400,6 +470,15 @@ var Connection = require(driver + '/connection');
|
||||
|
||||
var Collection = require(driver + '/collection');
|
||||
|
||||
/**
|
||||
* The Mongoose Aggregate constructor
|
||||
*
|
||||
* @method Aggregate
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.Aggregate = Aggregate;
|
||||
|
||||
/**
|
||||
* The Mongoose Collection constructor
|
||||
*
|
||||
@ -425,7 +504,7 @@ Mongoose.prototype.Connection = Connection;
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.version = require(__dirname + '/../package.json').version;
|
||||
Mongoose.prototype.version = pkg.version;
|
||||
|
||||
/**
|
||||
* The Mongoose constructor
|
||||
@ -533,7 +612,23 @@ Mongoose.prototype.Query = Query;
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.Promise = Promise;
|
||||
Object.defineProperty(Mongoose.prototype, 'Promise', {
|
||||
get: function() {
|
||||
return PromiseProvider.get();
|
||||
},
|
||||
set: function(lib) {
|
||||
PromiseProvider.set(lib);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Storage layer for mongoose promises
|
||||
*
|
||||
* @method PromiseProvider
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.PromiseProvider = PromiseProvider;
|
||||
|
||||
/**
|
||||
* The Mongoose [Model](#model_Model) constructor.
|
||||
@ -553,6 +648,15 @@ Mongoose.prototype.Model = Model;
|
||||
|
||||
Mongoose.prototype.Document = Document;
|
||||
|
||||
/**
|
||||
* The Mongoose DocumentProvider constructor.
|
||||
*
|
||||
* @method DocumentProvider
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.DocumentProvider = require('./document_provider');
|
||||
|
||||
/**
|
||||
* The [MongooseError](#error_MongooseError) constructor.
|
||||
*
|
||||
@ -571,6 +675,15 @@ Mongoose.prototype.Error = require('./error');
|
||||
|
||||
Mongoose.prototype.mongo = require('mongodb');
|
||||
|
||||
/**
|
||||
* The [mquery](https://github.com/aheckmann/mquery) query builder Mongoose uses.
|
||||
*
|
||||
* @property mquery
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mongoose.prototype.mquery = require('mquery');
|
||||
|
||||
/*!
|
||||
* The exports object is an instance of Mongoose.
|
||||
*
|
||||
|
Reference in New Issue
Block a user