mirror of
https://github.com/atlanticbiomedical/biomedjs.git
synced 2025-07-02 00:47:26 -04:00
Updated mongoose
This commit is contained in:
28
node_modules/mongoose/lib/ES6Promise.js
generated
vendored
Normal file
28
node_modules/mongoose/lib/ES6Promise.js
generated
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
/* eslint no-unused-vars: 1 */
|
||||
|
||||
/**
|
||||
* ES6 Promise wrapper constructor.
|
||||
*
|
||||
* Promises are returned from executed queries. Example:
|
||||
*
|
||||
* var query = Candy.find({ bar: true });
|
||||
* var promise = query.exec();
|
||||
*
|
||||
* DEPRECATED. Mongoose 5.0 will use native promises by default (or bluebird,
|
||||
* if native promises are not present) but still
|
||||
* support plugging in your own ES6-compatible promises library. Mongoose 5.0
|
||||
* will **not** support mpromise.
|
||||
*
|
||||
* @param {Function} fn a function which will be called when the promise is resolved that accepts `fn(err, ...){}` as signature
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function ES6Promise(fn) {
|
||||
throw 'Can\'t use ES6 promise with mpromise style constructor';
|
||||
}
|
||||
|
||||
ES6Promise.use = function(Promise) {
|
||||
ES6Promise.ES6 = Promise;
|
||||
};
|
||||
|
||||
module.exports = ES6Promise;
|
577
node_modules/mongoose/lib/aggregate.js
generated
vendored
Normal file
577
node_modules/mongoose/lib/aggregate.js
generated
vendored
Normal file
@ -0,0 +1,577 @@
|
||||
/* eslint no-unused-vars: 1 */
|
||||
|
||||
/*!
|
||||
* Module dependencies
|
||||
*/
|
||||
|
||||
var util = require('util');
|
||||
var utils = require('./utils');
|
||||
var PromiseProvider = require('./promise_provider');
|
||||
var Query = require('./query');
|
||||
var read = Query.prototype.read;
|
||||
|
||||
/**
|
||||
* Aggregate constructor used for building aggregation pipelines.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* new Aggregate();
|
||||
* new Aggregate({ $project: { a: 1, b: 1 } });
|
||||
* new Aggregate({ $project: { a: 1, b: 1 } }, { $skip: 5 });
|
||||
* new Aggregate([{ $project: { a: 1, b: 1 } }, { $skip: 5 }]);
|
||||
*
|
||||
* Returned when calling Model.aggregate().
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* Model
|
||||
* .aggregate({ $match: { age: { $gte: 21 }}})
|
||||
* .unwind('tags')
|
||||
* .exec(callback)
|
||||
*
|
||||
* ####Note:
|
||||
*
|
||||
* - The documents returned are plain javascript objects, not mongoose documents (since any shape of document can be returned).
|
||||
* - Requires MongoDB >= 2.1
|
||||
* - Mongoose does **not** cast pipeline stages. `new Aggregate({ $match: { _id: '00000000000000000000000a' } });` will not work unless `_id` is a string in the database. Use `new Aggregate({ $match: { _id: mongoose.Types.ObjectId('00000000000000000000000a') } });` instead.
|
||||
*
|
||||
* @see MongoDB http://docs.mongodb.org/manual/applications/aggregation/
|
||||
* @see driver http://mongodb.github.com/node-mongodb-native/api-generated/collection.html#aggregate
|
||||
* @param {Object|Array} [ops] aggregation operator(s) or operator array
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function Aggregate() {
|
||||
this._pipeline = [];
|
||||
this._model = undefined;
|
||||
this.options = undefined;
|
||||
|
||||
if (1 === arguments.length && util.isArray(arguments[0])) {
|
||||
this.append.apply(this, arguments[0]);
|
||||
} else {
|
||||
this.append.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Binds this aggregate to a model.
|
||||
*
|
||||
* @param {Model} model the model to which the aggregate is to be bound
|
||||
* @return {Aggregate}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Aggregate.prototype.model = function(model) {
|
||||
this._model = model;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Appends new operators to this aggregate pipeline
|
||||
*
|
||||
* ####Examples:
|
||||
*
|
||||
* aggregate.append({ $project: { field: 1 }}, { $limit: 2 });
|
||||
*
|
||||
* // or pass an array
|
||||
* var pipeline = [{ $match: { daw: 'Logic Audio X' }} ];
|
||||
* aggregate.append(pipeline);
|
||||
*
|
||||
* @param {Object} ops operator(s) to append
|
||||
* @return {Aggregate}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Aggregate.prototype.append = function() {
|
||||
var args = utils.args(arguments);
|
||||
|
||||
if (!args.every(isOperator)) {
|
||||
throw new Error("Arguments must be aggregate pipeline operators");
|
||||
}
|
||||
|
||||
this._pipeline = this._pipeline.concat(args);
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Appends a new $project operator to this aggregate pipeline.
|
||||
*
|
||||
* Mongoose query [selection syntax](#query_Query-select) is also supported.
|
||||
*
|
||||
* ####Examples:
|
||||
*
|
||||
* // include a, include b, exclude _id
|
||||
* aggregate.project("a b -_id");
|
||||
*
|
||||
* // or you may use object notation, useful when
|
||||
* // you have keys already prefixed with a "-"
|
||||
* aggregate.project({a: 1, b: 1, _id: 0});
|
||||
*
|
||||
* // reshaping documents
|
||||
* aggregate.project({
|
||||
* newField: '$b.nested'
|
||||
* , plusTen: { $add: ['$val', 10]}
|
||||
* , sub: {
|
||||
* name: '$a'
|
||||
* }
|
||||
* })
|
||||
*
|
||||
* // etc
|
||||
* aggregate.project({ salary_k: { $divide: [ "$salary", 1000 ] } });
|
||||
*
|
||||
* @param {Object|String} arg field specification
|
||||
* @see projection http://docs.mongodb.org/manual/reference/aggregation/project/
|
||||
* @return {Aggregate}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Aggregate.prototype.project = function(arg) {
|
||||
var fields = {};
|
||||
|
||||
if ('object' === typeof arg && !util.isArray(arg)) {
|
||||
Object.keys(arg).forEach(function(field) {
|
||||
fields[field] = arg[field];
|
||||
});
|
||||
} else if (1 === arguments.length && 'string' === typeof arg) {
|
||||
arg.split(/\s+/).forEach(function(field) {
|
||||
if (!field) return;
|
||||
var include = '-' == field[0] ? 0 : 1;
|
||||
if (include === 0) field = field.substring(1);
|
||||
fields[field] = include;
|
||||
});
|
||||
} else {
|
||||
throw new Error("Invalid project() argument. Must be string or object");
|
||||
}
|
||||
|
||||
return this.append({ $project: fields });
|
||||
};
|
||||
|
||||
/**
|
||||
* Appends a new custom $group operator to this aggregate pipeline.
|
||||
*
|
||||
* ####Examples:
|
||||
*
|
||||
* aggregate.group({ _id: "$department" });
|
||||
*
|
||||
* @see $group http://docs.mongodb.org/manual/reference/aggregation/group/
|
||||
* @method group
|
||||
* @memberOf Aggregate
|
||||
* @param {Object} arg $group operator contents
|
||||
* @return {Aggregate}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
/**
|
||||
* Appends a new custom $match operator to this aggregate pipeline.
|
||||
*
|
||||
* ####Examples:
|
||||
*
|
||||
* aggregate.match({ department: { $in: [ "sales", "engineering" } } });
|
||||
*
|
||||
* @see $match http://docs.mongodb.org/manual/reference/aggregation/match/
|
||||
* @method match
|
||||
* @memberOf Aggregate
|
||||
* @param {Object} arg $match operator contents
|
||||
* @return {Aggregate}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
/**
|
||||
* Appends a new $skip operator to this aggregate pipeline.
|
||||
*
|
||||
* ####Examples:
|
||||
*
|
||||
* aggregate.skip(10);
|
||||
*
|
||||
* @see $skip http://docs.mongodb.org/manual/reference/aggregation/skip/
|
||||
* @method skip
|
||||
* @memberOf Aggregate
|
||||
* @param {Number} num number of records to skip before next stage
|
||||
* @return {Aggregate}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
/**
|
||||
* Appends a new $limit operator to this aggregate pipeline.
|
||||
*
|
||||
* ####Examples:
|
||||
*
|
||||
* aggregate.limit(10);
|
||||
*
|
||||
* @see $limit http://docs.mongodb.org/manual/reference/aggregation/limit/
|
||||
* @method limit
|
||||
* @memberOf Aggregate
|
||||
* @param {Number} num maximum number of records to pass to the next stage
|
||||
* @return {Aggregate}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
/**
|
||||
* Appends a new $geoNear operator to this aggregate pipeline.
|
||||
*
|
||||
* ####NOTE:
|
||||
*
|
||||
* **MUST** be used as the first operator in the pipeline.
|
||||
*
|
||||
* ####Examples:
|
||||
*
|
||||
* aggregate.near({
|
||||
* near: [40.724, -73.997],
|
||||
* distanceField: "dist.calculated", // required
|
||||
* maxDistance: 0.008,
|
||||
* query: { type: "public" },
|
||||
* includeLocs: "dist.location",
|
||||
* uniqueDocs: true,
|
||||
* num: 5
|
||||
* });
|
||||
*
|
||||
* @see $geoNear http://docs.mongodb.org/manual/reference/aggregation/geoNear/
|
||||
* @method near
|
||||
* @memberOf Aggregate
|
||||
* @param {Object} parameters
|
||||
* @return {Aggregate}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Aggregate.prototype.near = function(arg) {
|
||||
var op = {};
|
||||
op.$geoNear = arg;
|
||||
return this.append(op);
|
||||
};
|
||||
|
||||
/*!
|
||||
* define methods
|
||||
*/
|
||||
|
||||
'group match skip limit out'.split(' ').forEach(function($operator) {
|
||||
Aggregate.prototype[$operator] = function(arg) {
|
||||
var op = {};
|
||||
op['$' + $operator] = arg;
|
||||
return this.append(op);
|
||||
};
|
||||
});
|
||||
|
||||
/**
|
||||
* Appends new custom $unwind operator(s) to this aggregate pipeline.
|
||||
*
|
||||
* Note that the `$unwind` operator requires the path name to start with '$'.
|
||||
* Mongoose will prepend '$' if the specified field doesn't start '$'.
|
||||
*
|
||||
* ####Examples:
|
||||
*
|
||||
* aggregate.unwind("tags");
|
||||
* aggregate.unwind("a", "b", "c");
|
||||
*
|
||||
* @see $unwind http://docs.mongodb.org/manual/reference/aggregation/unwind/
|
||||
* @param {String} fields the field(s) to unwind
|
||||
* @return {Aggregate}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Aggregate.prototype.unwind = function() {
|
||||
var args = utils.args(arguments);
|
||||
|
||||
return this.append.apply(this, args.map(function(arg) {
|
||||
return { $unwind: (arg && arg.charAt(0) === '$') ? arg : '$' + arg };
|
||||
}));
|
||||
};
|
||||
|
||||
/**
|
||||
* Appends a new $sort operator to this aggregate pipeline.
|
||||
*
|
||||
* If an object is passed, values allowed are `asc`, `desc`, `ascending`, `descending`, `1`, and `-1`.
|
||||
*
|
||||
* If a string is passed, it must be a space delimited list of path names. The sort order of each path is ascending unless the path name is prefixed with `-` which will be treated as descending.
|
||||
*
|
||||
* ####Examples:
|
||||
*
|
||||
* // these are equivalent
|
||||
* aggregate.sort({ field: 'asc', test: -1 });
|
||||
* aggregate.sort('field -test');
|
||||
*
|
||||
* @see $sort http://docs.mongodb.org/manual/reference/aggregation/sort/
|
||||
* @param {Object|String} arg
|
||||
* @return {Aggregate} this
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Aggregate.prototype.sort = function(arg) {
|
||||
// TODO refactor to reuse the query builder logic
|
||||
|
||||
var sort = {};
|
||||
|
||||
if ('Object' === arg.constructor.name) {
|
||||
var desc = ['desc', 'descending', -1];
|
||||
Object.keys(arg).forEach(function(field) {
|
||||
sort[field] = desc.indexOf(arg[field]) === -1 ? 1 : -1;
|
||||
});
|
||||
} else if (1 === arguments.length && 'string' == typeof arg) {
|
||||
arg.split(/\s+/).forEach(function(field) {
|
||||
if (!field) return;
|
||||
var ascend = '-' == field[0] ? -1 : 1;
|
||||
if (ascend === -1) field = field.substring(1);
|
||||
sort[field] = ascend;
|
||||
});
|
||||
} else {
|
||||
throw new TypeError('Invalid sort() argument. Must be a string or object.');
|
||||
}
|
||||
|
||||
return this.append({ $sort: sort });
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets the readPreference option for the aggregation query.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* Model.aggregate(..).read('primaryPreferred').exec(callback)
|
||||
*
|
||||
* @param {String} pref one of the listed preference options or their aliases
|
||||
* @param {Array} [tags] optional tags for this query
|
||||
* @see mongodb http://docs.mongodb.org/manual/applications/replication/#read-preference
|
||||
* @see driver http://mongodb.github.com/node-mongodb-native/driver-articles/anintroductionto1_1and2_2.html#read-preferences
|
||||
*/
|
||||
|
||||
Aggregate.prototype.read = function(pref) {
|
||||
if (!this.options) this.options = {};
|
||||
read.apply(this, arguments);
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Execute the aggregation with explain
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* Model.aggregate(..).explain(callback)
|
||||
*
|
||||
* @param {Function} callback
|
||||
* @return {Promise}
|
||||
*/
|
||||
|
||||
Aggregate.prototype.explain = function(callback) {
|
||||
var _this = this;
|
||||
var Promise = PromiseProvider.get();
|
||||
return new Promise.ES6(function(resolve, reject) {
|
||||
if (!_this._pipeline.length) {
|
||||
var err = new Error('Aggregate has empty pipeline');
|
||||
if (callback) {
|
||||
callback(err);
|
||||
}
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
prepareDiscriminatorPipeline(_this);
|
||||
|
||||
_this._model
|
||||
.collection
|
||||
.aggregate(_this._pipeline, _this.options || {})
|
||||
.explain(function(error, result) {
|
||||
if (error) {
|
||||
if (callback) {
|
||||
callback(error);
|
||||
}
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (callback) {
|
||||
callback(null, result);
|
||||
}
|
||||
resolve(result);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets the allowDiskUse option for the aggregation query (ignored for < 2.6.0)
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* Model.aggregate(..).allowDiskUse(true).exec(callback)
|
||||
*
|
||||
* @param {Boolean} value Should tell server it can use hard drive to store data during aggregation.
|
||||
* @param {Array} [tags] optional tags for this query
|
||||
* @see mongodb http://docs.mongodb.org/manual/reference/command/aggregate/
|
||||
*/
|
||||
|
||||
Aggregate.prototype.allowDiskUse = function(value) {
|
||||
if (!this.options) this.options = {};
|
||||
this.options.allowDiskUse = value;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets the cursor option option for the aggregation query (ignored for < 2.6.0).
|
||||
* Note the different syntax below: .exec() returns a cursor object, and no callback
|
||||
* is necessary.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var cursor = Model.aggregate(..).cursor({ batchSize: 1000 }).exec();
|
||||
* cursor.each(function(error, doc) {
|
||||
* // use doc
|
||||
* });
|
||||
*
|
||||
* @param {Object} options set the cursor batch size
|
||||
* @see mongodb http://mongodb.github.io/node-mongodb-native/2.0/api/AggregationCursor.html
|
||||
*/
|
||||
|
||||
Aggregate.prototype.cursor = function(options) {
|
||||
if (!this.options) this.options = {};
|
||||
this.options.cursor = options;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Executes the aggregate pipeline on the currently bound Model.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* aggregate.exec(callback);
|
||||
*
|
||||
* // Because a promise is returned, the `callback` is optional.
|
||||
* var promise = aggregate.exec();
|
||||
* promise.then(..);
|
||||
*
|
||||
* @see Promise #promise_Promise
|
||||
* @param {Function} [callback]
|
||||
* @return {Promise}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Aggregate.prototype.exec = function(callback) {
|
||||
if (!this._model) {
|
||||
throw new Error("Aggregate not bound to any Model");
|
||||
}
|
||||
var _this = this;
|
||||
var Promise = PromiseProvider.get();
|
||||
|
||||
if (this.options && this.options.cursor) {
|
||||
if (this.options.cursor.async) {
|
||||
return new Promise.ES6(function(resolve, reject) {
|
||||
if (!_this._model.collection.buffer) {
|
||||
process.nextTick(function() {
|
||||
var cursor = _this._model.collection.
|
||||
aggregate(_this._pipeline, _this.options || {});
|
||||
resolve(cursor);
|
||||
callback && callback(cursor);
|
||||
});
|
||||
return;
|
||||
} else {
|
||||
_this._model.collection.emitter.once('queue', function() {
|
||||
var cursor = _this._model.collection.
|
||||
aggregate(_this._pipeline, _this.options || {});
|
||||
resolve(cursor);
|
||||
callback && callback(null, cursor);
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return this._model.collection.
|
||||
aggregate(this._pipeline, this.options || {});
|
||||
}
|
||||
}
|
||||
|
||||
return new Promise.ES6(function(resolve, reject) {
|
||||
if (!_this._pipeline.length) {
|
||||
var err = new Error('Aggregate has empty pipeline');
|
||||
if (callback) {
|
||||
callback(err);
|
||||
}
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
prepareDiscriminatorPipeline(_this);
|
||||
|
||||
_this._model
|
||||
.collection
|
||||
.aggregate(_this._pipeline, _this.options || {}, function(error, result) {
|
||||
if (error) {
|
||||
if (callback) {
|
||||
callback(error);
|
||||
}
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (callback) {
|
||||
callback(null, result);
|
||||
}
|
||||
resolve(result);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/*!
|
||||
* Helpers
|
||||
*/
|
||||
|
||||
/**
|
||||
* Checks whether an object is likely a pipeline operator
|
||||
*
|
||||
* @param {Object} obj object to check
|
||||
* @return {Boolean}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function isOperator(obj) {
|
||||
var k;
|
||||
|
||||
if ('object' !== typeof obj) {
|
||||
return false;
|
||||
}
|
||||
|
||||
k = Object.keys(obj);
|
||||
|
||||
return 1 === k.length && k.some(function(key) {
|
||||
return '$' === key[0];
|
||||
});
|
||||
}
|
||||
|
||||
/*!
|
||||
* Adds the appropriate `$match` pipeline step to the top of an aggregate's
|
||||
* pipeline, should it's model is a non-root discriminator type. This is
|
||||
* analogous to the `prepareDiscriminatorCriteria` function in `lib/query.js`.
|
||||
*
|
||||
* @param {Aggregate} aggregate Aggregate to prepare
|
||||
*/
|
||||
|
||||
function prepareDiscriminatorPipeline(aggregate) {
|
||||
var schema = aggregate._model.schema,
|
||||
discriminatorMapping = schema && schema.discriminatorMapping;
|
||||
|
||||
if (discriminatorMapping && !discriminatorMapping.isRoot) {
|
||||
var originalPipeline = aggregate._pipeline,
|
||||
discriminatorKey = discriminatorMapping.key,
|
||||
discriminatorValue = discriminatorMapping.value;
|
||||
|
||||
// If the first pipeline stage is a match and it doesn't specify a `__t`
|
||||
// key, add the discriminator key to it. This allows for potential
|
||||
// aggregation query optimizations not to be disturbed by this feature.
|
||||
if (originalPipeline[0] && originalPipeline[0].$match &&
|
||||
!originalPipeline[0].$match[discriminatorKey]) {
|
||||
originalPipeline[0].$match[discriminatorKey] = discriminatorValue;
|
||||
// `originalPipeline` is a ref, so there's no need for
|
||||
// aggregate._pipeline = originalPipeline
|
||||
} else if (originalPipeline[0] && originalPipeline[0].$geoNear) {
|
||||
originalPipeline[0].$geoNear.query =
|
||||
originalPipeline[0].$geoNear.query || {};
|
||||
originalPipeline[0].$geoNear.query[discriminatorKey] = discriminatorValue;
|
||||
} else {
|
||||
var match = {};
|
||||
match[discriminatorKey] = discriminatorValue;
|
||||
aggregate._pipeline = [{ $match: match }].concat(originalPipeline);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
* Exports
|
||||
*/
|
||||
|
||||
module.exports = Aggregate;
|
97
node_modules/mongoose/lib/browser.js
generated
vendored
Normal file
97
node_modules/mongoose/lib/browser.js
generated
vendored
Normal file
@ -0,0 +1,97 @@
|
||||
/**
|
||||
* The [MongooseError](#error_MongooseError) constructor.
|
||||
*
|
||||
* @method Error
|
||||
* @api public
|
||||
*/
|
||||
|
||||
exports.Error = require('./error');
|
||||
|
||||
/**
|
||||
* The Mongoose [Schema](#schema_Schema) constructor
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var mongoose = require('mongoose');
|
||||
* var Schema = mongoose.Schema;
|
||||
* var CatSchema = new Schema(..);
|
||||
*
|
||||
* @method Schema
|
||||
* @api public
|
||||
*/
|
||||
|
||||
exports.Schema = require('./schema');
|
||||
|
||||
/**
|
||||
* The various Mongoose Types.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var mongoose = require('mongoose');
|
||||
* var array = mongoose.Types.Array;
|
||||
*
|
||||
* ####Types:
|
||||
*
|
||||
* - [ObjectId](#types-objectid-js)
|
||||
* - [Buffer](#types-buffer-js)
|
||||
* - [SubDocument](#types-embedded-js)
|
||||
* - [Array](#types-array-js)
|
||||
* - [DocumentArray](#types-documentarray-js)
|
||||
*
|
||||
* Using this exposed access to the `ObjectId` type, we can construct ids on demand.
|
||||
*
|
||||
* var ObjectId = mongoose.Types.ObjectId;
|
||||
* var id1 = new ObjectId;
|
||||
*
|
||||
* @property Types
|
||||
* @api public
|
||||
*/
|
||||
exports.Types = require('./types');
|
||||
|
||||
/**
|
||||
* The Mongoose [VirtualType](#virtualtype_VirtualType) constructor
|
||||
*
|
||||
* @method VirtualType
|
||||
* @api public
|
||||
*/
|
||||
exports.VirtualType = require('./virtualtype');
|
||||
|
||||
/**
|
||||
* The various Mongoose SchemaTypes.
|
||||
*
|
||||
* ####Note:
|
||||
*
|
||||
* _Alias of mongoose.Schema.Types for backwards compatibility._
|
||||
*
|
||||
* @property SchemaTypes
|
||||
* @see Schema.SchemaTypes #schema_Schema.Types
|
||||
* @api public
|
||||
*/
|
||||
|
||||
exports.SchemaType = require('./schematype.js');
|
||||
|
||||
/**
|
||||
* Internal utils
|
||||
*
|
||||
* @property utils
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.utils = require('./utils.js');
|
||||
|
||||
/**
|
||||
* The Mongoose browser [Document](#document-js) constructor.
|
||||
*
|
||||
* @method Document
|
||||
* @api public
|
||||
*/
|
||||
exports.Document = require('./document_provider.js')();
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
window.mongoose = module.exports;
|
||||
window.Buffer = Buffer;
|
||||
}
|
105
node_modules/mongoose/lib/browserDocument.js
generated
vendored
Normal file
105
node_modules/mongoose/lib/browserDocument.js
generated
vendored
Normal file
@ -0,0 +1,105 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var NodeJSDocument = require('./document'),
|
||||
EventEmitter = require('events').EventEmitter,
|
||||
MongooseError = require('./error'),
|
||||
Schema = require('./schema'),
|
||||
ObjectId = require('./types/objectid'),
|
||||
utils = require('./utils'),
|
||||
ValidationError = MongooseError.ValidationError,
|
||||
InternalCache = require('./internal');
|
||||
|
||||
/**
|
||||
* Document constructor.
|
||||
*
|
||||
* @param {Object} obj the values to set
|
||||
* @param {Object} [fields] optional object containing the fields which were selected in the query returning this document and any populated paths data
|
||||
* @param {Boolean} [skipId] bool, should we auto create an ObjectId _id
|
||||
* @inherits NodeJS EventEmitter http://nodejs.org/api/events.html#events_class_events_eventemitter
|
||||
* @event `init`: Emitted on a document after it has was retrieved from the db and fully hydrated by Mongoose.
|
||||
* @event `save`: Emitted when the document is successfully saved
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function Document(obj, schema, fields, skipId, skipInit) {
|
||||
if ( !(this instanceof Document) )
|
||||
return new Document( obj, schema, fields, skipId, skipInit );
|
||||
|
||||
|
||||
if (utils.isObject(schema) && !(schema instanceof Schema)) {
|
||||
schema = new Schema(schema);
|
||||
}
|
||||
|
||||
// When creating EmbeddedDocument, it already has the schema and he doesn't need the _id
|
||||
schema = this.schema || schema;
|
||||
|
||||
// Generate ObjectId if it is missing, but it requires a scheme
|
||||
if ( !this.schema && schema.options._id ) {
|
||||
obj = obj || {};
|
||||
|
||||
if ( obj._id === undefined ) {
|
||||
obj._id = new ObjectId();
|
||||
}
|
||||
}
|
||||
|
||||
if ( !schema ) {
|
||||
throw new MongooseError.MissingSchemaError();
|
||||
}
|
||||
|
||||
this.$__setSchema(schema);
|
||||
|
||||
this.$__ = new InternalCache;
|
||||
this.$__.emitter = new EventEmitter();
|
||||
this.isNew = true;
|
||||
this.errors = undefined;
|
||||
|
||||
//var schema = this.schema;
|
||||
|
||||
if ('boolean' === typeof fields) {
|
||||
this.$__.strictMode = fields;
|
||||
fields = undefined;
|
||||
} else {
|
||||
this.$__.strictMode = this.schema.options && this.schema.options.strict;
|
||||
this.$__.selected = fields;
|
||||
}
|
||||
|
||||
var required = this.schema.requiredPaths();
|
||||
for (var i = 0; i < required.length; ++i) {
|
||||
this.$__.activePaths.require(required[i]);
|
||||
}
|
||||
|
||||
this.$__.emitter.setMaxListeners(0);
|
||||
this._doc = this.$__buildDoc(obj, fields, skipId);
|
||||
|
||||
if ( !skipInit && obj ) {
|
||||
this.init( obj );
|
||||
}
|
||||
|
||||
this.$__registerHooksFromSchema();
|
||||
|
||||
// apply methods
|
||||
for ( var m in schema.methods ) {
|
||||
this[ m ] = schema.methods[ m ];
|
||||
}
|
||||
// apply statics
|
||||
for ( var s in schema.statics ) {
|
||||
this[ s ] = schema.statics[ s ];
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherit from the NodeJS document
|
||||
*/
|
||||
Document.prototype = Object.create(NodeJSDocument.prototype);
|
||||
Document.prototype.constructor = Document;
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
Document.ValidationError = ValidationError;
|
||||
module.exports = exports = Document;
|
211
node_modules/mongoose/lib/cast.js
generated
vendored
Normal file
211
node_modules/mongoose/lib/cast.js
generated
vendored
Normal file
@ -0,0 +1,211 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var utils = require('./utils');
|
||||
var Types = require('./schema/index');
|
||||
|
||||
/**
|
||||
* Handles internal casting for queries
|
||||
*
|
||||
* @param {Schema} schema
|
||||
* @param {Object obj Object to cast
|
||||
* @api private
|
||||
*/
|
||||
|
||||
var cast = module.exports = function(schema, obj) {
|
||||
var paths = Object.keys(obj),
|
||||
i = paths.length,
|
||||
any$conditionals,
|
||||
schematype,
|
||||
nested,
|
||||
path,
|
||||
type,
|
||||
val;
|
||||
|
||||
while (i--) {
|
||||
path = paths[i];
|
||||
val = obj[path];
|
||||
|
||||
if ('$or' === path || '$nor' === path || '$and' === path) {
|
||||
var k = val.length;
|
||||
|
||||
while (k--) {
|
||||
val[k] = cast(schema, val[k]);
|
||||
}
|
||||
|
||||
} else if (path === '$where') {
|
||||
type = typeof val;
|
||||
|
||||
if ('string' !== type && 'function' !== type) {
|
||||
throw new Error("Must have a string or function for $where");
|
||||
}
|
||||
|
||||
if ('function' === type) {
|
||||
obj[path] = val.toString();
|
||||
}
|
||||
|
||||
continue;
|
||||
|
||||
} else if (path === '$elemMatch') {
|
||||
|
||||
val = cast(schema, val);
|
||||
|
||||
} else {
|
||||
|
||||
if (!schema) {
|
||||
// no casting for Mixed types
|
||||
continue;
|
||||
}
|
||||
|
||||
schematype = schema.path(path);
|
||||
|
||||
if (!schematype) {
|
||||
// Handle potential embedded array queries
|
||||
var split = path.split('.'),
|
||||
j = split.length,
|
||||
pathFirstHalf,
|
||||
pathLastHalf,
|
||||
remainingConds;
|
||||
|
||||
// Find the part of the var path that is a path of the Schema
|
||||
while (j--) {
|
||||
pathFirstHalf = split.slice(0, j).join('.');
|
||||
schematype = schema.path(pathFirstHalf);
|
||||
if (schematype) break;
|
||||
}
|
||||
|
||||
// If a substring of the input path resolves to an actual real path...
|
||||
if (schematype) {
|
||||
// Apply the casting; similar code for $elemMatch in schema/array.js
|
||||
if (schematype.caster && schematype.caster.schema) {
|
||||
remainingConds = {};
|
||||
pathLastHalf = split.slice(j).join('.');
|
||||
remainingConds[pathLastHalf] = val;
|
||||
obj[path] = cast(schematype.caster.schema, remainingConds)[pathLastHalf];
|
||||
} else {
|
||||
obj[path] = val;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (utils.isObject(val)) {
|
||||
// handle geo schemas that use object notation
|
||||
// { loc: { long: Number, lat: Number }
|
||||
|
||||
var geo = val.$near ? '$near' :
|
||||
val.$nearSphere ? '$nearSphere' :
|
||||
val.$within ? '$within' :
|
||||
val.$geoIntersects ? '$geoIntersects' : '';
|
||||
|
||||
if (!geo) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var numbertype = new Types.Number('__QueryCasting__');
|
||||
var value = val[geo];
|
||||
|
||||
if (val.$maxDistance) {
|
||||
val.$maxDistance = numbertype.castForQuery(val.$maxDistance);
|
||||
}
|
||||
|
||||
if ('$within' == geo) {
|
||||
var withinType = value.$center
|
||||
|| value.$centerSphere
|
||||
|| value.$box
|
||||
|| value.$polygon;
|
||||
|
||||
if (!withinType) {
|
||||
throw new Error('Bad $within paramater: ' + JSON.stringify(val));
|
||||
}
|
||||
|
||||
value = withinType;
|
||||
|
||||
} else if ('$near' == geo &&
|
||||
'string' == typeof value.type && Array.isArray(value.coordinates)) {
|
||||
// geojson; cast the coordinates
|
||||
value = value.coordinates;
|
||||
|
||||
} else if (('$near' == geo || '$nearSphere' == geo || '$geoIntersects' == geo) &&
|
||||
value.$geometry && 'string' == typeof value.$geometry.type &&
|
||||
Array.isArray(value.$geometry.coordinates)) {
|
||||
// geojson; cast the coordinates
|
||||
value = value.$geometry.coordinates;
|
||||
}
|
||||
|
||||
(function _cast(val) {
|
||||
if (Array.isArray(val)) {
|
||||
val.forEach(function(item, i) {
|
||||
if (Array.isArray(item) || utils.isObject(item)) {
|
||||
return _cast(item);
|
||||
}
|
||||
val[i] = numbertype.castForQuery(item);
|
||||
});
|
||||
} else {
|
||||
var nearKeys = Object.keys(val);
|
||||
var nearLen = nearKeys.length;
|
||||
while (nearLen--) {
|
||||
var nkey = nearKeys[nearLen];
|
||||
var item = val[nkey];
|
||||
if (Array.isArray(item) || utils.isObject(item)) {
|
||||
_cast(item);
|
||||
val[nkey] = item;
|
||||
} else {
|
||||
val[nkey] = numbertype.castForQuery(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
})(value);
|
||||
}
|
||||
|
||||
} else if (val === null || val === undefined) {
|
||||
obj[path] = null;
|
||||
continue;
|
||||
} else if ('Object' === val.constructor.name) {
|
||||
|
||||
any$conditionals = Object.keys(val).some(function(k) {
|
||||
return k.charAt(0) === '$' && k !== '$id' && k !== '$ref';
|
||||
});
|
||||
|
||||
if (!any$conditionals) {
|
||||
obj[path] = schematype.castForQuery(val);
|
||||
} else {
|
||||
|
||||
var ks = Object.keys(val),
|
||||
$cond;
|
||||
|
||||
k = ks.length;
|
||||
|
||||
while (k--) {
|
||||
$cond = ks[k];
|
||||
nested = val[$cond];
|
||||
|
||||
if ('$exists' === $cond) {
|
||||
if ('boolean' !== typeof nested) {
|
||||
throw new Error("$exists parameter must be Boolean");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if ('$type' === $cond) {
|
||||
if ('number' !== typeof nested) {
|
||||
throw new Error("$type parameter must be Number");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if ('$not' === $cond) {
|
||||
cast(schema, nested);
|
||||
} else {
|
||||
val[$cond] = schematype.castForQuery($cond, nested);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
obj[path] = schematype.castForQuery(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return obj;
|
||||
};
|
50
node_modules/mongoose/lib/collection.js
generated
vendored
50
node_modules/mongoose/lib/collection.js
generated
vendored
@ -3,7 +3,8 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var STATES = require('./connectionstate')
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var STATES = require('./connectionstate');
|
||||
|
||||
/**
|
||||
* Abstract Collection constructor
|
||||
@ -16,7 +17,7 @@ var STATES = require('./connectionstate')
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function Collection (name, conn, opts) {
|
||||
function Collection(name, conn, opts) {
|
||||
if (undefined === opts) opts = {};
|
||||
if (undefined === opts.capped) opts.capped = {};
|
||||
|
||||
@ -30,14 +31,16 @@ function Collection (name, conn, opts) {
|
||||
|
||||
this.opts = opts;
|
||||
this.name = name;
|
||||
this.collectionName = name;
|
||||
this.conn = conn;
|
||||
this.queue = [];
|
||||
this.buffer = this.opts.bufferCommands;
|
||||
this.emitter = new EventEmitter();
|
||||
|
||||
if (STATES.connected == this.conn.readyState) {
|
||||
this.onOpen();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The collection name
|
||||
@ -48,6 +51,15 @@ function Collection (name, conn, opts) {
|
||||
|
||||
Collection.prototype.name;
|
||||
|
||||
/**
|
||||
* The collection name
|
||||
*
|
||||
* @api public
|
||||
* @property collectionName
|
||||
*/
|
||||
|
||||
Collection.prototype.collectionName;
|
||||
|
||||
/**
|
||||
* The Connection instance
|
||||
*
|
||||
@ -63,7 +75,7 @@ Collection.prototype.conn;
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Collection.prototype.onOpen = function () {
|
||||
Collection.prototype.onOpen = function() {
|
||||
var self = this;
|
||||
this.buffer = false;
|
||||
self.doQueue();
|
||||
@ -75,7 +87,7 @@ Collection.prototype.onOpen = function () {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Collection.prototype.onClose = function () {
|
||||
Collection.prototype.onClose = function() {
|
||||
if (this.opts.bufferCommands) {
|
||||
this.buffer = true;
|
||||
}
|
||||
@ -90,7 +102,7 @@ Collection.prototype.onClose = function () {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Collection.prototype.addQueue = function (name, args) {
|
||||
Collection.prototype.addQueue = function(name, args) {
|
||||
this.queue.push([name, args]);
|
||||
return this;
|
||||
};
|
||||
@ -101,11 +113,15 @@ Collection.prototype.addQueue = function (name, args) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Collection.prototype.doQueue = function () {
|
||||
for (var i = 0, l = this.queue.length; i < l; i++){
|
||||
Collection.prototype.doQueue = function() {
|
||||
for (var i = 0, l = this.queue.length; i < l; i++) {
|
||||
this[this.queue[i][0]].apply(this, this.queue[i][1]);
|
||||
}
|
||||
this.queue = [];
|
||||
var _this = this;
|
||||
process.nextTick(function() {
|
||||
_this.emitter.emit('queue');
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
@ -113,7 +129,7 @@ Collection.prototype.doQueue = function () {
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.ensureIndex = function(){
|
||||
Collection.prototype.ensureIndex = function() {
|
||||
throw new Error('Collection#ensureIndex unimplemented by driver');
|
||||
};
|
||||
|
||||
@ -121,7 +137,7 @@ Collection.prototype.ensureIndex = function(){
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.findAndModify = function(){
|
||||
Collection.prototype.findAndModify = function() {
|
||||
throw new Error('Collection#findAndModify unimplemented by driver');
|
||||
};
|
||||
|
||||
@ -129,7 +145,7 @@ Collection.prototype.findAndModify = function(){
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.findOne = function(){
|
||||
Collection.prototype.findOne = function() {
|
||||
throw new Error('Collection#findOne unimplemented by driver');
|
||||
};
|
||||
|
||||
@ -137,7 +153,7 @@ Collection.prototype.findOne = function(){
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.find = function(){
|
||||
Collection.prototype.find = function() {
|
||||
throw new Error('Collection#find unimplemented by driver');
|
||||
};
|
||||
|
||||
@ -145,7 +161,7 @@ Collection.prototype.find = function(){
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.insert = function(){
|
||||
Collection.prototype.insert = function() {
|
||||
throw new Error('Collection#insert unimplemented by driver');
|
||||
};
|
||||
|
||||
@ -153,7 +169,7 @@ Collection.prototype.insert = function(){
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.save = function(){
|
||||
Collection.prototype.save = function() {
|
||||
throw new Error('Collection#save unimplemented by driver');
|
||||
};
|
||||
|
||||
@ -161,7 +177,7 @@ Collection.prototype.save = function(){
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.update = function(){
|
||||
Collection.prototype.update = function() {
|
||||
throw new Error('Collection#update unimplemented by driver');
|
||||
};
|
||||
|
||||
@ -169,7 +185,7 @@ Collection.prototype.update = function(){
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.getIndexes = function(){
|
||||
Collection.prototype.getIndexes = function() {
|
||||
throw new Error('Collection#getIndexes unimplemented by driver');
|
||||
};
|
||||
|
||||
@ -177,7 +193,7 @@ Collection.prototype.getIndexes = function(){
|
||||
* Abstract method that drivers must implement.
|
||||
*/
|
||||
|
||||
Collection.prototype.mapReduce = function(){
|
||||
Collection.prototype.mapReduce = function() {
|
||||
throw new Error('Collection#mapReduce unimplemented by driver');
|
||||
};
|
||||
|
||||
|
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.
|
||||
|
3
node_modules/mongoose/lib/connectionstate.js
generated
vendored
3
node_modules/mongoose/lib/connectionstate.js
generated
vendored
@ -9,16 +9,19 @@ var disconnected = 'disconnected';
|
||||
var connected = 'connected';
|
||||
var connecting = 'connecting';
|
||||
var disconnecting = 'disconnecting';
|
||||
var unauthorized = 'unauthorized';
|
||||
var uninitialized = 'uninitialized';
|
||||
|
||||
STATES[0] = disconnected;
|
||||
STATES[1] = connected;
|
||||
STATES[2] = connecting;
|
||||
STATES[3] = disconnecting;
|
||||
STATES[4] = unauthorized;
|
||||
STATES[99] = uninitialized;
|
||||
|
||||
STATES[disconnected] = 0;
|
||||
STATES[connected] = 1;
|
||||
STATES[connecting] = 2;
|
||||
STATES[disconnecting] = 3;
|
||||
STATES[unauthorized] = 4;
|
||||
STATES[uninitialized] = 99;
|
||||
|
1558
node_modules/mongoose/lib/document.js
generated
vendored
1558
node_modules/mongoose/lib/document.js
generated
vendored
File diff suppressed because it is too large
Load Diff
20
node_modules/mongoose/lib/document_provider.js
generated
vendored
Normal file
20
node_modules/mongoose/lib/document_provider.js
generated
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
var Document = require('./document.js');
|
||||
var BrowserDocument = require('./browserDocument.js');
|
||||
|
||||
/**
|
||||
* Returns the Document constructor for the current context
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
module.exports = function() {
|
||||
if (typeof window !== 'undefined' && typeof document !== 'undefined' && document === window.document) {
|
||||
return BrowserDocument;
|
||||
} else {
|
||||
return Document;
|
||||
}
|
||||
};
|
5
node_modules/mongoose/lib/drivers/browser/ReadPreference.js
generated
vendored
Normal file
5
node_modules/mongoose/lib/drivers/browser/ReadPreference.js
generated
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = function() {};
|
12
node_modules/mongoose/lib/drivers/browser/binary.js
generated
vendored
Normal file
12
node_modules/mongoose/lib/drivers/browser/binary.js
generated
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var Binary = require('bson').Binary;
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = exports = Binary;
|
7
node_modules/mongoose/lib/drivers/browser/index.js
generated
vendored
Normal file
7
node_modules/mongoose/lib/drivers/browser/index.js
generated
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
exports.Binary = require('./binary');
|
||||
exports.ObjectId = require('./objectid');
|
||||
exports.ReadPreference = require('./ReadPreference');
|
14
node_modules/mongoose/lib/drivers/browser/objectid.js
generated
vendored
Normal file
14
node_modules/mongoose/lib/drivers/browser/objectid.js
generated
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
|
||||
/*!
|
||||
* [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) ObjectId
|
||||
* @constructor NodeMongoDbObjectId
|
||||
* @see ObjectId
|
||||
*/
|
||||
|
||||
var ObjectId = require('bson').ObjectID;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = exports = ObjectId;
|
17
node_modules/mongoose/lib/drivers/index.js
generated
vendored
Normal file
17
node_modules/mongoose/lib/drivers/index.js
generated
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
var driver;
|
||||
|
||||
if (typeof window === 'undefined') {
|
||||
driver = require(global.MONGOOSE_DRIVER_PATH || './node-mongodb-native');
|
||||
} else {
|
||||
driver = require('./browser');
|
||||
}
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = driver;
|
45
node_modules/mongoose/lib/drivers/node-mongodb-native/ReadPreference.js
generated
vendored
Normal file
45
node_modules/mongoose/lib/drivers/node-mongodb-native/ReadPreference.js
generated
vendored
Normal file
@ -0,0 +1,45 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var mongodb = require('mongodb');
|
||||
var ReadPref = mongodb.ReadPreference;
|
||||
|
||||
/*!
|
||||
* Converts arguments to ReadPrefs the driver
|
||||
* can understand.
|
||||
*
|
||||
* @param {String|Array} pref
|
||||
* @param {Array} [tags]
|
||||
*/
|
||||
|
||||
module.exports = function readPref(pref, tags) {
|
||||
if (Array.isArray(pref)) {
|
||||
tags = pref[1];
|
||||
pref = pref[0];
|
||||
}
|
||||
|
||||
if (pref instanceof ReadPref) {
|
||||
return pref;
|
||||
}
|
||||
|
||||
switch (pref) {
|
||||
case 'p':
|
||||
pref = 'primary';
|
||||
break;
|
||||
case 'pp':
|
||||
pref = 'primaryPreferred';
|
||||
break;
|
||||
case 's':
|
||||
pref = 'secondary';
|
||||
break;
|
||||
case 'sp':
|
||||
pref = 'secondaryPreferred';
|
||||
break;
|
||||
case 'n':
|
||||
pref = 'nearest';
|
||||
break;
|
||||
}
|
||||
|
||||
return new ReadPref(pref, tags);
|
||||
};
|
2
node_modules/mongoose/lib/drivers/node-mongodb-native/binary.js
generated
vendored
2
node_modules/mongoose/lib/drivers/node-mongodb-native/binary.js
generated
vendored
@ -3,6 +3,6 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var Binary = require('mongodb').BSONPure.Binary;
|
||||
var Binary = require('mongodb').Binary;
|
||||
|
||||
module.exports = exports = Binary;
|
||||
|
123
node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js
generated
vendored
123
node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js
generated
vendored
@ -3,10 +3,9 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var MongooseCollection = require('../../collection')
|
||||
, Collection = require('mongodb').Collection
|
||||
, STATES = require('../../connectionstate')
|
||||
, utils = require('../../utils')
|
||||
var MongooseCollection = require('../../collection'),
|
||||
Collection = require('mongodb').Collection,
|
||||
utils = require('../../utils');
|
||||
|
||||
/**
|
||||
* A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) collection implementation.
|
||||
@ -17,7 +16,7 @@ var MongooseCollection = require('../../collection')
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function NativeCollection () {
|
||||
function NativeCollection() {
|
||||
this.collection = null;
|
||||
MongooseCollection.apply(this, arguments);
|
||||
}
|
||||
@ -34,7 +33,7 @@ NativeCollection.prototype.__proto__ = MongooseCollection.prototype;
|
||||
* @api private
|
||||
*/
|
||||
|
||||
NativeCollection.prototype.onOpen = function () {
|
||||
NativeCollection.prototype.onOpen = function() {
|
||||
var self = this;
|
||||
|
||||
// always get a new collection in case the user changed host:port
|
||||
@ -46,21 +45,25 @@ NativeCollection.prototype.onOpen = function () {
|
||||
}
|
||||
|
||||
// capped
|
||||
return self.conn.db.collection(self.name, function (err, c) {
|
||||
return self.conn.db.collection(self.name, function(err, c) {
|
||||
if (err) return callback(err);
|
||||
|
||||
// discover if this collection exists and if it is capped
|
||||
c.options(function (err, exists) {
|
||||
if (err) return callback(err);
|
||||
self.conn.db.listCollections({ name: self.name }).toArray(function(err, docs) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
var doc = docs[0];
|
||||
var exists = !!doc;
|
||||
|
||||
if (exists) {
|
||||
if (exists.capped) {
|
||||
if (doc.options && doc.options.capped) {
|
||||
callback(null, c);
|
||||
} else {
|
||||
var msg = 'A non-capped collection exists with the name: '+ self.name +'\n\n'
|
||||
var msg = 'A non-capped collection exists with the name: ' + self.name + '\n\n'
|
||||
+ ' To use this collection as a capped collection, please '
|
||||
+ 'first convert it.\n'
|
||||
+ ' http://www.mongodb.org/display/DOCS/Capped+Collections#CappedCollections-Convertingacollectiontocapped'
|
||||
+ ' http://www.mongodb.org/display/DOCS/Capped+Collections#CappedCollections-Convertingacollectiontocapped';
|
||||
err = new Error(msg);
|
||||
callback(err);
|
||||
}
|
||||
@ -73,7 +76,7 @@ NativeCollection.prototype.onOpen = function () {
|
||||
});
|
||||
});
|
||||
|
||||
function callback (err, collection) {
|
||||
function callback(err, collection) {
|
||||
if (err) {
|
||||
// likely a strict mode error
|
||||
self.conn.emit('error', err);
|
||||
@ -81,7 +84,7 @@ NativeCollection.prototype.onOpen = function () {
|
||||
self.collection = collection;
|
||||
MongooseCollection.prototype.onOpen.call(self);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@ -90,7 +93,7 @@ NativeCollection.prototype.onOpen = function () {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
NativeCollection.prototype.onClose = function () {
|
||||
NativeCollection.prototype.onClose = function() {
|
||||
MongooseCollection.prototype.onClose.call(this);
|
||||
};
|
||||
|
||||
@ -99,68 +102,92 @@ NativeCollection.prototype.onClose = function () {
|
||||
*/
|
||||
|
||||
for (var i in Collection.prototype) {
|
||||
(function(i){
|
||||
NativeCollection.prototype[i] = function () {
|
||||
// Janky hack to work around gh-3005 until we can get rid of the mongoose
|
||||
// collection abstraction
|
||||
try {
|
||||
if (typeof Collection.prototype[i] !== 'function') {
|
||||
continue;
|
||||
}
|
||||
} catch (e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
(function(i) {
|
||||
NativeCollection.prototype[i] = function() {
|
||||
if (this.buffer) {
|
||||
this.addQueue(i, arguments);
|
||||
return;
|
||||
}
|
||||
|
||||
var collection = this.collection
|
||||
, args = arguments
|
||||
, self = this
|
||||
, debug = self.conn.base.options.debug;
|
||||
var collection = this.collection,
|
||||
args = arguments,
|
||||
self = this,
|
||||
debug = self.conn.base.options.debug;
|
||||
|
||||
if (debug) {
|
||||
if ('function' === typeof debug) {
|
||||
debug.apply(debug
|
||||
, [self.name, i].concat(utils.args(args, 0, args.length-1)));
|
||||
, [self.name, i].concat(utils.args(args, 0, args.length - 1)));
|
||||
} else {
|
||||
console.error('\x1B[0;36mMongoose:\x1B[0m %s.%s(%s) %s %s %s'
|
||||
, self.name
|
||||
, i
|
||||
, print(args[0])
|
||||
, print(args[1])
|
||||
, print(args[2])
|
||||
, print(args[3]))
|
||||
this.$print(self.name, i, args);
|
||||
}
|
||||
}
|
||||
|
||||
collection[i].apply(collection, args);
|
||||
return collection[i].apply(collection, args);
|
||||
};
|
||||
})(i);
|
||||
}
|
||||
|
||||
/*!
|
||||
/**
|
||||
* Debug print helper
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function print (arg) {
|
||||
NativeCollection.prototype.$print = function(name, i, args) {
|
||||
console.error(
|
||||
'\x1B[0;36mMongoose:\x1B[0m %s.%s(%s) %s %s %s',
|
||||
name,
|
||||
i,
|
||||
this.$format(args[0]),
|
||||
this.$format(args[1]),
|
||||
this.$format(args[2]),
|
||||
this.$format(args[3]));
|
||||
};
|
||||
|
||||
/**
|
||||
* Formatter for debug print args
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
NativeCollection.prototype.$format = function(arg) {
|
||||
var type = typeof arg;
|
||||
if ('function' === type || 'undefined' === type) return '';
|
||||
return format(arg);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Debug print helper
|
||||
*/
|
||||
|
||||
function format (obj, sub) {
|
||||
var x = utils.clone(obj);
|
||||
function format(obj, sub) {
|
||||
var x = utils.clone(obj, { retainKeyOrder: 1 });
|
||||
var representation;
|
||||
if (x) {
|
||||
if ('Binary' === x.constructor.name) {
|
||||
x = '[object Buffer]';
|
||||
} else if ('ObjectID' === x.constructor.name) {
|
||||
var representation = 'ObjectId("' + x.toHexString() + '")';
|
||||
representation = 'ObjectId("' + x.toHexString() + '")';
|
||||
x = { inspect: function() { return representation; } };
|
||||
} else if ('Date' === x.constructor.name) {
|
||||
var representation = 'new Date("' + x.toUTCString() + '")';
|
||||
representation = 'new Date("' + x.toUTCString() + '")';
|
||||
x = { inspect: function() { return representation; } };
|
||||
} else if ('Object' === x.constructor.name) {
|
||||
var keys = Object.keys(x)
|
||||
, i = keys.length
|
||||
, key
|
||||
while (i--) {
|
||||
var keys = Object.keys(x);
|
||||
var numKeys = keys.length;
|
||||
var key;
|
||||
for (var i = 0; i < numKeys; ++i) {
|
||||
key = keys[i];
|
||||
if (x[key]) {
|
||||
if ('Binary' === x[key].constructor.name) {
|
||||
@ -168,18 +195,18 @@ function format (obj, sub) {
|
||||
} else if ('Object' === x[key].constructor.name) {
|
||||
x[key] = format(x[key], true);
|
||||
} else if ('ObjectID' === x[key].constructor.name) {
|
||||
;(function(x){
|
||||
(function(x) {
|
||||
var representation = 'ObjectId("' + x[key].toHexString() + '")';
|
||||
x[key] = { inspect: function() { return representation; } };
|
||||
})(x)
|
||||
})(x);
|
||||
} else if ('Date' === x[key].constructor.name) {
|
||||
;(function(x){
|
||||
(function(x) {
|
||||
var representation = 'new Date("' + x[key].toUTCString() + '")';
|
||||
x[key] = { inspect: function() { return representation; } };
|
||||
})(x)
|
||||
})(x);
|
||||
} else if (Array.isArray(x[key])) {
|
||||
x[key] = x[key].map(function (o) {
|
||||
return format(o, true)
|
||||
x[key] = x[key].map(function(o) {
|
||||
return format(o, true);
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -191,7 +218,7 @@ function format (obj, sub) {
|
||||
return require('util')
|
||||
.inspect(x, false, 10, true)
|
||||
.replace(/\n/g, '')
|
||||
.replace(/\s{2,}/g, ' ')
|
||||
.replace(/\s{2,}/g, ' ');
|
||||
}
|
||||
|
||||
/**
|
||||
|
174
node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js
generated
vendored
174
node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js
generated
vendored
@ -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(
|
||||
|
7
node_modules/mongoose/lib/drivers/node-mongodb-native/index.js
generated
vendored
Normal file
7
node_modules/mongoose/lib/drivers/node-mongodb-native/index.js
generated
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
exports.Binary = require('./binary');
|
||||
exports.ObjectId = require('./objectid');
|
||||
exports.ReadPreference = require('./ReadPreference');
|
17
node_modules/mongoose/lib/drivers/node-mongodb-native/objectid.js
generated
vendored
17
node_modules/mongoose/lib/drivers/node-mongodb-native/objectid.js
generated
vendored
@ -5,25 +5,10 @@
|
||||
* @see ObjectId
|
||||
*/
|
||||
|
||||
var ObjectId = require('mongodb').BSONPure.ObjectID;
|
||||
var ObjectId = require('mongodb').ObjectId;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
var ObjectIdToString = ObjectId.toString.bind(ObjectId);
|
||||
module.exports = exports = ObjectId;
|
||||
|
||||
ObjectId.fromString = function(str){
|
||||
// patch native driver bug in V0.9.6.4
|
||||
if (!('string' === typeof str && 24 === str.length)) {
|
||||
throw new Error("Invalid ObjectId");
|
||||
}
|
||||
|
||||
return ObjectId.createFromHexString(str);
|
||||
};
|
||||
|
||||
ObjectId.toString = function(oid){
|
||||
if (!arguments.length) return ObjectIdToString();
|
||||
return oid.toHexString();
|
||||
};
|
||||
|
40
node_modules/mongoose/lib/error.js
generated
vendored
40
node_modules/mongoose/lib/error.js
generated
vendored
@ -6,18 +6,23 @@
|
||||
* @inherits Error https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error
|
||||
*/
|
||||
|
||||
function MongooseError (msg) {
|
||||
function MongooseError(msg) {
|
||||
Error.call(this);
|
||||
Error.captureStackTrace(this, arguments.callee);
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
} else {
|
||||
this.stack = new Error().stack
|
||||
}
|
||||
this.message = msg;
|
||||
this.name = 'MongooseError';
|
||||
};
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from Error.
|
||||
*/
|
||||
|
||||
MongooseError.prototype.__proto__ = Error.prototype;
|
||||
MongooseError.prototype = Object.create(Error.prototype);
|
||||
MongooseError.prototype.constructor = Error;
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
@ -25,15 +30,26 @@ MongooseError.prototype.__proto__ = Error.prototype;
|
||||
|
||||
module.exports = exports = MongooseError;
|
||||
|
||||
/**
|
||||
* The default built-in validator error messages.
|
||||
*
|
||||
* @see Error.messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
MongooseError.messages = require('./error/messages');
|
||||
|
||||
// backward compat
|
||||
MongooseError.Messages = MongooseError.messages;
|
||||
|
||||
/*!
|
||||
* Expose subclasses
|
||||
*/
|
||||
|
||||
MongooseError.CastError = require('./errors/cast');
|
||||
MongooseError.DocumentError = require('./errors/document');
|
||||
MongooseError.ValidationError = require('./errors/validation')
|
||||
MongooseError.ValidatorError = require('./errors/validator')
|
||||
MongooseError.VersionError =require('./errors/version')
|
||||
MongooseError.OverwriteModelError = require('./errors/overwriteModel')
|
||||
MongooseError.MissingSchemaError = require('./errors/missingSchema')
|
||||
MongooseError.DivergentArrayError = require('./errors/divergentArray')
|
||||
MongooseError.CastError = require('./error/cast');
|
||||
MongooseError.ValidationError = require('./error/validation');
|
||||
MongooseError.ValidatorError = require('./error/validator');
|
||||
MongooseError.VersionError = require('./error/version');
|
||||
MongooseError.OverwriteModelError = require('./error/overwriteModel');
|
||||
MongooseError.MissingSchemaError = require('./error/missingSchema');
|
||||
MongooseError.DivergentArrayError = require('./error/divergentArray');
|
||||
|
33
node_modules/mongoose/lib/error/browserMissingSchema.js
generated
vendored
Normal file
33
node_modules/mongoose/lib/error/browserMissingSchema.js
generated
vendored
Normal file
@ -0,0 +1,33 @@
|
||||
/* eslint no-unused-vars: 1 */
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var MongooseError = require('../error.js');
|
||||
|
||||
/*!
|
||||
* MissingSchema Error constructor.
|
||||
*
|
||||
* @inherits MongooseError
|
||||
*/
|
||||
|
||||
function MissingSchemaError(name) {
|
||||
var msg = 'Schema hasn\'t been registered for document.\n'
|
||||
+ 'Use mongoose.Document(name, schema)';
|
||||
MongooseError.call(this, msg);
|
||||
Error.captureStackTrace && Error.captureStackTrace(this, arguments.callee);
|
||||
this.name = 'MissingSchemaError';
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
MissingSchemaError.prototype = Object.create(MongooseError.prototype);
|
||||
MissingSchemaError.prototype.constructor = MongooseError;
|
||||
|
||||
/*!
|
||||
* exports
|
||||
*/
|
||||
|
||||
module.exports = MissingSchemaError;
|
19
node_modules/mongoose/lib/errors/cast.js → node_modules/mongoose/lib/error/cast.js
generated
vendored
19
node_modules/mongoose/lib/errors/cast.js → node_modules/mongoose/lib/error/cast.js
generated
vendored
@ -2,7 +2,7 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var MongooseError = require('../error');
|
||||
var MongooseError = require('../error.js');
|
||||
|
||||
/**
|
||||
* Casting Error constructor.
|
||||
@ -13,20 +13,27 @@ var MongooseError = require('../error');
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function CastError (type, value, path) {
|
||||
function CastError(type, value, path, reason) {
|
||||
MongooseError.call(this, 'Cast to ' + type + ' failed for value "' + value + '" at path "' + path + '"');
|
||||
Error.captureStackTrace(this, arguments.callee);
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
} else {
|
||||
this.stack = new Error().stack
|
||||
}
|
||||
this.name = 'CastError';
|
||||
this.type = type;
|
||||
this.kind = type;
|
||||
this.value = value;
|
||||
this.path = path;
|
||||
};
|
||||
this.reason = reason;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
CastError.prototype.__proto__ = MongooseError.prototype;
|
||||
CastError.prototype = Object.create(MongooseError.prototype);
|
||||
CastError.prototype.constructor = MongooseError;
|
||||
|
||||
|
||||
/*!
|
||||
* exports
|
@ -3,7 +3,7 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var MongooseError = require('../error');
|
||||
var MongooseError = require('../error.js');
|
||||
|
||||
/*!
|
||||
* DivergentArrayError constructor.
|
||||
@ -11,7 +11,7 @@ var MongooseError = require('../error');
|
||||
* @inherits MongooseError
|
||||
*/
|
||||
|
||||
function DivergentArrayError (paths) {
|
||||
function DivergentArrayError(paths) {
|
||||
var msg = 'For your own good, using `document.save()` to update an array '
|
||||
+ 'which was selected using an $elemMatch projection OR '
|
||||
+ 'populated using skip, limit, query conditions, or exclusion of '
|
||||
@ -19,19 +19,21 @@ function DivergentArrayError (paths) {
|
||||
+ 'the entire array is not supported. The following '
|
||||
+ 'path(s) would have been modified unsafely:\n'
|
||||
+ ' ' + paths.join('\n ') + '\n'
|
||||
+ 'Use Model.update() to update these arrays instead.'
|
||||
+ 'Use Model.update() to update these arrays instead.';
|
||||
// TODO write up a docs page (FAQ) and link to it
|
||||
|
||||
MongooseError.call(this, msg);
|
||||
Error.captureStackTrace(this, arguments.callee);
|
||||
Error.captureStackTrace && Error.captureStackTrace(this, arguments.callee);
|
||||
this.name = 'DivergentArrayError';
|
||||
};
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
DivergentArrayError.prototype.__proto__ = MongooseError.prototype;
|
||||
DivergentArrayError.prototype = Object.create(MongooseError.prototype);
|
||||
DivergentArrayError.prototype.constructor = MongooseError;
|
||||
|
||||
|
||||
/*!
|
||||
* exports
|
43
node_modules/mongoose/lib/error/messages.js
generated
vendored
Normal file
43
node_modules/mongoose/lib/error/messages.js
generated
vendored
Normal file
@ -0,0 +1,43 @@
|
||||
|
||||
/**
|
||||
* The default built-in validator error messages. These may be customized.
|
||||
*
|
||||
* // customize within each schema or globally like so
|
||||
* var mongoose = require('mongoose');
|
||||
* mongoose.Error.messages.String.enum = "Your custom message for {PATH}.";
|
||||
*
|
||||
* As you might have noticed, error messages support basic templating
|
||||
*
|
||||
* - `{PATH}` is replaced with the invalid document path
|
||||
* - `{VALUE}` is replaced with the invalid value
|
||||
* - `{TYPE}` is replaced with the validator type such as "regexp", "min", or "user defined"
|
||||
* - `{MIN}` is replaced with the declared min value for the Number.min validator
|
||||
* - `{MAX}` is replaced with the declared max value for the Number.max validator
|
||||
*
|
||||
* Click the "show code" link below to see all defaults.
|
||||
*
|
||||
* @property messages
|
||||
* @receiver MongooseError
|
||||
* @api public
|
||||
*/
|
||||
|
||||
var msg = module.exports = exports = {};
|
||||
|
||||
msg.general = {};
|
||||
msg.general.default = "Validator failed for path `{PATH}` with value `{VALUE}`";
|
||||
msg.general.required = "Path `{PATH}` is required.";
|
||||
|
||||
msg.Number = {};
|
||||
msg.Number.min = "Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).";
|
||||
msg.Number.max = "Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).";
|
||||
|
||||
msg.Date = {};
|
||||
msg.Date.min = "Path `{PATH}` ({VALUE}) is before minimum allowed value ({MIN}).";
|
||||
msg.Date.max = "Path `{PATH}` ({VALUE}) is after maximum allowed value ({MAX}).";
|
||||
|
||||
msg.String = {};
|
||||
msg.String.enum = "`{VALUE}` is not a valid enum value for path `{PATH}`.";
|
||||
msg.String.match = "Path `{PATH}` is invalid ({VALUE}).";
|
||||
msg.String.minlength = "Path `{PATH}` (`{VALUE}`) is shorter than the minimum allowed length ({MINLENGTH}).";
|
||||
msg.String.maxlength = "Path `{PATH}` (`{VALUE}`) is longer than the maximum allowed length ({MAXLENGTH}).";
|
||||
|
@ -3,7 +3,7 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var MongooseError = require('../error');
|
||||
var MongooseError = require('../error.js');
|
||||
|
||||
/*!
|
||||
* MissingSchema Error constructor.
|
||||
@ -11,19 +11,20 @@ var MongooseError = require('../error');
|
||||
* @inherits MongooseError
|
||||
*/
|
||||
|
||||
function MissingSchemaError (name) {
|
||||
function MissingSchemaError(name) {
|
||||
var msg = 'Schema hasn\'t been registered for model "' + name + '".\n'
|
||||
+ 'Use mongoose.model(name, schema)';
|
||||
MongooseError.call(this, msg);
|
||||
Error.captureStackTrace(this, arguments.callee);
|
||||
Error.captureStackTrace && Error.captureStackTrace(this, arguments.callee);
|
||||
this.name = 'MissingSchemaError';
|
||||
};
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
MissingSchemaError.prototype.__proto__ = MongooseError.prototype;
|
||||
MissingSchemaError.prototype = Object.create(MongooseError.prototype);
|
||||
MissingSchemaError.prototype.constructor = MongooseError;
|
||||
|
||||
/*!
|
||||
* exports
|
@ -3,7 +3,7 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var MongooseError = require('../error');
|
||||
var MongooseError = require('../error.js');
|
||||
|
||||
/*!
|
||||
* OverwriteModel Error constructor.
|
||||
@ -11,17 +11,18 @@ var MongooseError = require('../error');
|
||||
* @inherits MongooseError
|
||||
*/
|
||||
|
||||
function OverwriteModelError (name) {
|
||||
function OverwriteModelError(name) {
|
||||
MongooseError.call(this, 'Cannot overwrite `' + name + '` model once compiled.');
|
||||
Error.captureStackTrace(this, arguments.callee);
|
||||
Error.captureStackTrace && Error.captureStackTrace(this, arguments.callee);
|
||||
this.name = 'OverwriteModelError';
|
||||
};
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
OverwriteModelError.prototype.__proto__ = MongooseError.prototype;
|
||||
OverwriteModelError.prototype = Object.create(MongooseError.prototype);
|
||||
OverwriteModelError.prototype.constructor = MongooseError;
|
||||
|
||||
/*!
|
||||
* exports
|
62
node_modules/mongoose/lib/error/validation.js
generated
vendored
Normal file
62
node_modules/mongoose/lib/error/validation.js
generated
vendored
Normal file
@ -0,0 +1,62 @@
|
||||
|
||||
/*!
|
||||
* Module requirements
|
||||
*/
|
||||
|
||||
var MongooseError = require('../error.js');
|
||||
|
||||
/**
|
||||
* Document Validation Error
|
||||
*
|
||||
* @api private
|
||||
* @param {Document} instance
|
||||
* @inherits MongooseError
|
||||
*/
|
||||
|
||||
function ValidationError(instance) {
|
||||
if (instance && instance.constructor.name === 'model') {
|
||||
MongooseError.call(this, instance.constructor.modelName + " validation failed");
|
||||
} else {
|
||||
MongooseError.call(this, "Validation failed");
|
||||
}
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
} else {
|
||||
this.stack = new Error().stack
|
||||
}
|
||||
this.name = 'ValidationError';
|
||||
this.errors = {};
|
||||
if (instance) {
|
||||
instance.errors = this.errors;
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
ValidationError.prototype = Object.create(MongooseError.prototype);
|
||||
ValidationError.prototype.constructor = MongooseError;
|
||||
|
||||
|
||||
/**
|
||||
* Console.log helper
|
||||
*/
|
||||
|
||||
ValidationError.prototype.toString = function() {
|
||||
var ret = this.name + ': ';
|
||||
var msgs = [];
|
||||
|
||||
Object.keys(this.errors).forEach(function(key) {
|
||||
if (this == this.errors[key]) return;
|
||||
msgs.push(String(this.errors[key]));
|
||||
}, this);
|
||||
|
||||
return ret + msgs.join(', ');
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports
|
||||
*/
|
||||
|
||||
module.exports = exports = ValidationError;
|
71
node_modules/mongoose/lib/error/validator.js
generated
vendored
Normal file
71
node_modules/mongoose/lib/error/validator.js
generated
vendored
Normal file
@ -0,0 +1,71 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var MongooseError = require('../error.js');
|
||||
var errorMessages = MongooseError.messages;
|
||||
|
||||
/**
|
||||
* Schema validator error
|
||||
*
|
||||
* @param {Object} properties
|
||||
* @inherits MongooseError
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function ValidatorError(properties) {
|
||||
var msg = properties.message;
|
||||
if (!msg) {
|
||||
msg = errorMessages.general.default;
|
||||
}
|
||||
|
||||
this.properties = properties;
|
||||
var message = this.formatMessage(msg, properties);
|
||||
MongooseError.call(this, message);
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this);
|
||||
} else {
|
||||
this.stack = new Error().stack
|
||||
}
|
||||
this.name = 'ValidatorError';
|
||||
this.kind = properties.type;
|
||||
this.path = properties.path;
|
||||
this.value = properties.value;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError
|
||||
*/
|
||||
|
||||
ValidatorError.prototype = Object.create(MongooseError.prototype);
|
||||
ValidatorError.prototype.constructor = MongooseError;
|
||||
|
||||
/*!
|
||||
* Formats error messages
|
||||
*/
|
||||
|
||||
ValidatorError.prototype.formatMessage = function(msg, properties) {
|
||||
var propertyNames = Object.keys(properties);
|
||||
for (var i = 0; i < propertyNames.length; ++i) {
|
||||
var propertyName = propertyNames[i];
|
||||
if (propertyName === 'message') {
|
||||
continue;
|
||||
}
|
||||
msg = msg.replace('{' + propertyName.toUpperCase() + '}', properties[propertyName]);
|
||||
}
|
||||
return msg;
|
||||
};
|
||||
|
||||
/*!
|
||||
* toString helper
|
||||
*/
|
||||
|
||||
ValidatorError.prototype.toString = function() {
|
||||
return this.message;
|
||||
};
|
||||
|
||||
/*!
|
||||
* exports
|
||||
*/
|
||||
|
||||
module.exports = ValidatorError;
|
11
node_modules/mongoose/lib/errors/version.js → node_modules/mongoose/lib/error/version.js
generated
vendored
11
node_modules/mongoose/lib/errors/version.js → node_modules/mongoose/lib/error/version.js
generated
vendored
@ -3,7 +3,7 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var MongooseError = require('../error');
|
||||
var MongooseError = require('../error.js');
|
||||
|
||||
/**
|
||||
* Version Error constructor.
|
||||
@ -12,17 +12,18 @@ var MongooseError = require('../error');
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function VersionError () {
|
||||
function VersionError() {
|
||||
MongooseError.call(this, 'No matching document found.');
|
||||
Error.captureStackTrace(this, arguments.callee);
|
||||
Error.captureStackTrace && Error.captureStackTrace(this, arguments.callee);
|
||||
this.name = 'VersionError';
|
||||
};
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
VersionError.prototype.__proto__ = MongooseError.prototype;
|
||||
VersionError.prototype = Object.create(MongooseError.prototype);
|
||||
VersionError.prototype.constructor = MongooseError;
|
||||
|
||||
/*!
|
||||
* exports
|
32
node_modules/mongoose/lib/errors/document.js
generated
vendored
32
node_modules/mongoose/lib/errors/document.js
generated
vendored
@ -1,32 +0,0 @@
|
||||
|
||||
/*!
|
||||
* Module requirements
|
||||
*/
|
||||
|
||||
var MongooseError = require('../error')
|
||||
|
||||
/**
|
||||
* Document Error
|
||||
*
|
||||
* @param {String} msg
|
||||
* @inherits MongooseError
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function DocumentError (msg) {
|
||||
MongooseError.call(this, msg);
|
||||
Error.captureStackTrace(this, arguments.callee);
|
||||
this.name = 'DocumentError';
|
||||
};
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
DocumentError.prototype.__proto__ = MongooseError.prototype;
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = exports = DocumentError;
|
49
node_modules/mongoose/lib/errors/validation.js
generated
vendored
49
node_modules/mongoose/lib/errors/validation.js
generated
vendored
@ -1,49 +0,0 @@
|
||||
|
||||
/*!
|
||||
* Module requirements
|
||||
*/
|
||||
|
||||
var MongooseError = require('../error')
|
||||
|
||||
/**
|
||||
* Document Validation Error
|
||||
*
|
||||
* @api private
|
||||
* @param {Document} instance
|
||||
* @inherits MongooseError
|
||||
*/
|
||||
|
||||
function ValidationError (instance) {
|
||||
MongooseError.call(this, "Validation failed");
|
||||
Error.captureStackTrace(this, arguments.callee);
|
||||
this.name = 'ValidationError';
|
||||
this.errors = instance.errors = {};
|
||||
};
|
||||
|
||||
/**
|
||||
* Console.log helper
|
||||
*/
|
||||
|
||||
ValidationError.prototype.toString = function () {
|
||||
var ret = this.name + ': ';
|
||||
var msgs = [];
|
||||
|
||||
Object.keys(this.errors).forEach(function (key) {
|
||||
if (this == this.errors[key]) return;
|
||||
msgs.push(String(this.errors[key]));
|
||||
}, this)
|
||||
|
||||
return ret + msgs.join(', ');
|
||||
};
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError.
|
||||
*/
|
||||
|
||||
ValidationError.prototype.__proto__ = MongooseError.prototype;
|
||||
|
||||
/*!
|
||||
* Module exports
|
||||
*/
|
||||
|
||||
module.exports = exports = ValidationError;
|
51
node_modules/mongoose/lib/errors/validator.js
generated
vendored
51
node_modules/mongoose/lib/errors/validator.js
generated
vendored
@ -1,51 +0,0 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var MongooseError = require('../error');
|
||||
|
||||
/**
|
||||
* Schema validator error
|
||||
*
|
||||
* @param {String} path
|
||||
* @param {String} msg
|
||||
* @param {String|Number|any} val
|
||||
* @inherits MongooseError
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function ValidatorError (path, type, val) {
|
||||
var msg = type
|
||||
? '"' + type + '" '
|
||||
: '';
|
||||
|
||||
var message = 'Validator ' + msg + 'failed for path ' + path
|
||||
if (2 < arguments.length) message += ' with value `' + String(val) + '`';
|
||||
|
||||
MongooseError.call(this, message);
|
||||
Error.captureStackTrace(this, arguments.callee);
|
||||
this.name = 'ValidatorError';
|
||||
this.path = path;
|
||||
this.type = type;
|
||||
this.value = val;
|
||||
};
|
||||
|
||||
/*!
|
||||
* toString helper
|
||||
*/
|
||||
|
||||
ValidatorError.prototype.toString = function () {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseError
|
||||
*/
|
||||
|
||||
ValidatorError.prototype.__proto__ = MongooseError.prototype;
|
||||
|
||||
/*!
|
||||
* exports
|
||||
*/
|
||||
|
||||
module.exports = ValidatorError;
|
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.
|
||||
*
|
||||
|
6
node_modules/mongoose/lib/internal.js
generated
vendored
6
node_modules/mongoose/lib/internal.js
generated
vendored
@ -2,12 +2,12 @@
|
||||
* Dependencies
|
||||
*/
|
||||
|
||||
var StateMachine = require('./statemachine')
|
||||
var ActiveRoster = StateMachine.ctor('require', 'modify', 'init', 'default')
|
||||
var StateMachine = require('./statemachine');
|
||||
var ActiveRoster = StateMachine.ctor('require', 'modify', 'init', 'default', 'ignore');
|
||||
|
||||
module.exports = exports = InternalCache;
|
||||
|
||||
function InternalCache () {
|
||||
function InternalCache() {
|
||||
this.strictMode = undefined;
|
||||
this.selected = undefined;
|
||||
this.shardval = undefined;
|
||||
|
2066
node_modules/mongoose/lib/model.js
generated
vendored
2066
node_modules/mongoose/lib/model.js
generated
vendored
File diff suppressed because it is too large
Load Diff
70
node_modules/mongoose/lib/namedscope.js
generated
vendored
70
node_modules/mongoose/lib/namedscope.js
generated
vendored
@ -1,70 +0,0 @@
|
||||
var Query = require('./query');
|
||||
function NamedScope () {}
|
||||
|
||||
NamedScope.prototype.query;
|
||||
|
||||
NamedScope.prototype.where = function () {
|
||||
var q = this.query || (this.query = new Query());
|
||||
q.where.apply(q, arguments);
|
||||
return q;
|
||||
};
|
||||
|
||||
/**
|
||||
* Decorate
|
||||
*
|
||||
* @param {NamedScope} target
|
||||
* @param {Object} getters
|
||||
* @api private
|
||||
*/
|
||||
|
||||
NamedScope.prototype.decorate = function (target, getters) {
|
||||
var name = this.name
|
||||
, block = this.block
|
||||
, query = this.query;
|
||||
if (block) {
|
||||
if (block.length === 0) {
|
||||
Object.defineProperty(target, name, {
|
||||
get: getters.block0(block)
|
||||
});
|
||||
} else {
|
||||
target[name] = getters.blockN(block);
|
||||
}
|
||||
} else {
|
||||
Object.defineProperty(target, name, {
|
||||
get: getters.basic(query)
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
NamedScope.prototype.compile = function (model) {
|
||||
var allScopes = this.scopesByName
|
||||
, scope;
|
||||
for (var k in allScopes) {
|
||||
scope = allScopes[k];
|
||||
scope.decorate(model, {
|
||||
block0: function (block) {
|
||||
return function () {
|
||||
var cquery = this._cumulativeQuery || (this._cumulativeQuery = new Query().bind(this));
|
||||
block.call(cquery);
|
||||
return this;
|
||||
};
|
||||
},
|
||||
blockN: function (block) {
|
||||
return function () {
|
||||
var cquery = this._cumulativeQuery || (this._cumulativeQuery = new Query().bind(this));
|
||||
block.apply(cquery, arguments);
|
||||
return this;
|
||||
};
|
||||
},
|
||||
basic: function (query) {
|
||||
return function () {
|
||||
var cquery = this._cumulativeQuery || (this._cumulativeQuery = new Query().bind(this));
|
||||
cquery.find(query);
|
||||
return this;
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = NamedScope;
|
96
node_modules/mongoose/lib/promise.js
generated
vendored
96
node_modules/mongoose/lib/promise.js
generated
vendored
@ -1,9 +1,9 @@
|
||||
|
||||
/*!
|
||||
* Module dependencies
|
||||
*/
|
||||
|
||||
var MPromise = require('mpromise');
|
||||
var util = require('util');
|
||||
|
||||
/**
|
||||
* Promise constructor.
|
||||
@ -13,29 +13,57 @@ var MPromise = require('mpromise');
|
||||
* var query = Candy.find({ bar: true });
|
||||
* var promise = query.exec();
|
||||
*
|
||||
* DEPRECATED. Mongoose 5.0 will use native promises by default (or bluebird,
|
||||
* if native promises are not present) but still
|
||||
* support plugging in your own ES6-compatible promises library. Mongoose 5.0
|
||||
* will **not** support mpromise.
|
||||
*
|
||||
* @param {Function} fn a function which will be called when the promise is resolved that accepts `fn(err, ...){}` as signature
|
||||
* @inherits mpromise https://github.com/aheckmann/mpromise
|
||||
* @inherits NodeJS EventEmitter http://nodejs.org/api/events.html#events_class_events_eventemitter
|
||||
* @event `err`: Emits when the promise is rejected
|
||||
* @event `complete`: Emits when the promise is fulfilled
|
||||
* @api public
|
||||
* @deprecated
|
||||
*/
|
||||
|
||||
function Promise (fn) {
|
||||
function Promise(fn) {
|
||||
MPromise.call(this, fn);
|
||||
}
|
||||
|
||||
/**
|
||||
* ES6-style promise constructor wrapper around mpromise.
|
||||
*
|
||||
* @param {Function} resolver
|
||||
* @return {Promise} new promise
|
||||
* @api public
|
||||
*/
|
||||
Promise.ES6 = function(resolver) {
|
||||
var promise = new Promise();
|
||||
|
||||
// No try/catch for backwards compatibility
|
||||
resolver(
|
||||
function() {
|
||||
promise.complete.apply(promise, arguments);
|
||||
},
|
||||
function(e) {
|
||||
promise.error(e);
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Inherit from mpromise
|
||||
*/
|
||||
|
||||
Promise.prototype = Object.create(MPromise.prototype, {
|
||||
constructor: {
|
||||
value: Promise
|
||||
, enumerable: false
|
||||
, writable: true
|
||||
, configurable: true
|
||||
}
|
||||
constructor: {
|
||||
value: Promise,
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
|
||||
/*!
|
||||
@ -84,10 +112,15 @@ Promise.FAILURE = 'err';
|
||||
* @return {Promise} this
|
||||
*/
|
||||
|
||||
Promise.prototype.error = function (err) {
|
||||
if (!(err instanceof Error)) err = new Error(err);
|
||||
Promise.prototype.error = function(err) {
|
||||
if (!(err instanceof Error)) {
|
||||
if (err instanceof Object) {
|
||||
err = util.inspect(err);
|
||||
}
|
||||
err = new Error(err);
|
||||
}
|
||||
return this.reject(err);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Resolves this promise to a rejected state if `err` is passed or a fulfilled state if no `err` is passed.
|
||||
@ -101,17 +134,18 @@ Promise.prototype.error = function (err) {
|
||||
* @param {Error} [err] error or null
|
||||
* @param {Object} [val] value to fulfill the promise with
|
||||
* @api public
|
||||
* @deprecated
|
||||
*/
|
||||
|
||||
Promise.prototype.resolve = function (err, val) {
|
||||
Promise.prototype.resolve = function(err) {
|
||||
if (err) return this.error(err);
|
||||
return this.fulfill(val);
|
||||
}
|
||||
return this.fulfill.apply(this, Array.prototype.slice.call(arguments, 1));
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds a single function as a listener to both err and complete.
|
||||
*
|
||||
* It will be executed with traditional node.js argument position when the promise is resolved.
|
||||
* It will be executed with traditional node.js argument position when the promise is resolved.
|
||||
*
|
||||
* promise.addBack(function (err, args...) {
|
||||
* if (err) return handleError(err);
|
||||
@ -120,9 +154,12 @@ Promise.prototype.resolve = function (err, val) {
|
||||
*
|
||||
* Alias of [mpromise#onResolve](https://github.com/aheckmann/mpromise#onresolve).
|
||||
*
|
||||
* _Deprecated. Use `onResolve` instead._
|
||||
*
|
||||
* @method addBack
|
||||
* @param {Function} listener
|
||||
* @return {Promise} this
|
||||
* @deprecated
|
||||
*/
|
||||
|
||||
Promise.prototype.addBack = Promise.prototype.onResolve;
|
||||
@ -130,11 +167,26 @@ Promise.prototype.addBack = Promise.prototype.onResolve;
|
||||
/**
|
||||
* Fulfills this promise with passed arguments.
|
||||
*
|
||||
* Alias of [mpromise#fulfill](https://github.com/aheckmann/mpromise#fulfill).
|
||||
*
|
||||
* @method complete
|
||||
* @method fulfill
|
||||
* @receiver Promise
|
||||
* @see https://github.com/aheckmann/mpromise#fulfill
|
||||
* @param {any} args
|
||||
* @api public
|
||||
* @deprecated
|
||||
*/
|
||||
|
||||
/**
|
||||
* Fulfills this promise with passed arguments.
|
||||
*
|
||||
* Alias of [mpromise#fulfill](https://github.com/aheckmann/mpromise#fulfill).
|
||||
*
|
||||
* _Deprecated. Use `fulfill` instead._
|
||||
*
|
||||
* @method complete
|
||||
* @receiver Promise
|
||||
* @param {any} args
|
||||
* @api public
|
||||
* @deprecated
|
||||
*/
|
||||
|
||||
Promise.prototype.complete = MPromise.prototype.fulfill;
|
||||
@ -144,10 +196,13 @@ Promise.prototype.complete = MPromise.prototype.fulfill;
|
||||
*
|
||||
* Alias of [mpromise#onFulfill](https://github.com/aheckmann/mpromise#onfulfill).
|
||||
*
|
||||
* _Deprecated. Use `onFulfill` instead._
|
||||
*
|
||||
* @method addCallback
|
||||
* @param {Function} listener
|
||||
* @return {Promise} this
|
||||
* @api public
|
||||
* @deprecated
|
||||
*/
|
||||
|
||||
Promise.prototype.addCallback = Promise.prototype.onFulfill;
|
||||
@ -157,10 +212,13 @@ Promise.prototype.addCallback = Promise.prototype.onFulfill;
|
||||
*
|
||||
* Alias of [mpromise#onReject](https://github.com/aheckmann/mpromise#onreject).
|
||||
*
|
||||
* _Deprecated. Use `onReject` instead._
|
||||
*
|
||||
* @method addErrback
|
||||
* @param {Function} listener
|
||||
* @return {Promise} this
|
||||
* @api public
|
||||
* @deprecated
|
||||
*/
|
||||
|
||||
Promise.prototype.addErrback = Promise.prototype.onReject;
|
||||
@ -195,6 +253,7 @@ Promise.prototype.addErrback = Promise.prototype.onReject;
|
||||
* @param {Function} onFulFill
|
||||
* @param {Function} onReject
|
||||
* @return {Promise} newPromise
|
||||
* @deprecated
|
||||
*/
|
||||
|
||||
/**
|
||||
@ -222,6 +281,7 @@ Promise.prototype.addErrback = Promise.prototype.onReject;
|
||||
* @see mpromise#end https://github.com/aheckmann/mpromise#end
|
||||
* @method end
|
||||
* @memberOf Promise
|
||||
* @deprecated
|
||||
*/
|
||||
|
||||
/*!
|
||||
|
51
node_modules/mongoose/lib/promise_provider.js
generated
vendored
Normal file
51
node_modules/mongoose/lib/promise_provider.js
generated
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var MPromise = require('./promise');
|
||||
|
||||
/**
|
||||
* Helper for multiplexing promise implementations
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
var Promise = {
|
||||
_promise: MPromise
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the current promise constructor
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
Promise.get = function() {
|
||||
return Promise._promise;
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the current promise constructor
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Promise.set = function(lib) {
|
||||
if (lib === MPromise) {
|
||||
return Promise.reset();
|
||||
}
|
||||
Promise._promise = require('./ES6Promise');
|
||||
Promise._promise.use(lib);
|
||||
require('mquery').Promise = Promise._promise.ES6;
|
||||
};
|
||||
|
||||
/**
|
||||
* Resets to using mpromise
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Promise.reset = function() {
|
||||
Promise._promise = MPromise;
|
||||
};
|
||||
|
||||
module.exports = Promise;
|
3996
node_modules/mongoose/lib/query.js
generated
vendored
3996
node_modules/mongoose/lib/query.js
generated
vendored
File diff suppressed because it is too large
Load Diff
53
node_modules/mongoose/lib/queryhelpers.js
generated
vendored
53
node_modules/mongoose/lib/queryhelpers.js
generated
vendored
@ -3,7 +3,7 @@
|
||||
* Module dependencies
|
||||
*/
|
||||
|
||||
var utils = require('./utils')
|
||||
var utils = require('./utils');
|
||||
|
||||
/*!
|
||||
* Prepare a set of path options for query population.
|
||||
@ -13,14 +13,58 @@ var utils = require('./utils')
|
||||
* @return {Array}
|
||||
*/
|
||||
|
||||
exports.preparePopulationOptions = function preparePopulationOptions (query, options) {
|
||||
exports.preparePopulationOptions = function preparePopulationOptions(query, options) {
|
||||
var pop = utils.object.vals(query.options.populate);
|
||||
|
||||
// lean options should trickle through all queries
|
||||
if (options.lean) pop.forEach(makeLean);
|
||||
|
||||
return pop;
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Prepare a set of path options for query population. This is the MongooseQuery
|
||||
* version
|
||||
*
|
||||
* @param {Query} query
|
||||
* @param {Object} options
|
||||
* @return {Array}
|
||||
*/
|
||||
|
||||
exports.preparePopulationOptionsMQ = function preparePopulationOptionsMQ(query, options) {
|
||||
var pop = utils.object.vals(query._mongooseOptions.populate);
|
||||
|
||||
// lean options should trickle through all queries
|
||||
if (options.lean) pop.forEach(makeLean);
|
||||
|
||||
return pop;
|
||||
};
|
||||
|
||||
/*!
|
||||
* If the document is a mapped discriminator type, it returns a model instance for that type, otherwise,
|
||||
* it returns an instance of the given model.
|
||||
*
|
||||
* @param {Model} model
|
||||
* @param {Object} doc
|
||||
* @param {Object} fields
|
||||
*
|
||||
* @return {Model}
|
||||
*/
|
||||
exports.createModel = function createModel(model, doc, fields) {
|
||||
var discriminatorMapping = model.schema
|
||||
? model.schema.discriminatorMapping
|
||||
: null;
|
||||
|
||||
var key = discriminatorMapping && discriminatorMapping.isRoot
|
||||
? discriminatorMapping.key
|
||||
: null;
|
||||
|
||||
if (key && doc[key] && model.discriminators && model.discriminators[doc[key]]) {
|
||||
return new model.discriminators[doc[key]](undefined, fields, true);
|
||||
}
|
||||
|
||||
return new model(undefined, fields, true);
|
||||
};
|
||||
|
||||
/*!
|
||||
* Set each path query option to lean
|
||||
@ -28,8 +72,7 @@ exports.preparePopulationOptions = function preparePopulationOptions (query, opt
|
||||
* @param {Object} option
|
||||
*/
|
||||
|
||||
function makeLean (option) {
|
||||
function makeLean(option) {
|
||||
option.options || (option.options = {});
|
||||
option.options.lean = true;
|
||||
}
|
||||
|
||||
|
90
node_modules/mongoose/lib/querystream.js
generated
vendored
90
node_modules/mongoose/lib/querystream.js
generated
vendored
@ -1,12 +1,13 @@
|
||||
/* eslint no-empty: 1 */
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var Stream = require('stream').Stream
|
||||
var utils = require('./utils')
|
||||
var helpers = require('./queryhelpers')
|
||||
var K = function(k){ return k }
|
||||
var Stream = require('stream').Stream;
|
||||
var utils = require('./utils');
|
||||
var helpers = require('./queryhelpers');
|
||||
var K = function(k) { return k; };
|
||||
|
||||
/**
|
||||
* Provides a Node.js 0.8 style [ReadStream](http://nodejs.org/docs/v0.8.21/api/stream.html#stream_readable_stream) interface for Queries.
|
||||
@ -49,7 +50,7 @@ var K = function(k){ return k }
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function QueryStream (query, options) {
|
||||
function QueryStream(query, options) {
|
||||
Stream.call(this);
|
||||
|
||||
this.query = query;
|
||||
@ -67,7 +68,7 @@ function QueryStream (query, options) {
|
||||
|
||||
// give time to hook up events
|
||||
var self = this;
|
||||
process.nextTick(function () {
|
||||
process.nextTick(function() {
|
||||
self._init();
|
||||
});
|
||||
}
|
||||
@ -107,13 +108,13 @@ var T_CONT = 2;
|
||||
* @api private
|
||||
*/
|
||||
|
||||
QueryStream.prototype._init = function () {
|
||||
QueryStream.prototype._init = function() {
|
||||
if (this._destroyed) return;
|
||||
|
||||
var query = this.query
|
||||
, model = query.model
|
||||
, options = query._optionsForExec(model)
|
||||
, self = this
|
||||
var query = this.query,
|
||||
model = query.model,
|
||||
options = query._optionsForExec(model),
|
||||
self = this;
|
||||
|
||||
try {
|
||||
query.cast(model);
|
||||
@ -124,12 +125,12 @@ QueryStream.prototype._init = function () {
|
||||
self._fields = utils.clone(query._fields);
|
||||
options.fields = query._castFields(self._fields);
|
||||
|
||||
model.collection.find(query._conditions, options, function (err, cursor) {
|
||||
model.collection.find(query._conditions, options, function(err, cursor) {
|
||||
if (err) return self.destroy(err);
|
||||
self._cursor = cursor;
|
||||
self._next();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Trampoline for pulling the next doc from cursor.
|
||||
@ -138,7 +139,7 @@ QueryStream.prototype._init = function () {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
QueryStream.prototype._next = function _next () {
|
||||
QueryStream.prototype._next = function _next() {
|
||||
if (this.paused || this._destroyed) {
|
||||
return this._running = false;
|
||||
}
|
||||
@ -155,7 +156,7 @@ QueryStream.prototype._next = function _next () {
|
||||
// avoid stack overflows with large result sets.
|
||||
// trampoline instead of recursion.
|
||||
while (this.__next()) {}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Pulls the next doc from the cursor.
|
||||
@ -164,14 +165,14 @@ QueryStream.prototype._next = function _next () {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
QueryStream.prototype.__next = function () {
|
||||
QueryStream.prototype.__next = function() {
|
||||
if (this.paused || this._destroyed)
|
||||
return this._running = false;
|
||||
|
||||
var self = this;
|
||||
self._inline = T_INIT;
|
||||
|
||||
self._cursor.nextObject(function cursorcb (err, doc) {
|
||||
self._cursor.nextObject(function cursorcb(err, doc) {
|
||||
self._onNextObject(err, doc);
|
||||
});
|
||||
|
||||
@ -185,7 +186,7 @@ QueryStream.prototype.__next = function () {
|
||||
// the trampoline anymore.
|
||||
this._inline = T_IDLE;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Transforms raw `doc`s returned from the cursor into a model instance.
|
||||
@ -195,7 +196,7 @@ QueryStream.prototype.__next = function () {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
QueryStream.prototype._onNextObject = function _onNextObject (err, doc) {
|
||||
QueryStream.prototype._onNextObject = function _onNextObject(err, doc) {
|
||||
if (this._destroyed) return;
|
||||
|
||||
if (this.paused) {
|
||||
@ -212,28 +213,37 @@ QueryStream.prototype._onNextObject = function _onNextObject (err, doc) {
|
||||
return this.destroy();
|
||||
}
|
||||
|
||||
var opts = this.query.options;
|
||||
var opts = this.query._mongooseOptions;
|
||||
|
||||
if (!opts.populate) {
|
||||
return true === opts.lean
|
||||
? emit(this, doc)
|
||||
: createAndEmit(this, doc);
|
||||
return true === opts.lean ?
|
||||
emit(this, doc) :
|
||||
createAndEmit(this, null, doc);
|
||||
}
|
||||
|
||||
var self = this;
|
||||
var pop = helpers.preparePopulationOptions(self.query, self.query.options);
|
||||
var pop = helpers.preparePopulationOptionsMQ(self.query, self.query._mongooseOptions);
|
||||
|
||||
self.query.model.populate(doc, pop, function (err, doc) {
|
||||
// Hack to work around gh-3108
|
||||
pop.forEach(function(option) {
|
||||
delete option.model;
|
||||
});
|
||||
|
||||
self.query.model.populate(doc, pop, function(err, doc) {
|
||||
if (err) return self.destroy(err);
|
||||
return true === opts.lean
|
||||
? emit(self, doc)
|
||||
: createAndEmit(self, doc);
|
||||
})
|
||||
}
|
||||
return true === opts.lean ?
|
||||
emit(self, doc) :
|
||||
createAndEmit(self, pop, doc);
|
||||
});
|
||||
};
|
||||
|
||||
function createAndEmit (self, doc) {
|
||||
var instance = new self.query.model(undefined, self._fields, true);
|
||||
instance.init(doc, function (err) {
|
||||
function createAndEmit(self, populatedIds, doc) {
|
||||
var instance = helpers.createModel(self.query.model, doc, self._fields);
|
||||
var opts = populatedIds ?
|
||||
{ populated: populatedIds } :
|
||||
undefined;
|
||||
|
||||
instance.init(doc, opts, function(err) {
|
||||
if (err) return self.destroy(err);
|
||||
emit(self, instance);
|
||||
});
|
||||
@ -243,7 +253,7 @@ function createAndEmit (self, doc) {
|
||||
* Emit a data event and manage the trampoline state
|
||||
*/
|
||||
|
||||
function emit (self, doc) {
|
||||
function emit(self, doc) {
|
||||
self.emit('data', self._transform(doc));
|
||||
|
||||
// trampoline management
|
||||
@ -263,9 +273,9 @@ function emit (self, doc) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
QueryStream.prototype.pause = function () {
|
||||
QueryStream.prototype.pause = function() {
|
||||
this.paused = true;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Resumes this stream.
|
||||
@ -273,7 +283,7 @@ QueryStream.prototype.pause = function () {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
QueryStream.prototype.resume = function () {
|
||||
QueryStream.prototype.resume = function() {
|
||||
this.paused = false;
|
||||
|
||||
if (!this._cursor) {
|
||||
@ -290,7 +300,7 @@ QueryStream.prototype.resume = function () {
|
||||
// outside QueryStream control, need manual restart
|
||||
return this._next();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Destroys the stream, closing the underlying cursor. No more events will be emitted.
|
||||
@ -299,7 +309,7 @@ QueryStream.prototype.resume = function () {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
QueryStream.prototype.destroy = function (err) {
|
||||
QueryStream.prototype.destroy = function(err) {
|
||||
if (this._destroyed) return;
|
||||
this._destroyed = true;
|
||||
this._running = false;
|
||||
@ -314,7 +324,7 @@ QueryStream.prototype.destroy = function (err) {
|
||||
}
|
||||
|
||||
this.emit('close');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Pipes this query stream into another stream. This method is inherited from NodeJS Streams.
|
||||
|
693
node_modules/mongoose/lib/schema.js
generated
vendored
693
node_modules/mongoose/lib/schema.js
generated
vendored
File diff suppressed because it is too large
Load Diff
370
node_modules/mongoose/lib/schema/array.js
generated
vendored
370
node_modules/mongoose/lib/schema/array.js
generated
vendored
@ -2,23 +2,22 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var SchemaType = require('../schematype')
|
||||
, CastError = SchemaType.CastError
|
||||
, NumberSchema = require('./number')
|
||||
, Types = {
|
||||
Boolean: require('./boolean')
|
||||
, Date: require('./date')
|
||||
, Number: require('./number')
|
||||
, String: require('./string')
|
||||
, ObjectId: require('./objectid')
|
||||
, Buffer: require('./buffer')
|
||||
}
|
||||
, MongooseArray = require('../types').Array
|
||||
, EmbeddedDoc = require('../types').Embedded
|
||||
, Mixed = require('./mixed')
|
||||
, Query = require('../query')
|
||||
, utils = require('../utils')
|
||||
, isMongooseObject = utils.isMongooseObject
|
||||
var SchemaType = require('../schematype'),
|
||||
CastError = SchemaType.CastError,
|
||||
Types = {
|
||||
Boolean: require('./boolean'),
|
||||
Date: require('./date'),
|
||||
Number: require('./number'),
|
||||
String: require('./string'),
|
||||
ObjectId: require('./objectid'),
|
||||
Buffer: require('./buffer')
|
||||
},
|
||||
MongooseArray = require('../types').Array,
|
||||
EmbeddedDoc = require('../types').Embedded,
|
||||
Mixed = require('./mixed'),
|
||||
cast = require('../cast'),
|
||||
utils = require('../utils'),
|
||||
isMongooseObject = utils.isMongooseObject;
|
||||
|
||||
/**
|
||||
* Array SchemaType constructor
|
||||
@ -30,11 +29,11 @@ var SchemaType = require('../schematype')
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function SchemaArray (key, cast, options) {
|
||||
function SchemaArray(key, cast, options) {
|
||||
if (cast) {
|
||||
var castOptions = {};
|
||||
|
||||
if ('Object' === cast.constructor.name) {
|
||||
if ('Object' === utils.getFunctionName(cast.constructor)) {
|
||||
if (cast.type) {
|
||||
// support { type: Woot }
|
||||
castOptions = utils.clone(cast); // do not alter user arguments
|
||||
@ -48,7 +47,7 @@ function SchemaArray (key, cast, options) {
|
||||
// support { type: 'String' }
|
||||
var name = 'string' == typeof cast
|
||||
? cast
|
||||
: cast.name;
|
||||
: utils.getFunctionName(cast);
|
||||
|
||||
var caster = name in Types
|
||||
? Types[name]
|
||||
@ -61,28 +60,36 @@ function SchemaArray (key, cast, options) {
|
||||
}
|
||||
}
|
||||
|
||||
SchemaType.call(this, key, options);
|
||||
SchemaType.call(this, key, options, 'Array');
|
||||
|
||||
var self = this
|
||||
, defaultArr
|
||||
, fn;
|
||||
var self = this,
|
||||
defaultArr,
|
||||
fn;
|
||||
|
||||
if (this.defaultValue) {
|
||||
defaultArr = this.defaultValue;
|
||||
fn = 'function' == typeof defaultArr;
|
||||
}
|
||||
|
||||
this.default(function(){
|
||||
this.default(function() {
|
||||
var arr = fn ? defaultArr() : defaultArr || [];
|
||||
return new MongooseArray(arr, self.path, this);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
SchemaArray.schemaName = 'Array';
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
|
||||
SchemaArray.prototype.__proto__ = SchemaType.prototype;
|
||||
SchemaArray.prototype = Object.create( SchemaType.prototype );
|
||||
SchemaArray.prototype.constructor = SchemaArray;
|
||||
|
||||
/**
|
||||
* Check required
|
||||
@ -91,7 +98,7 @@ SchemaArray.prototype.__proto__ = SchemaType.prototype;
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaArray.prototype.checkRequired = function (value) {
|
||||
SchemaArray.prototype.checkRequired = function(value) {
|
||||
return !!(value && value.length);
|
||||
};
|
||||
|
||||
@ -103,7 +110,7 @@ SchemaArray.prototype.checkRequired = function (value) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaArray.prototype.applyGetters = function (value, scope) {
|
||||
SchemaArray.prototype.applyGetters = function(value, scope) {
|
||||
if (this.caster.options && this.caster.options.ref) {
|
||||
// means the object id was populated
|
||||
return value;
|
||||
@ -113,7 +120,7 @@ SchemaArray.prototype.applyGetters = function (value, scope) {
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts contents
|
||||
* Casts values for set().
|
||||
*
|
||||
* @param {Object} value
|
||||
* @param {Document} doc document that triggers the casting
|
||||
@ -121,15 +128,27 @@ SchemaArray.prototype.applyGetters = function (value, scope) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaArray.prototype.cast = function (value, doc, init) {
|
||||
SchemaArray.prototype.cast = function(value, doc, init) {
|
||||
if (Array.isArray(value)) {
|
||||
if (!(value instanceof MongooseArray)) {
|
||||
|
||||
if (!value.length && doc) {
|
||||
var indexes = doc.schema.indexedPaths();
|
||||
|
||||
for (var i = 0, l = indexes.length; i < l; ++i) {
|
||||
var pathIndex = indexes[i][0][this.path];
|
||||
if ('2dsphere' === pathIndex || '2d' === pathIndex) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!(value && value.isMongooseArray)) {
|
||||
value = new MongooseArray(value, this.path, doc);
|
||||
}
|
||||
|
||||
if (this.caster) {
|
||||
try {
|
||||
for (var i = 0, l = value.length; i < l; i++) {
|
||||
for (i = 0, l = value.length; i < l; i++) {
|
||||
value[i] = this.caster.cast(value[i], doc, init);
|
||||
}
|
||||
} catch (e) {
|
||||
@ -140,175 +159,234 @@ SchemaArray.prototype.cast = function (value, doc, init) {
|
||||
|
||||
return value;
|
||||
} else {
|
||||
// gh-2442: if we're loading this from the db and its not an array, mark
|
||||
// the whole array as modified.
|
||||
if (!!doc && !!init) {
|
||||
doc.markModified(this.path);
|
||||
}
|
||||
return this.cast([value], doc, init);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts contents for queries.
|
||||
* Casts values for queries.
|
||||
*
|
||||
* @param {String} $conditional
|
||||
* @param {any} [value]
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaArray.prototype.castForQuery = function ($conditional, value) {
|
||||
var handler
|
||||
, val;
|
||||
SchemaArray.prototype.castForQuery = function($conditional, value) {
|
||||
var handler,
|
||||
val;
|
||||
|
||||
if (arguments.length === 2) {
|
||||
handler = this.$conditionalHandlers[$conditional];
|
||||
if (!handler)
|
||||
|
||||
if (!handler) {
|
||||
throw new Error("Can't use " + $conditional + " with Array.");
|
||||
}
|
||||
|
||||
val = handler.call(this, value);
|
||||
|
||||
} else {
|
||||
|
||||
val = $conditional;
|
||||
var proto = this.casterConstructor.prototype;
|
||||
var method = proto.castForQuery || proto.cast;
|
||||
|
||||
var caster = this.caster;
|
||||
if (Array.isArray(val)) {
|
||||
val = val.map(function (v) {
|
||||
if (method) v = method.call(caster, v);
|
||||
|
||||
return isMongooseObject(v)
|
||||
? v.toObject()
|
||||
: v;
|
||||
if (Array.isArray(val)) {
|
||||
val = val.map(function(v) {
|
||||
if (utils.isObject(v) && v.$elemMatch) {
|
||||
return v;
|
||||
}
|
||||
if (method) v = method.call(caster, v);
|
||||
return isMongooseObject(v) ?
|
||||
v.toObject({ virtuals: false }) :
|
||||
v;
|
||||
});
|
||||
|
||||
} else if (method) {
|
||||
val = method.call(caster, val);
|
||||
}
|
||||
}
|
||||
return val && isMongooseObject(val)
|
||||
? val.toObject()
|
||||
: val;
|
||||
|
||||
return val && isMongooseObject(val) ?
|
||||
val.toObject({ virtuals: false }) :
|
||||
val;
|
||||
};
|
||||
|
||||
/*!
|
||||
* @ignore
|
||||
*
|
||||
* $atomic cast helpers
|
||||
*/
|
||||
|
||||
function castToNumber (val) {
|
||||
function castToNumber(val) {
|
||||
return Types.Number.prototype.cast.call(this, val);
|
||||
}
|
||||
|
||||
function castArray (arr, self) {
|
||||
function castArraysOfNumbers(arr, self) {
|
||||
self || (self = this);
|
||||
|
||||
arr.forEach(function (v, i) {
|
||||
arr.forEach(function(v, i) {
|
||||
if (Array.isArray(v)) {
|
||||
castArray(v, self);
|
||||
castArraysOfNumbers(v, self);
|
||||
} else {
|
||||
arr[i] = castToNumber.call(self, v);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
SchemaArray.prototype.$conditionalHandlers = {
|
||||
'$all': function handle$all (val) {
|
||||
if (!Array.isArray(val)) {
|
||||
val = [val];
|
||||
function cast$near(val) {
|
||||
if (Array.isArray(val)) {
|
||||
castArraysOfNumbers(val, this);
|
||||
return val;
|
||||
}
|
||||
|
||||
if (val && val.$geometry) {
|
||||
return cast$geometry(val, this);
|
||||
}
|
||||
|
||||
return SchemaArray.prototype.castForQuery.call(this, val);
|
||||
}
|
||||
|
||||
function cast$geometry(val, self) {
|
||||
switch (val.$geometry.type) {
|
||||
case 'Polygon':
|
||||
case 'LineString':
|
||||
case 'Point':
|
||||
castArraysOfNumbers(val.$geometry.coordinates, self);
|
||||
break;
|
||||
default:
|
||||
// ignore unknowns
|
||||
break;
|
||||
}
|
||||
|
||||
if (val.$maxDistance) {
|
||||
val.$maxDistance = castToNumber.call(self, val.$maxDistance);
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
function cast$within(val) {
|
||||
var self = this;
|
||||
|
||||
if (val.$maxDistance) {
|
||||
val.$maxDistance = castToNumber.call(self, val.$maxDistance);
|
||||
}
|
||||
|
||||
if (val.$box || val.$polygon) {
|
||||
var type = val.$box ? '$box' : '$polygon';
|
||||
val[type].forEach(function(arr) {
|
||||
if (!Array.isArray(arr)) {
|
||||
var msg = 'Invalid $within $box argument. '
|
||||
+ 'Expected an array, received ' + arr;
|
||||
throw new TypeError(msg);
|
||||
}
|
||||
arr.forEach(function(v, i) {
|
||||
arr[i] = castToNumber.call(this, v);
|
||||
});
|
||||
});
|
||||
} else if (val.$center || val.$centerSphere) {
|
||||
type = val.$center ? '$center' : '$centerSphere';
|
||||
val[type].forEach(function(item, i) {
|
||||
if (Array.isArray(item)) {
|
||||
item.forEach(function(v, j) {
|
||||
item[j] = castToNumber.call(this, v);
|
||||
});
|
||||
} else {
|
||||
val[type][i] = castToNumber.call(this, item);
|
||||
}
|
||||
});
|
||||
} else if (val.$geometry) {
|
||||
cast$geometry(val, this);
|
||||
}
|
||||
|
||||
val = val.map(function (v) {
|
||||
if (v && 'Object' === v.constructor.name) {
|
||||
var o = {};
|
||||
o[this.path] = v;
|
||||
var query = new Query(o);
|
||||
query.cast(this.casterConstructor);
|
||||
return query._conditions[this.path];
|
||||
}
|
||||
return v;
|
||||
}, this);
|
||||
return val;
|
||||
}
|
||||
|
||||
return this.castForQuery(val);
|
||||
function cast$all(val) {
|
||||
if (!Array.isArray(val)) {
|
||||
val = [val];
|
||||
}
|
||||
|
||||
val = val.map(function(v) {
|
||||
if (utils.isObject(v)) {
|
||||
var o = {};
|
||||
o[this.path] = v;
|
||||
return cast(this.casterConstructor.schema, o)[this.path];
|
||||
}
|
||||
, '$elemMatch': function (val) {
|
||||
if (val.$in) {
|
||||
val.$in = this.castForQuery('$in', val.$in);
|
||||
return val;
|
||||
}
|
||||
return v;
|
||||
}, this);
|
||||
|
||||
var query = new Query(val);
|
||||
query.cast(this.casterConstructor);
|
||||
return query._conditions;
|
||||
return this.castForQuery(val);
|
||||
}
|
||||
|
||||
function cast$elemMatch(val) {
|
||||
var keys = Object.keys(val);
|
||||
var numKeys = keys.length;
|
||||
var key;
|
||||
var value;
|
||||
for (var i = 0; i < numKeys; ++i) {
|
||||
key = keys[i];
|
||||
value = val[key];
|
||||
if (key.indexOf('$') === 0 && value) {
|
||||
val[key] = this.castForQuery(key, value);
|
||||
}
|
||||
, '$size': castToNumber
|
||||
, '$ne': SchemaArray.prototype.castForQuery
|
||||
, '$in': SchemaArray.prototype.castForQuery
|
||||
, '$nin': SchemaArray.prototype.castForQuery
|
||||
, '$regex': SchemaArray.prototype.castForQuery
|
||||
, '$options': String
|
||||
, '$near': SchemaArray.prototype.castForQuery
|
||||
, '$nearSphere': SchemaArray.prototype.castForQuery
|
||||
, '$gt': SchemaArray.prototype.castForQuery
|
||||
, '$gte': SchemaArray.prototype.castForQuery
|
||||
, '$lt': SchemaArray.prototype.castForQuery
|
||||
, '$lte': SchemaArray.prototype.castForQuery
|
||||
, '$within': function (val) {
|
||||
var self = this;
|
||||
}
|
||||
|
||||
if (val.$maxDistance) {
|
||||
val.$maxDistance = castToNumber.call(this, val.$maxDistance);
|
||||
}
|
||||
return cast(this.casterConstructor.schema, val);
|
||||
}
|
||||
|
||||
if (val.$box || val.$polygon) {
|
||||
var type = val.$box ? '$box' : '$polygon';
|
||||
val[type].forEach(function (arr) {
|
||||
if (!Array.isArray(arr)) {
|
||||
var msg = 'Invalid $within $box argument. '
|
||||
+ 'Expected an array, received ' + arr;
|
||||
throw new TypeError(msg);
|
||||
}
|
||||
arr.forEach(function (v, i) {
|
||||
arr[i] = castToNumber.call(this, v);
|
||||
});
|
||||
})
|
||||
} else if (val.$center || val.$centerSphere) {
|
||||
var type = val.$center ? '$center' : '$centerSphere';
|
||||
val[type].forEach(function (item, i) {
|
||||
if (Array.isArray(item)) {
|
||||
item.forEach(function (v, j) {
|
||||
item[j] = castToNumber.call(this, v);
|
||||
});
|
||||
} else {
|
||||
val[type][i] = castToNumber.call(this, item);
|
||||
}
|
||||
})
|
||||
} else if (val.$geometry) {
|
||||
switch (val.$geometry.type) {
|
||||
case 'Polygon':
|
||||
case 'LineString':
|
||||
case 'Point':
|
||||
val.$geometry.coordinates.forEach(castArray);
|
||||
break;
|
||||
default:
|
||||
// ignore unknowns
|
||||
break;
|
||||
}
|
||||
}
|
||||
function cast$geoIntersects(val) {
|
||||
var geo = val.$geometry;
|
||||
if (!geo) return;
|
||||
|
||||
return val;
|
||||
}
|
||||
, '$geoIntersects': function (val) {
|
||||
var geo = val.$geometry;
|
||||
if (!geo) return;
|
||||
cast$geometry(val, this);
|
||||
return val;
|
||||
}
|
||||
|
||||
switch (val.$geometry.type) {
|
||||
case 'Polygon':
|
||||
case 'LineString':
|
||||
case 'Point':
|
||||
val.$geometry.coordinates.forEach(castArray);
|
||||
break;
|
||||
default:
|
||||
// ignore unknowns
|
||||
break;
|
||||
}
|
||||
var handle = SchemaArray.prototype.$conditionalHandlers = {};
|
||||
|
||||
return val;
|
||||
}
|
||||
, '$maxDistance': castToNumber
|
||||
handle.$all = cast$all;
|
||||
handle.$options = String;
|
||||
handle.$elemMatch = cast$elemMatch;
|
||||
handle.$geoIntersects = cast$geoIntersects;
|
||||
handle.$or = handle.$and = function(val) {
|
||||
if (!Array.isArray(val)) {
|
||||
throw new TypeError('conditional $or/$and require array');
|
||||
}
|
||||
|
||||
var ret = [];
|
||||
for (var i = 0; i < val.length; ++i) {
|
||||
ret.push(cast(this.casterConstructor.schema, val[i]));
|
||||
}
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
handle.$near =
|
||||
handle.$nearSphere = cast$near;
|
||||
|
||||
handle.$within =
|
||||
handle.$geoWithin = cast$within;
|
||||
|
||||
handle.$size =
|
||||
handle.$maxDistance = castToNumber;
|
||||
|
||||
handle.$eq =
|
||||
handle.$gt =
|
||||
handle.$gte =
|
||||
handle.$in =
|
||||
handle.$lt =
|
||||
handle.$lte =
|
||||
handle.$ne =
|
||||
handle.$nin =
|
||||
handle.$regex = SchemaArray.prototype.castForQuery;
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
45
node_modules/mongoose/lib/schema/boolean.js
generated
vendored
45
node_modules/mongoose/lib/schema/boolean.js
generated
vendored
@ -2,6 +2,8 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var utils = require('../utils');
|
||||
|
||||
var SchemaType = require('../schematype');
|
||||
|
||||
/**
|
||||
@ -13,14 +15,23 @@ var SchemaType = require('../schematype');
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function SchemaBoolean (path, options) {
|
||||
SchemaType.call(this, path, options);
|
||||
};
|
||||
function SchemaBoolean(path, options) {
|
||||
SchemaType.call(this, path, options, 'Boolean');
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
SchemaBoolean.schemaName = 'Boolean';
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
SchemaBoolean.prototype.__proto__ = SchemaType.prototype;
|
||||
SchemaBoolean.prototype = Object.create( SchemaType.prototype );
|
||||
SchemaBoolean.prototype.constructor = SchemaBoolean;
|
||||
|
||||
/**
|
||||
* Required validator
|
||||
@ -28,7 +39,7 @@ SchemaBoolean.prototype.__proto__ = SchemaType.prototype;
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaBoolean.prototype.checkRequired = function (value) {
|
||||
SchemaBoolean.prototype.checkRequired = function(value) {
|
||||
return value === true || value === false;
|
||||
};
|
||||
|
||||
@ -39,28 +50,16 @@ SchemaBoolean.prototype.checkRequired = function (value) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaBoolean.prototype.cast = function (value) {
|
||||
SchemaBoolean.prototype.cast = function(value) {
|
||||
if (null === value) return value;
|
||||
if ('0' === value) return false;
|
||||
if ('true' === value) return true;
|
||||
if ('false' === value) return false;
|
||||
return !! value;
|
||||
}
|
||||
return !!value;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function handleArray (val) {
|
||||
var self = this;
|
||||
return val.map(function (m) {
|
||||
return self.cast(m);
|
||||
});
|
||||
}
|
||||
|
||||
SchemaBoolean.$conditionalHandlers = {
|
||||
'$in': handleArray
|
||||
}
|
||||
SchemaBoolean.$conditionalHandlers =
|
||||
utils.options(SchemaType.prototype.$conditionalHandlers, {});
|
||||
|
||||
/**
|
||||
* Casts contents for queries.
|
||||
@ -70,7 +69,7 @@ SchemaBoolean.$conditionalHandlers = {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaBoolean.prototype.castForQuery = function ($conditional, val) {
|
||||
SchemaBoolean.prototype.castForQuery = function($conditional, val) {
|
||||
var handler;
|
||||
if (2 === arguments.length) {
|
||||
handler = SchemaBoolean.$conditionalHandlers[$conditional];
|
||||
|
78
node_modules/mongoose/lib/schema/buffer.js
generated
vendored
78
node_modules/mongoose/lib/schema/buffer.js
generated
vendored
@ -2,13 +2,14 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var SchemaType = require('../schematype')
|
||||
, CastError = SchemaType.CastError
|
||||
, MongooseBuffer = require('../types').Buffer
|
||||
, Binary = MongooseBuffer.Binary
|
||||
, Query = require('../query')
|
||||
, utils = require('../utils')
|
||||
, Document
|
||||
var utils = require('../utils');
|
||||
|
||||
var MongooseBuffer = require('../types').Buffer;
|
||||
var SchemaType = require('../schematype');
|
||||
|
||||
var Binary = MongooseBuffer.Binary;
|
||||
var CastError = SchemaType.CastError;
|
||||
var Document;
|
||||
|
||||
/**
|
||||
* Buffer SchemaType constructor
|
||||
@ -19,15 +20,23 @@ var SchemaType = require('../schematype')
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function SchemaBuffer (key, options) {
|
||||
function SchemaBuffer(key, options) {
|
||||
SchemaType.call(this, key, options, 'Buffer');
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
SchemaBuffer.schemaName = 'Buffer';
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
|
||||
SchemaBuffer.prototype.__proto__ = SchemaType.prototype;
|
||||
SchemaBuffer.prototype = Object.create( SchemaType.prototype );
|
||||
SchemaBuffer.prototype.constructor = SchemaBuffer;
|
||||
|
||||
/**
|
||||
* Check required
|
||||
@ -35,7 +44,7 @@ SchemaBuffer.prototype.__proto__ = SchemaType.prototype;
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaBuffer.prototype.checkRequired = function (value, doc) {
|
||||
SchemaBuffer.prototype.checkRequired = function(value, doc) {
|
||||
if (SchemaType._isRef(this, value, doc, true)) {
|
||||
return null != value;
|
||||
} else {
|
||||
@ -52,7 +61,8 @@ SchemaBuffer.prototype.checkRequired = function (value, doc) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaBuffer.prototype.cast = function (value, doc, init) {
|
||||
SchemaBuffer.prototype.cast = function(value, doc, init) {
|
||||
var ret;
|
||||
if (SchemaType._isRef(this, value, doc, init)) {
|
||||
// wait! we may need to cast this to a document
|
||||
|
||||
@ -81,7 +91,7 @@ SchemaBuffer.prototype.cast = function (value, doc, init) {
|
||||
var path = doc.$__fullPath(this.path);
|
||||
var owner = doc.ownerDocument ? doc.ownerDocument() : doc;
|
||||
var pop = owner.populated(path, true);
|
||||
var ret = new pop.options.model(value);
|
||||
ret = new pop.options.model(value);
|
||||
ret.$__.wasPopulated = true;
|
||||
return ret;
|
||||
}
|
||||
@ -91,17 +101,22 @@ SchemaBuffer.prototype.cast = function (value, doc, init) {
|
||||
value = value._id;
|
||||
}
|
||||
|
||||
if (value && value.isMongooseBuffer) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (Buffer.isBuffer(value)) {
|
||||
if (!(value instanceof MongooseBuffer)) {
|
||||
if (!value || !value.isMongooseBuffer) {
|
||||
value = new MongooseBuffer(value, [this.path, doc]);
|
||||
}
|
||||
|
||||
return value;
|
||||
} else if (value instanceof Binary) {
|
||||
var ret = new MongooseBuffer(value.value(true), [this.path, doc]);
|
||||
ret = new MongooseBuffer(value.value(true), [this.path, doc]);
|
||||
if (typeof value.sub_type !== 'number') {
|
||||
throw new CastError('buffer', value, this.path);
|
||||
}
|
||||
ret._subtype = value.sub_type;
|
||||
// do not override Binary subtypes. users set this
|
||||
// to whatever they want.
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -109,7 +124,7 @@ SchemaBuffer.prototype.cast = function (value, doc, init) {
|
||||
|
||||
var type = typeof value;
|
||||
if ('string' == type || 'number' == type || Array.isArray(value)) {
|
||||
var ret = new MongooseBuffer(value, [this.path, doc]);
|
||||
ret = new MongooseBuffer(value, [this.path, doc]);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -119,26 +134,17 @@ SchemaBuffer.prototype.cast = function (value, doc, init) {
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
function handleSingle (val) {
|
||||
function handleSingle(val) {
|
||||
return this.castForQuery(val);
|
||||
}
|
||||
|
||||
function handleArray (val) {
|
||||
var self = this;
|
||||
return val.map( function (m) {
|
||||
return self.castForQuery(m);
|
||||
SchemaBuffer.prototype.$conditionalHandlers =
|
||||
utils.options(SchemaType.prototype.$conditionalHandlers, {
|
||||
'$gt' : handleSingle,
|
||||
'$gte': handleSingle,
|
||||
'$lt' : handleSingle,
|
||||
'$lte': handleSingle
|
||||
});
|
||||
}
|
||||
|
||||
SchemaBuffer.prototype.$conditionalHandlers = {
|
||||
'$ne' : handleSingle
|
||||
, '$in' : handleArray
|
||||
, '$nin': handleArray
|
||||
, '$gt' : handleSingle
|
||||
, '$lt' : handleSingle
|
||||
, '$gte': handleSingle
|
||||
, '$lte': handleSingle
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts contents for queries.
|
||||
@ -148,7 +154,7 @@ SchemaBuffer.prototype.$conditionalHandlers = {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaBuffer.prototype.castForQuery = function ($conditional, val) {
|
||||
SchemaBuffer.prototype.castForQuery = function($conditional, val) {
|
||||
var handler;
|
||||
if (arguments.length === 2) {
|
||||
handler = this.$conditionalHandlers[$conditional];
|
||||
|
193
node_modules/mongoose/lib/schema/date.js
generated
vendored
193
node_modules/mongoose/lib/schema/date.js
generated
vendored
@ -2,10 +2,13 @@
|
||||
* Module requirements.
|
||||
*/
|
||||
|
||||
var SchemaType = require('../schematype');
|
||||
var CastError = SchemaType.CastError;
|
||||
var errorMessages = require('../error').messages;
|
||||
var utils = require('../utils');
|
||||
|
||||
var SchemaType = require('../schematype');
|
||||
|
||||
var CastError = SchemaType.CastError;
|
||||
|
||||
/**
|
||||
* Date SchemaType constructor.
|
||||
*
|
||||
@ -15,15 +18,23 @@ var utils = require('../utils');
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function SchemaDate (key, options) {
|
||||
SchemaType.call(this, key, options);
|
||||
};
|
||||
function SchemaDate(key, options) {
|
||||
SchemaType.call(this, key, options, 'Date');
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
SchemaDate.schemaName = 'Date';
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
|
||||
SchemaDate.prototype.__proto__ = SchemaType.prototype;
|
||||
SchemaDate.prototype = Object.create( SchemaType.prototype );
|
||||
SchemaDate.prototype.constructor = SchemaDate;
|
||||
|
||||
/**
|
||||
* Declares a TTL index (rounded to the nearest second) for _Date_ types only.
|
||||
@ -56,7 +67,7 @@ SchemaDate.prototype.__proto__ = SchemaType.prototype;
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaDate.prototype.expires = function (when) {
|
||||
SchemaDate.prototype.expires = function(when) {
|
||||
if (!this._index || 'Object' !== this._index.constructor.name) {
|
||||
this._index = {};
|
||||
}
|
||||
@ -72,10 +83,122 @@ SchemaDate.prototype.expires = function (when) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaDate.prototype.checkRequired = function (value) {
|
||||
SchemaDate.prototype.checkRequired = function(value) {
|
||||
return value instanceof Date;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a minimum date validator.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var s = new Schema({ d: { type: Date, min: Date('1970-01-01') })
|
||||
* var M = db.model('M', s)
|
||||
* var m = new M({ d: Date('1969-12-31') })
|
||||
* m.save(function (err) {
|
||||
* console.error(err) // validator error
|
||||
* m.d = Date('2014-12-08');
|
||||
* m.save() // success
|
||||
* })
|
||||
*
|
||||
* // custom error messages
|
||||
* // We can also use the special {MIN} token which will be replaced with the invalid value
|
||||
* var min = [Date('1970-01-01'), 'The value of path `{PATH}` ({VALUE}) is beneath the limit ({MIN}).'];
|
||||
* var schema = new Schema({ d: { type: Date, min: min })
|
||||
* var M = mongoose.model('M', schema);
|
||||
* var s= new M({ d: Date('1969-12-31') });
|
||||
* s.validate(function (err) {
|
||||
* console.log(String(err)) // ValidationError: The value of path `d` (1969-12-31) is before the limit (1970-01-01).
|
||||
* })
|
||||
*
|
||||
* @param {Date} value minimum date
|
||||
* @param {String} [message] optional custom error message
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaDate.prototype.min = function(value, message) {
|
||||
if (this.minValidator) {
|
||||
this.validators = this.validators.filter(function(v) {
|
||||
return v.validator != this.minValidator;
|
||||
}, this);
|
||||
}
|
||||
|
||||
if (value) {
|
||||
var msg = message || errorMessages.Date.min;
|
||||
msg = msg.replace(/{MIN}/, (value === Date.now ? 'Date.now()' : this.cast(value).toString()));
|
||||
var self = this;
|
||||
this.validators.push({
|
||||
validator: this.minValidator = function(val) {
|
||||
var min = (value === Date.now ? value() : self.cast(value));
|
||||
return val === null || val.valueOf() >= min.valueOf();
|
||||
},
|
||||
message: msg,
|
||||
type: 'min',
|
||||
min: value
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a maximum date validator.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var s = new Schema({ d: { type: Date, max: Date('2014-01-01') })
|
||||
* var M = db.model('M', s)
|
||||
* var m = new M({ d: Date('2014-12-08') })
|
||||
* m.save(function (err) {
|
||||
* console.error(err) // validator error
|
||||
* m.d = Date('2013-12-31');
|
||||
* m.save() // success
|
||||
* })
|
||||
*
|
||||
* // custom error messages
|
||||
* // We can also use the special {MAX} token which will be replaced with the invalid value
|
||||
* var max = [Date('2014-01-01'), 'The value of path `{PATH}` ({VALUE}) exceeds the limit ({MAX}).'];
|
||||
* var schema = new Schema({ d: { type: Date, max: max })
|
||||
* var M = mongoose.model('M', schema);
|
||||
* var s= new M({ d: Date('2014-12-08') });
|
||||
* s.validate(function (err) {
|
||||
* console.log(String(err)) // ValidationError: The value of path `d` (2014-12-08) exceeds the limit (2014-01-01).
|
||||
* })
|
||||
*
|
||||
* @param {Date} maximum date
|
||||
* @param {String} [message] optional custom error message
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaDate.prototype.max = function(value, message) {
|
||||
if (this.maxValidator) {
|
||||
this.validators = this.validators.filter(function(v) {
|
||||
return v.validator != this.maxValidator;
|
||||
}, this);
|
||||
}
|
||||
|
||||
if (value) {
|
||||
var msg = message || errorMessages.Date.max;
|
||||
msg = msg.replace(/{MAX}/, (value === Date.now ? 'Date.now()' : this.cast(value).toString()));
|
||||
var self = this;
|
||||
this.validators.push({
|
||||
validator: this.maxValidator = function(val) {
|
||||
var max = (value === Date.now ? value() : self.cast(value));
|
||||
return val === null || val.valueOf() <= max.valueOf();
|
||||
},
|
||||
message: msg,
|
||||
type: 'max',
|
||||
max: value
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts to date
|
||||
*
|
||||
@ -83,8 +206,9 @@ SchemaDate.prototype.checkRequired = function (value) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaDate.prototype.cast = function (value) {
|
||||
if (value === null || value === '')
|
||||
SchemaDate.prototype.cast = function(value) {
|
||||
// If null or undefined
|
||||
if (value == null || value === '')
|
||||
return null;
|
||||
|
||||
if (value instanceof Date)
|
||||
@ -93,16 +217,19 @@ SchemaDate.prototype.cast = function (value) {
|
||||
var date;
|
||||
|
||||
// support for timestamps
|
||||
if (value instanceof Number || 'number' == typeof value
|
||||
|| String(value) == Number(value))
|
||||
date = new Date(Number(value));
|
||||
if (typeof value !== 'undefined') {
|
||||
if (value instanceof Number || 'number' == typeof value
|
||||
|| String(value) == Number(value)) {
|
||||
date = new Date(Number(value));
|
||||
} else if (value.toString) {
|
||||
// support for date strings
|
||||
date = new Date(value.toString());
|
||||
}
|
||||
|
||||
// support for date strings
|
||||
else if (value.toString)
|
||||
date = new Date(value.toString());
|
||||
|
||||
if (date.toString() != 'Invalid Date')
|
||||
return date;
|
||||
if (date.toString() != 'Invalid Date') {
|
||||
return date;
|
||||
}
|
||||
}
|
||||
|
||||
throw new CastError('date', value, this.path);
|
||||
};
|
||||
@ -113,27 +240,17 @@ SchemaDate.prototype.cast = function (value) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function handleSingle (val) {
|
||||
function handleSingle(val) {
|
||||
return this.cast(val);
|
||||
}
|
||||
|
||||
function handleArray (val) {
|
||||
var self = this;
|
||||
return val.map( function (m) {
|
||||
return self.cast(m);
|
||||
SchemaDate.prototype.$conditionalHandlers =
|
||||
utils.options(SchemaType.prototype.$conditionalHandlers, {
|
||||
'$gt': handleSingle,
|
||||
'$gte': handleSingle,
|
||||
'$lt': handleSingle,
|
||||
'$lte': handleSingle
|
||||
});
|
||||
}
|
||||
|
||||
SchemaDate.prototype.$conditionalHandlers = {
|
||||
'$lt': handleSingle
|
||||
, '$lte': handleSingle
|
||||
, '$gt': handleSingle
|
||||
, '$gte': handleSingle
|
||||
, '$ne': handleSingle
|
||||
, '$in': handleArray
|
||||
, '$nin': handleArray
|
||||
, '$all': handleArray
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
@ -144,7 +261,7 @@ SchemaDate.prototype.$conditionalHandlers = {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaDate.prototype.castForQuery = function ($conditional, val) {
|
||||
SchemaDate.prototype.castForQuery = function($conditional, val) {
|
||||
var handler;
|
||||
|
||||
if (2 !== arguments.length) {
|
||||
|
173
node_modules/mongoose/lib/schema/documentarray.js
generated
vendored
173
node_modules/mongoose/lib/schema/documentarray.js
generated
vendored
@ -1,13 +1,14 @@
|
||||
/* eslint no-empty: 1 */
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var SchemaType = require('../schematype')
|
||||
, ArrayType = require('./array')
|
||||
, MongooseDocumentArray = require('../types/documentarray')
|
||||
, Subdocument = require('../types/embedded')
|
||||
, Document = require('../document');
|
||||
var ArrayType = require('./array');
|
||||
var CastError = require('../error/cast');
|
||||
var MongooseDocumentArray = require('../types/documentarray');
|
||||
var SchemaType = require('../schematype');
|
||||
var Subdocument = require('../types/embedded');
|
||||
|
||||
/**
|
||||
* SubdocsArray SchemaType constructor
|
||||
@ -19,24 +20,22 @@ var SchemaType = require('../schematype')
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function DocumentArray (key, schema, options) {
|
||||
|
||||
function DocumentArray(key, schema, options) {
|
||||
// compile an embedded document for this schema
|
||||
function EmbeddedDocument () {
|
||||
function EmbeddedDocument() {
|
||||
Subdocument.apply(this, arguments);
|
||||
}
|
||||
|
||||
EmbeddedDocument.prototype.__proto__ = Subdocument.prototype;
|
||||
EmbeddedDocument.prototype = Object.create(Subdocument.prototype);
|
||||
EmbeddedDocument.prototype.$__setSchema(schema);
|
||||
EmbeddedDocument.schema = schema;
|
||||
|
||||
// apply methods
|
||||
for (var i in schema.methods) {
|
||||
for (var i in schema.methods)
|
||||
EmbeddedDocument.prototype[i] = schema.methods[i];
|
||||
}
|
||||
|
||||
// apply statics
|
||||
for (var i in schema.statics)
|
||||
for (i in schema.statics)
|
||||
EmbeddedDocument[i] = schema.statics[i];
|
||||
|
||||
EmbeddedDocument.options = options;
|
||||
@ -48,18 +47,26 @@ function DocumentArray (key, schema, options) {
|
||||
var path = this.path;
|
||||
var fn = this.defaultValue;
|
||||
|
||||
this.default(function(){
|
||||
this.default(function() {
|
||||
var arr = fn.call(this);
|
||||
if (!Array.isArray(arr)) arr = [arr];
|
||||
return new MongooseDocumentArray(arr, path, this);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
DocumentArray.schemaName = 'DocumentArray';
|
||||
|
||||
/*!
|
||||
* Inherits from ArrayType.
|
||||
*/
|
||||
|
||||
DocumentArray.prototype.__proto__ = ArrayType.prototype;
|
||||
DocumentArray.prototype = Object.create( ArrayType.prototype );
|
||||
DocumentArray.prototype.constructor = DocumentArray;
|
||||
|
||||
/**
|
||||
* Performs local validations first, then validations on each embedded doc
|
||||
@ -67,14 +74,14 @@ DocumentArray.prototype.__proto__ = ArrayType.prototype;
|
||||
* @api private
|
||||
*/
|
||||
|
||||
DocumentArray.prototype.doValidate = function (array, fn, scope) {
|
||||
var self = this;
|
||||
DocumentArray.prototype.doValidate = function(array, fn, scope) {
|
||||
SchemaType.prototype.doValidate.call(this, array, function(err) {
|
||||
if (err) {
|
||||
return fn(err);
|
||||
}
|
||||
|
||||
SchemaType.prototype.doValidate.call(this, array, function (err) {
|
||||
if (err) return fn(err);
|
||||
|
||||
var count = array && array.length
|
||||
, error;
|
||||
var count = array && array.length;
|
||||
var error;
|
||||
|
||||
if (!count) return fn();
|
||||
|
||||
@ -86,24 +93,61 @@ DocumentArray.prototype.doValidate = function (array, fn, scope) {
|
||||
// sidestep sparse entries
|
||||
var doc = array[i];
|
||||
if (!doc) {
|
||||
--count || fn();
|
||||
--count || fn(error);
|
||||
continue;
|
||||
}
|
||||
|
||||
;(function (i) {
|
||||
doc.validate(function (err) {
|
||||
if (err && !error) {
|
||||
// rewrite the key
|
||||
err.key = self.key + '.' + i + '.' + err.key;
|
||||
return fn(error = err);
|
||||
}
|
||||
--count || fn();
|
||||
});
|
||||
})(i);
|
||||
doc.validate(function(err) {
|
||||
if (err) {
|
||||
error = err;
|
||||
}
|
||||
--count || fn(error);
|
||||
});
|
||||
}
|
||||
}, scope);
|
||||
};
|
||||
|
||||
/**
|
||||
* Performs local validations first, then validations on each embedded doc.
|
||||
*
|
||||
* ####Note:
|
||||
*
|
||||
* This method ignores the asynchronous validators.
|
||||
*
|
||||
* @return {MongooseError|undefined}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
DocumentArray.prototype.doValidateSync = function(array, scope) {
|
||||
var schemaTypeError = SchemaType.prototype.doValidateSync.call(this, array, scope);
|
||||
if (schemaTypeError) return schemaTypeError;
|
||||
|
||||
var count = array && array.length,
|
||||
resultError = null;
|
||||
|
||||
if (!count) return;
|
||||
|
||||
// handle sparse arrays, do not use array.forEach which does not
|
||||
// iterate over sparse elements yet reports array.length including
|
||||
// them :(
|
||||
|
||||
for (var i = 0, len = count; i < len; ++i) {
|
||||
// only first error
|
||||
if ( resultError ) break;
|
||||
// sidestep sparse entries
|
||||
var doc = array[i];
|
||||
if (!doc) continue;
|
||||
|
||||
var subdocValidateError = doc.validateSync();
|
||||
|
||||
if (subdocValidateError) {
|
||||
resultError = subdocValidateError;
|
||||
}
|
||||
}
|
||||
|
||||
return resultError;
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts contents
|
||||
*
|
||||
@ -112,17 +156,27 @@ DocumentArray.prototype.doValidate = function (array, fn, scope) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
DocumentArray.prototype.cast = function (value, doc, init, prev) {
|
||||
var selected
|
||||
, subdoc
|
||||
, i
|
||||
DocumentArray.prototype.cast = function(value, doc, init, prev) {
|
||||
var selected,
|
||||
subdoc,
|
||||
i;
|
||||
|
||||
if (!Array.isArray(value)) {
|
||||
// gh-2442 mark whole array as modified if we're initializing a doc from
|
||||
// the db and the path isn't an array in the document
|
||||
if (!!doc && init) {
|
||||
doc.markModified(this.path);
|
||||
}
|
||||
return this.cast([value], doc, init, prev);
|
||||
}
|
||||
|
||||
if (!(value instanceof MongooseDocumentArray)) {
|
||||
if (!(value && value.isMongooseDocumentArray)) {
|
||||
value = new MongooseDocumentArray(value, this.path, doc);
|
||||
if (prev && prev._handlers) {
|
||||
for (var key in prev._handlers) {
|
||||
doc.removeListener(key, prev._handlers[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
i = value.length;
|
||||
@ -131,26 +185,37 @@ DocumentArray.prototype.cast = function (value, doc, init, prev) {
|
||||
if (!(value[i] instanceof Subdocument) && value[i]) {
|
||||
if (init) {
|
||||
selected || (selected = scopePaths(this, doc.$__.selected, init));
|
||||
subdoc = new this.casterConstructor(null, value, true, selected);
|
||||
subdoc = new this.casterConstructor(null, value, true, selected, i);
|
||||
value[i] = subdoc.init(value[i]);
|
||||
} else {
|
||||
if (prev && (subdoc = prev.id(value[i]._id))) {
|
||||
try {
|
||||
subdoc = prev.id(value[i]._id);
|
||||
} catch (e) {}
|
||||
|
||||
if (prev && subdoc) {
|
||||
// handle resetting doc with existing id but differing data
|
||||
// doc.array = [{ doc: 'val' }]
|
||||
subdoc.set(value[i]);
|
||||
// if set() is hooked it will have no return value
|
||||
// see gh-746
|
||||
value[i] = subdoc;
|
||||
} else {
|
||||
subdoc = new this.casterConstructor(value[i], value);
|
||||
try {
|
||||
subdoc = new this.casterConstructor(value[i], value, undefined,
|
||||
undefined, i);
|
||||
// if set() is hooked it will have no return value
|
||||
// see gh-746
|
||||
value[i] = subdoc;
|
||||
} catch (error) {
|
||||
throw new CastError('embedded', value[i], value._path);
|
||||
}
|
||||
}
|
||||
|
||||
// if set() is hooked it will have no return value
|
||||
// see gh-746
|
||||
value[i] = subdoc;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Scopes paths selected in a query to this array.
|
||||
@ -161,15 +226,15 @@ DocumentArray.prototype.cast = function (value, doc, init, prev) {
|
||||
* @param {Boolean|undefined} init - if we are being created part of a query result
|
||||
*/
|
||||
|
||||
function scopePaths (array, fields, init) {
|
||||
function scopePaths(array, fields, init) {
|
||||
if (!(init && fields)) return undefined;
|
||||
|
||||
var path = array.path + '.'
|
||||
, keys = Object.keys(fields)
|
||||
, i = keys.length
|
||||
, selected = {}
|
||||
, hasKeys
|
||||
, key
|
||||
var path = array.path + '.',
|
||||
keys = Object.keys(fields),
|
||||
i = keys.length,
|
||||
selected = {},
|
||||
hasKeys,
|
||||
key;
|
||||
|
||||
while (i--) {
|
||||
key = keys[i];
|
||||
|
118
node_modules/mongoose/lib/schema/embedded.js
generated
vendored
Normal file
118
node_modules/mongoose/lib/schema/embedded.js
generated
vendored
Normal file
@ -0,0 +1,118 @@
|
||||
var SchemaType = require('../schematype');
|
||||
var Subdocument = require('../types/subdocument');
|
||||
|
||||
module.exports = Embedded;
|
||||
|
||||
/**
|
||||
* Sub-schema schematype constructor
|
||||
*
|
||||
* @param {Schema} schema
|
||||
* @param {String} key
|
||||
* @param {Object} options
|
||||
* @inherits SchemaType
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function Embedded(schema, path, options) {
|
||||
var _embedded = function() {
|
||||
Subdocument.apply(this, arguments);
|
||||
};
|
||||
_embedded.prototype = Object.create(Subdocument.prototype);
|
||||
_embedded.prototype.$__setSchema(schema);
|
||||
_embedded.schema = schema;
|
||||
_embedded.$isSingleNested = true;
|
||||
_embedded.prototype.$basePath = path;
|
||||
|
||||
// apply methods
|
||||
for (var i in schema.methods) {
|
||||
_embedded.prototype[i] = schema.methods[i];
|
||||
}
|
||||
|
||||
// apply statics
|
||||
for (i in schema.statics) {
|
||||
_embedded[i] = schema.statics[i];
|
||||
}
|
||||
|
||||
this.caster = _embedded;
|
||||
this.schema = schema;
|
||||
this.$isSingleNested = true;
|
||||
SchemaType.call(this, path, options, 'Embedded');
|
||||
}
|
||||
|
||||
Embedded.prototype = Object.create(SchemaType.prototype);
|
||||
|
||||
/**
|
||||
* Casts contents
|
||||
*
|
||||
* @param {Object} value
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Embedded.prototype.cast = function(val, doc) {
|
||||
var subdoc = new this.caster();
|
||||
subdoc = subdoc.init(val);
|
||||
subdoc.$parent = doc;
|
||||
return subdoc;
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts contents for query
|
||||
*
|
||||
* @param {string} [$conditional] optional query operator (like `$eq` or `$in`)
|
||||
* @param {any} value
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Embedded.prototype.castForQuery = function($conditional, val) {
|
||||
var handler;
|
||||
if (arguments.length === 2) {
|
||||
handler = this.$conditionalHandlers[$conditional];
|
||||
if (!handler) {
|
||||
throw new Error('Can\'t use ' + $conditional);
|
||||
}
|
||||
return handler.call(this, val);
|
||||
} else {
|
||||
val = $conditional;
|
||||
return new this.caster(val).
|
||||
toObject({ virtuals: false });
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Async validation on this single nested doc.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Embedded.prototype.doValidate = function(value, fn) {
|
||||
SchemaType.prototype.doValidate.call(this, value, function(error) {
|
||||
if (error) {
|
||||
return fn(error);
|
||||
}
|
||||
value.validate(fn, { __noPromise: true });
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Synchronously validate this single nested doc
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Embedded.prototype.doValidateSync = function(value) {
|
||||
var schemaTypeError = SchemaType.prototype.doValidateSync.call(this, value);
|
||||
if (schemaTypeError) {
|
||||
return schemaTypeError;
|
||||
}
|
||||
return value.validateSync();
|
||||
};
|
||||
|
||||
/**
|
||||
* Required validator for single nested docs
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Embedded.prototype.checkRequired = function(value) {
|
||||
return !!value && value.$isSingleNested;
|
||||
};
|
2
node_modules/mongoose/lib/schema/index.js
generated
vendored
2
node_modules/mongoose/lib/schema/index.js
generated
vendored
@ -11,6 +11,8 @@ exports.Boolean = require('./boolean');
|
||||
|
||||
exports.DocumentArray = require('./documentarray');
|
||||
|
||||
exports.Embedded = require('./embedded');
|
||||
|
||||
exports.Array = require('./array');
|
||||
|
||||
exports.Buffer = require('./buffer');
|
||||
|
32
node_modules/mongoose/lib/schema/mixed.js
generated
vendored
32
node_modules/mongoose/lib/schema/mixed.js
generated
vendored
@ -15,7 +15,7 @@ var utils = require('../utils');
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function Mixed (path, options) {
|
||||
function Mixed(path, options) {
|
||||
if (options && options.default) {
|
||||
var def = options.default;
|
||||
if (Array.isArray(def) && 0 === def.length) {
|
||||
@ -25,20 +25,28 @@ function Mixed (path, options) {
|
||||
utils.isObject(def) &&
|
||||
0 === Object.keys(def).length) {
|
||||
// prevent odd "shared" objects between documents
|
||||
options.default = function () {
|
||||
return {}
|
||||
}
|
||||
options.default = function() {
|
||||
return {};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
SchemaType.call(this, path, options);
|
||||
};
|
||||
SchemaType.call(this, path, options, 'Mixed');
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
Mixed.schemaName = 'Mixed';
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
|
||||
Mixed.prototype.__proto__ = SchemaType.prototype;
|
||||
Mixed.prototype = Object.create( SchemaType.prototype );
|
||||
Mixed.prototype.constructor = Mixed;
|
||||
|
||||
/**
|
||||
* Required validator
|
||||
@ -46,8 +54,8 @@ Mixed.prototype.__proto__ = SchemaType.prototype;
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Mixed.prototype.checkRequired = function (val) {
|
||||
return true;
|
||||
Mixed.prototype.checkRequired = function(val) {
|
||||
return (val !== undefined) && (val !== null);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -59,7 +67,7 @@ Mixed.prototype.checkRequired = function (val) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Mixed.prototype.cast = function (val) {
|
||||
Mixed.prototype.cast = function(val) {
|
||||
return val;
|
||||
};
|
||||
|
||||
@ -71,7 +79,7 @@ Mixed.prototype.cast = function (val) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Mixed.prototype.castForQuery = function ($cond, val) {
|
||||
Mixed.prototype.castForQuery = function($cond, val) {
|
||||
if (arguments.length === 2) return val;
|
||||
return $cond;
|
||||
};
|
||||
|
145
node_modules/mongoose/lib/schema/number.js
generated
vendored
145
node_modules/mongoose/lib/schema/number.js
generated
vendored
@ -2,10 +2,11 @@
|
||||
* Module requirements.
|
||||
*/
|
||||
|
||||
var SchemaType = require('../schematype')
|
||||
, CastError = SchemaType.CastError
|
||||
, utils = require('../utils')
|
||||
, Document
|
||||
var SchemaType = require('../schematype'),
|
||||
CastError = SchemaType.CastError,
|
||||
errorMessages = require('../error').messages,
|
||||
utils = require('../utils'),
|
||||
Document;
|
||||
|
||||
/**
|
||||
* Number SchemaType constructor.
|
||||
@ -16,15 +17,23 @@ var SchemaType = require('../schematype')
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function SchemaNumber (key, options) {
|
||||
function SchemaNumber(key, options) {
|
||||
SchemaType.call(this, key, options, 'Number');
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
SchemaNumber.schemaName = 'Number';
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
|
||||
SchemaNumber.prototype.__proto__ = SchemaType.prototype;
|
||||
SchemaNumber.prototype = Object.create( SchemaType.prototype );
|
||||
SchemaNumber.prototype.constructor = SchemaNumber;
|
||||
|
||||
/**
|
||||
* Required validator for number
|
||||
@ -32,7 +41,7 @@ SchemaNumber.prototype.__proto__ = SchemaType.prototype;
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaNumber.prototype.checkRequired = function checkRequired (value, doc) {
|
||||
SchemaNumber.prototype.checkRequired = function checkRequired(value, doc) {
|
||||
if (SchemaType._isRef(this, value, doc, true)) {
|
||||
return null != value;
|
||||
} else {
|
||||
@ -54,22 +63,41 @@ SchemaNumber.prototype.checkRequired = function checkRequired (value, doc) {
|
||||
* m.save() // success
|
||||
* })
|
||||
*
|
||||
* // custom error messages
|
||||
* // We can also use the special {MIN} token which will be replaced with the invalid value
|
||||
* var min = [10, 'The value of path `{PATH}` ({VALUE}) is beneath the limit ({MIN}).'];
|
||||
* var schema = new Schema({ n: { type: Number, min: min })
|
||||
* var M = mongoose.model('Measurement', schema);
|
||||
* var s= new M({ n: 4 });
|
||||
* s.validate(function (err) {
|
||||
* console.log(String(err)) // ValidationError: The value of path `n` (4) is beneath the limit (10).
|
||||
* })
|
||||
*
|
||||
* @param {Number} value minimum number
|
||||
* @param {String} [message] optional custom error message
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaNumber.prototype.min = function (value) {
|
||||
SchemaNumber.prototype.min = function(value, message) {
|
||||
if (this.minValidator) {
|
||||
this.validators = this.validators.filter(function (v) {
|
||||
return 'min' != v[1];
|
||||
});
|
||||
this.validators = this.validators.filter(function(v) {
|
||||
return v.validator != this.minValidator;
|
||||
}, this);
|
||||
}
|
||||
|
||||
if (value != null) {
|
||||
this.validators.push([this.minValidator = function (v) {
|
||||
return v === null || v >= value;
|
||||
}, 'min']);
|
||||
if (null != value) {
|
||||
var msg = message || errorMessages.Number.min;
|
||||
msg = msg.replace(/{MIN}/, value);
|
||||
this.validators.push({
|
||||
validator: this.minValidator = function(v) {
|
||||
return v == null || v >= value;
|
||||
},
|
||||
message: msg,
|
||||
type: 'min',
|
||||
min: value
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
@ -89,22 +117,41 @@ SchemaNumber.prototype.min = function (value) {
|
||||
* m.save() // success
|
||||
* })
|
||||
*
|
||||
* // custom error messages
|
||||
* // We can also use the special {MAX} token which will be replaced with the invalid value
|
||||
* var max = [10, 'The value of path `{PATH}` ({VALUE}) exceeds the limit ({MAX}).'];
|
||||
* var schema = new Schema({ n: { type: Number, max: max })
|
||||
* var M = mongoose.model('Measurement', schema);
|
||||
* var s= new M({ n: 4 });
|
||||
* s.validate(function (err) {
|
||||
* console.log(String(err)) // ValidationError: The value of path `n` (4) exceeds the limit (10).
|
||||
* })
|
||||
*
|
||||
* @param {Number} maximum number
|
||||
* @param {String} [message] optional custom error message
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaNumber.prototype.max = function (value) {
|
||||
SchemaNumber.prototype.max = function(value, message) {
|
||||
if (this.maxValidator) {
|
||||
this.validators = this.validators.filter(function(v){
|
||||
return 'max' != v[1];
|
||||
});
|
||||
this.validators = this.validators.filter(function(v) {
|
||||
return v.validator != this.maxValidator;
|
||||
}, this);
|
||||
}
|
||||
|
||||
if (value != null) {
|
||||
this.validators.push([this.maxValidator = function(v){
|
||||
return v === null || v <= value;
|
||||
}, 'max']);
|
||||
if (null != value) {
|
||||
var msg = message || errorMessages.Number.max;
|
||||
msg = msg.replace(/{MAX}/, value);
|
||||
this.validators.push({
|
||||
validator: this.maxValidator = function(v) {
|
||||
return v == null || v <= value;
|
||||
},
|
||||
message: msg,
|
||||
type: 'max',
|
||||
max: value
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
@ -119,7 +166,7 @@ SchemaNumber.prototype.max = function (value) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaNumber.prototype.cast = function (value, doc, init) {
|
||||
SchemaNumber.prototype.cast = function(value, doc, init) {
|
||||
if (SchemaType._isRef(this, value, doc, init)) {
|
||||
// wait! we may need to cast this to a document
|
||||
|
||||
@ -157,15 +204,17 @@ SchemaNumber.prototype.cast = function (value, doc, init) {
|
||||
? value._id // documents
|
||||
: value;
|
||||
|
||||
if (!isNaN(val)){
|
||||
if (!isNaN(val)) {
|
||||
if (null === val) return val;
|
||||
if ('' === val) return null;
|
||||
if ('string' == typeof val) val = Number(val);
|
||||
if (val instanceof Number) return val
|
||||
if (typeof val === 'string' || typeof val === 'boolean') {
|
||||
val = Number(val);
|
||||
}
|
||||
if (val instanceof Number) return val;
|
||||
if ('number' == typeof val) return val;
|
||||
if (val.toString && !Array.isArray(val) &&
|
||||
val.toString() == Number(val)) {
|
||||
return new Number(val)
|
||||
return new Number(val);
|
||||
}
|
||||
}
|
||||
|
||||
@ -176,28 +225,28 @@ SchemaNumber.prototype.cast = function (value, doc, init) {
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function handleSingle (val) {
|
||||
return this.cast(val)
|
||||
function handleSingle(val) {
|
||||
return this.cast(val);
|
||||
}
|
||||
|
||||
function handleArray (val) {
|
||||
function handleArray(val) {
|
||||
var self = this;
|
||||
return val.map(function (m) {
|
||||
return self.cast(m)
|
||||
if (!Array.isArray(val)) {
|
||||
return [this.cast(val)];
|
||||
}
|
||||
return val.map(function(m) {
|
||||
return self.cast(m);
|
||||
});
|
||||
}
|
||||
|
||||
SchemaNumber.prototype.$conditionalHandlers = {
|
||||
'$lt' : handleSingle
|
||||
, '$lte': handleSingle
|
||||
, '$gt' : handleSingle
|
||||
, '$gte': handleSingle
|
||||
, '$ne' : handleSingle
|
||||
, '$in' : handleArray
|
||||
, '$nin': handleArray
|
||||
, '$mod': handleArray
|
||||
, '$all': handleArray
|
||||
};
|
||||
SchemaNumber.prototype.$conditionalHandlers =
|
||||
utils.options(SchemaType.prototype.$conditionalHandlers, {
|
||||
'$gt' : handleSingle,
|
||||
'$gte': handleSingle,
|
||||
'$lt' : handleSingle,
|
||||
'$lte': handleSingle,
|
||||
'$mod': handleArray
|
||||
});
|
||||
|
||||
/**
|
||||
* Casts contents for queries.
|
||||
@ -207,7 +256,7 @@ SchemaNumber.prototype.$conditionalHandlers = {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaNumber.prototype.castForQuery = function ($conditional, val) {
|
||||
SchemaNumber.prototype.castForQuery = function($conditional, val) {
|
||||
var handler;
|
||||
if (arguments.length === 2) {
|
||||
handler = this.$conditionalHandlers[$conditional];
|
||||
@ -216,7 +265,7 @@ SchemaNumber.prototype.castForQuery = function ($conditional, val) {
|
||||
return handler.call(this, val);
|
||||
} else {
|
||||
val = this.cast($conditional);
|
||||
return val == null ? val : val
|
||||
return val == null ? val : val;
|
||||
}
|
||||
};
|
||||
|
||||
|
88
node_modules/mongoose/lib/schema/objectid.js
generated
vendored
88
node_modules/mongoose/lib/schema/objectid.js
generated
vendored
@ -1,13 +1,14 @@
|
||||
/* eslint no-empty: 1 */
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var SchemaType = require('../schematype')
|
||||
, CastError = SchemaType.CastError
|
||||
, driver = global.MONGOOSE_DRIVER_PATH || './../drivers/node-mongodb-native'
|
||||
, oid = require('../types/objectid')
|
||||
, utils = require('../utils')
|
||||
, Document
|
||||
var SchemaType = require('../schematype'),
|
||||
CastError = SchemaType.CastError,
|
||||
oid = require('../types/objectid'),
|
||||
utils = require('../utils'),
|
||||
Document;
|
||||
|
||||
/**
|
||||
* ObjectId SchemaType constructor.
|
||||
@ -18,15 +19,23 @@ var SchemaType = require('../schematype')
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function ObjectId (key, options) {
|
||||
function ObjectId(key, options) {
|
||||
SchemaType.call(this, key, options, 'ObjectID');
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
ObjectId.schemaName = 'ObjectId';
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
|
||||
ObjectId.prototype.__proto__ = SchemaType.prototype;
|
||||
ObjectId.prototype = Object.create( SchemaType.prototype );
|
||||
ObjectId.prototype.constructor = ObjectId;
|
||||
|
||||
/**
|
||||
* Adds an auto-generated ObjectId default if turnOn is true.
|
||||
@ -35,10 +44,10 @@ ObjectId.prototype.__proto__ = SchemaType.prototype;
|
||||
* @return {SchemaType} this
|
||||
*/
|
||||
|
||||
ObjectId.prototype.auto = function (turnOn) {
|
||||
ObjectId.prototype.auto = function(turnOn) {
|
||||
if (turnOn) {
|
||||
this.default(defaultId);
|
||||
this.set(resetId)
|
||||
this.set(resetId);
|
||||
}
|
||||
|
||||
return this;
|
||||
@ -50,7 +59,7 @@ ObjectId.prototype.auto = function (turnOn) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
ObjectId.prototype.checkRequired = function checkRequired (value, doc) {
|
||||
ObjectId.prototype.checkRequired = function checkRequired(value, doc) {
|
||||
if (SchemaType._isRef(this, value, doc, true)) {
|
||||
return null != value;
|
||||
} else {
|
||||
@ -67,7 +76,7 @@ ObjectId.prototype.checkRequired = function checkRequired (value, doc) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
ObjectId.prototype.cast = function (value, doc, init) {
|
||||
ObjectId.prototype.cast = function(value, doc, init) {
|
||||
if (SchemaType._isRef(this, value, doc, init)) {
|
||||
// wait! we may need to cast this to a document
|
||||
|
||||
@ -101,17 +110,26 @@ ObjectId.prototype.cast = function (value, doc, init) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (value === null) return value;
|
||||
// If null or undefined
|
||||
if (value == null) return value;
|
||||
|
||||
if (value instanceof oid)
|
||||
return value;
|
||||
|
||||
if (value._id && value._id instanceof oid)
|
||||
return value._id;
|
||||
if (value._id) {
|
||||
if (value._id instanceof oid) {
|
||||
return value._id;
|
||||
}
|
||||
if (value._id.toString instanceof Function) {
|
||||
try {
|
||||
return oid.createFromHexString(value._id.toString());
|
||||
} catch (e) {}
|
||||
}
|
||||
}
|
||||
|
||||
if (value.toString) {
|
||||
if (value.toString instanceof Function) {
|
||||
try {
|
||||
return oid.fromString(value.toString());
|
||||
return oid.createFromHexString(value.toString());
|
||||
} catch (err) {
|
||||
throw new CastError('ObjectId', value, this.path);
|
||||
}
|
||||
@ -124,27 +142,17 @@ ObjectId.prototype.cast = function (value, doc, init) {
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function handleSingle (val) {
|
||||
function handleSingle(val) {
|
||||
return this.cast(val);
|
||||
}
|
||||
|
||||
function handleArray (val) {
|
||||
var self = this;
|
||||
return val.map(function (m) {
|
||||
return self.cast(m);
|
||||
ObjectId.prototype.$conditionalHandlers =
|
||||
utils.options(SchemaType.prototype.$conditionalHandlers, {
|
||||
'$gt': handleSingle,
|
||||
'$gte': handleSingle,
|
||||
'$lt': handleSingle,
|
||||
'$lte': handleSingle
|
||||
});
|
||||
}
|
||||
|
||||
ObjectId.prototype.$conditionalHandlers = {
|
||||
'$ne': handleSingle
|
||||
, '$in': handleArray
|
||||
, '$nin': handleArray
|
||||
, '$gt': handleSingle
|
||||
, '$lt': handleSingle
|
||||
, '$gte': handleSingle
|
||||
, '$lte': handleSingle
|
||||
, '$all': handleArray
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts contents for queries.
|
||||
@ -154,7 +162,7 @@ ObjectId.prototype.$conditionalHandlers = {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
ObjectId.prototype.castForQuery = function ($conditional, val) {
|
||||
ObjectId.prototype.castForQuery = function($conditional, val) {
|
||||
var handler;
|
||||
if (arguments.length === 2) {
|
||||
handler = this.$conditionalHandlers[$conditional];
|
||||
@ -170,11 +178,11 @@ ObjectId.prototype.castForQuery = function ($conditional, val) {
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function defaultId () {
|
||||
function defaultId() {
|
||||
return new oid();
|
||||
};
|
||||
}
|
||||
|
||||
function resetId (v) {
|
||||
function resetId(v) {
|
||||
this.$__._id = null;
|
||||
return v;
|
||||
}
|
||||
|
320
node_modules/mongoose/lib/schema/string.js
generated
vendored
320
node_modules/mongoose/lib/schema/string.js
generated
vendored
@ -3,10 +3,11 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var SchemaType = require('../schematype')
|
||||
, CastError = SchemaType.CastError
|
||||
, utils = require('../utils')
|
||||
, Document
|
||||
var SchemaType = require('../schematype'),
|
||||
CastError = SchemaType.CastError,
|
||||
errorMessages = require('../error').messages,
|
||||
utils = require('../utils'),
|
||||
Document;
|
||||
|
||||
/**
|
||||
* String SchemaType constructor.
|
||||
@ -17,64 +18,100 @@ var SchemaType = require('../schematype')
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function SchemaString (key, options) {
|
||||
function SchemaString(key, options) {
|
||||
this.enumValues = [];
|
||||
this.regExp = null;
|
||||
SchemaType.call(this, key, options, 'String');
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
SchemaString.schemaName = 'String';
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
|
||||
SchemaString.prototype.__proto__ = SchemaType.prototype;
|
||||
SchemaString.prototype = Object.create( SchemaType.prototype );
|
||||
SchemaString.prototype.constructor = SchemaString;
|
||||
|
||||
/**
|
||||
* Adds enumeration values and a coinciding validator.
|
||||
* Adds an enum validator
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var states = 'opening open closing closed'.split(' ')
|
||||
* var s = new Schema({ state: { type: String, enum: states })
|
||||
* var s = new Schema({ state: { type: String, enum: states }})
|
||||
* var M = db.model('M', s)
|
||||
* var m = new M({ state: 'invalid' })
|
||||
* m.save(function (err) {
|
||||
* console.error(err) // validator error
|
||||
* console.error(String(err)) // ValidationError: `invalid` is not a valid enum value for path `state`.
|
||||
* m.state = 'open'
|
||||
* m.save() // success
|
||||
* m.save(callback) // success
|
||||
* })
|
||||
*
|
||||
* @param {String} [args...] enumeration values
|
||||
* // or with custom error messages
|
||||
* var enu = {
|
||||
* values: 'opening open closing closed'.split(' '),
|
||||
* message: 'enum validator failed for path `{PATH}` with value `{VALUE}`'
|
||||
* }
|
||||
* var s = new Schema({ state: { type: String, enum: enu })
|
||||
* var M = db.model('M', s)
|
||||
* var m = new M({ state: 'invalid' })
|
||||
* m.save(function (err) {
|
||||
* console.error(String(err)) // ValidationError: enum validator failed for path `state` with value `invalid`
|
||||
* m.state = 'open'
|
||||
* m.save(callback) // success
|
||||
* })
|
||||
*
|
||||
* @param {String|Object} [args...] enumeration values
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaString.prototype.enum = function () {
|
||||
var len = arguments.length;
|
||||
SchemaString.prototype.enum = function() {
|
||||
if (this.enumValidator) {
|
||||
this.validators = this.validators.filter(function(v) {
|
||||
return v.validator != this.enumValidator;
|
||||
}, this);
|
||||
this.enumValidator = false;
|
||||
}
|
||||
|
||||
if (!len || undefined === arguments[0] || false === arguments[0]) {
|
||||
if (this.enumValidator){
|
||||
this.enumValidator = false;
|
||||
this.validators = this.validators.filter(function(v){
|
||||
return v[1] != 'enum';
|
||||
});
|
||||
}
|
||||
if (undefined === arguments[0] || false === arguments[0]) {
|
||||
return this;
|
||||
}
|
||||
|
||||
for (var i = 0; i < len; i++) {
|
||||
if (undefined !== arguments[i]) {
|
||||
this.enumValues.push(this.cast(arguments[i]));
|
||||
var values;
|
||||
var errorMessage;
|
||||
|
||||
if (utils.isObject(arguments[0])) {
|
||||
values = arguments[0].values;
|
||||
errorMessage = arguments[0].message;
|
||||
} else {
|
||||
values = arguments;
|
||||
errorMessage = errorMessages.String.enum;
|
||||
}
|
||||
|
||||
for (var i = 0; i < values.length; i++) {
|
||||
if (undefined !== values[i]) {
|
||||
this.enumValues.push(this.cast(values[i]));
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.enumValidator) {
|
||||
var values = this.enumValues;
|
||||
this.enumValidator = function(v){
|
||||
return undefined === v || ~values.indexOf(v);
|
||||
};
|
||||
this.validators.push([this.enumValidator, 'enum']);
|
||||
}
|
||||
var vals = this.enumValues;
|
||||
this.enumValidator = function(v) {
|
||||
return undefined === v || ~vals.indexOf(v);
|
||||
};
|
||||
this.validators.push({
|
||||
validator: this.enumValidator,
|
||||
message: errorMessage,
|
||||
type: 'enum',
|
||||
enumValues: vals
|
||||
});
|
||||
|
||||
return this;
|
||||
};
|
||||
@ -93,9 +130,9 @@ SchemaString.prototype.enum = function () {
|
||||
* @return {SchemaType} this
|
||||
*/
|
||||
|
||||
SchemaString.prototype.lowercase = function () {
|
||||
return this.set(function (v, self) {
|
||||
if ('string' != typeof v) v = self.cast(v)
|
||||
SchemaString.prototype.lowercase = function() {
|
||||
return this.set(function(v, self) {
|
||||
if ('string' != typeof v) v = self.cast(v);
|
||||
if (v) return v.toLowerCase();
|
||||
return v;
|
||||
});
|
||||
@ -115,9 +152,9 @@ SchemaString.prototype.lowercase = function () {
|
||||
* @return {SchemaType} this
|
||||
*/
|
||||
|
||||
SchemaString.prototype.uppercase = function () {
|
||||
return this.set(function (v, self) {
|
||||
if ('string' != typeof v) v = self.cast(v)
|
||||
SchemaString.prototype.uppercase = function() {
|
||||
return this.set(function(v, self) {
|
||||
if ('string' != typeof v) v = self.cast(v);
|
||||
if (v) return v.toUpperCase();
|
||||
return v;
|
||||
});
|
||||
@ -141,14 +178,122 @@ SchemaString.prototype.uppercase = function () {
|
||||
* @return {SchemaType} this
|
||||
*/
|
||||
|
||||
SchemaString.prototype.trim = function () {
|
||||
return this.set(function (v, self) {
|
||||
if ('string' != typeof v) v = self.cast(v)
|
||||
SchemaString.prototype.trim = function() {
|
||||
return this.set(function(v, self) {
|
||||
if ('string' != typeof v) v = self.cast(v);
|
||||
if (v) return v.trim();
|
||||
return v;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a minimum length validator.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var schema = new Schema({ postalCode: { type: String, minlength: 5 })
|
||||
* var Address = db.model('Address', schema)
|
||||
* var address = new Address({ postalCode: '9512' })
|
||||
* address.save(function (err) {
|
||||
* console.error(err) // validator error
|
||||
* address.postalCode = '95125';
|
||||
* address.save() // success
|
||||
* })
|
||||
*
|
||||
* // custom error messages
|
||||
* // We can also use the special {MINLENGTH} token which will be replaced with the minimum allowed length
|
||||
* var minlength = [5, 'The value of path `{PATH}` (`{VALUE}`) is shorter than the minimum allowed length ({MINLENGTH}).'];
|
||||
* var schema = new Schema({ postalCode: { type: String, minlength: minlength })
|
||||
* var Address = mongoose.model('Address', schema);
|
||||
* var address = new Address({ postalCode: '9512' });
|
||||
* address.validate(function (err) {
|
||||
* console.log(String(err)) // ValidationError: The value of path `postalCode` (`9512`) is shorter than the minimum length (5).
|
||||
* })
|
||||
*
|
||||
* @param {Number} value minimum string length
|
||||
* @param {String} [message] optional custom error message
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaString.prototype.minlength = function(value, message) {
|
||||
if (this.minlengthValidator) {
|
||||
this.validators = this.validators.filter(function(v) {
|
||||
return v.validator != this.minlengthValidator;
|
||||
}, this);
|
||||
}
|
||||
|
||||
if (null != value) {
|
||||
var msg = message || errorMessages.String.minlength;
|
||||
msg = msg.replace(/{MINLENGTH}/, value);
|
||||
this.validators.push({
|
||||
validator: this.minlengthValidator = function(v) {
|
||||
return v === null || v.length >= value;
|
||||
},
|
||||
message: msg,
|
||||
type: 'minlength',
|
||||
minlength: value
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a maximum length validator.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var schema = new Schema({ postalCode: { type: String, maxlength: 9 })
|
||||
* var Address = db.model('Address', schema)
|
||||
* var address = new Address({ postalCode: '9512512345' })
|
||||
* address.save(function (err) {
|
||||
* console.error(err) // validator error
|
||||
* address.postalCode = '95125';
|
||||
* address.save() // success
|
||||
* })
|
||||
*
|
||||
* // custom error messages
|
||||
* // We can also use the special {MAXLENGTH} token which will be replaced with the maximum allowed length
|
||||
* var maxlength = [9, 'The value of path `{PATH}` (`{VALUE}`) exceeds the maximum allowed length ({MAXLENGTH}).'];
|
||||
* var schema = new Schema({ postalCode: { type: String, maxlength: maxlength })
|
||||
* var Address = mongoose.model('Address', schema);
|
||||
* var address = new Address({ postalCode: '9512512345' });
|
||||
* address.validate(function (err) {
|
||||
* console.log(String(err)) // ValidationError: The value of path `postalCode` (`9512512345`) exceeds the maximum allowed length (9).
|
||||
* })
|
||||
*
|
||||
* @param {Number} value maximum string length
|
||||
* @param {String} [message] optional custom error message
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaString.prototype.maxlength = function(value, message) {
|
||||
if (this.maxlengthValidator) {
|
||||
this.validators = this.validators.filter(function(v) {
|
||||
return v.validator != this.maxlengthValidator;
|
||||
}, this);
|
||||
}
|
||||
|
||||
if (null != value) {
|
||||
var msg = message || errorMessages.String.maxlength;
|
||||
msg = msg.replace(/{MAXLENGTH}/, value);
|
||||
this.validators.push({
|
||||
validator: this.maxlengthValidator = function(v) {
|
||||
return v === null || v.length <= value;
|
||||
},
|
||||
message: msg,
|
||||
type: 'maxlength',
|
||||
maxlength: value
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a regexp validator.
|
||||
*
|
||||
@ -158,27 +303,57 @@ SchemaString.prototype.trim = function () {
|
||||
*
|
||||
* var s = new Schema({ name: { type: String, match: /^a/ }})
|
||||
* var M = db.model('M', s)
|
||||
* var m = new M({ name: 'invalid' })
|
||||
* var m = new M({ name: 'I am invalid' })
|
||||
* m.validate(function (err) {
|
||||
* console.error(err) // validation error
|
||||
* console.error(String(err)) // "ValidationError: Path `name` is invalid (I am invalid)."
|
||||
* m.name = 'apples'
|
||||
* m.validate(function (err) {
|
||||
* assert.ok(err) // success
|
||||
* })
|
||||
* })
|
||||
*
|
||||
* // using a custom error message
|
||||
* var match = [ /\.html$/, "That file doesn't end in .html ({VALUE})" ];
|
||||
* var s = new Schema({ file: { type: String, match: match }})
|
||||
* var M = db.model('M', s);
|
||||
* var m = new M({ file: 'invalid' });
|
||||
* m.validate(function (err) {
|
||||
* console.log(String(err)) // "ValidationError: That file doesn't end in .html (invalid)"
|
||||
* })
|
||||
*
|
||||
* Empty strings, `undefined`, and `null` values always pass the match validator. If you require these values, enable the `required` validator also.
|
||||
*
|
||||
* var s = new Schema({ name: { type: String, match: /^a/, required: true }})
|
||||
*
|
||||
* @param {RegExp} regExp regular expression to test against
|
||||
* @param {String} [message] optional custom error message
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaString.prototype.match = function match (regExp) {
|
||||
this.validators.push([function(v){
|
||||
return null != v && '' !== v
|
||||
? regExp.test(v)
|
||||
: true
|
||||
}, 'regexp']);
|
||||
SchemaString.prototype.match = function match(regExp, message) {
|
||||
// yes, we allow multiple match validators
|
||||
|
||||
var msg = message || errorMessages.String.match;
|
||||
|
||||
var matchValidator = function(v) {
|
||||
if (!regExp) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var ret = ((null != v && '' !== v)
|
||||
? regExp.test(v)
|
||||
: true);
|
||||
return ret;
|
||||
};
|
||||
|
||||
this.validators.push({
|
||||
validator: matchValidator,
|
||||
message: msg,
|
||||
type: 'regexp',
|
||||
regexp: regExp
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
@ -189,7 +364,7 @@ SchemaString.prototype.match = function match (regExp) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaString.prototype.checkRequired = function checkRequired (value, doc) {
|
||||
SchemaString.prototype.checkRequired = function checkRequired(value, doc) {
|
||||
if (SchemaType._isRef(this, value, doc, true)) {
|
||||
return null != value;
|
||||
} else {
|
||||
@ -203,7 +378,7 @@ SchemaString.prototype.checkRequired = function checkRequired (value, doc) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaString.prototype.cast = function (value, doc, init) {
|
||||
SchemaString.prototype.cast = function(value, doc, init) {
|
||||
if (SchemaType._isRef(this, value, doc, init)) {
|
||||
// wait! we may need to cast this to a document
|
||||
|
||||
@ -237,7 +412,8 @@ SchemaString.prototype.cast = function (value, doc, init) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (value === null) {
|
||||
// If null or undefined
|
||||
if (value == null) {
|
||||
return value;
|
||||
}
|
||||
|
||||
@ -246,12 +422,15 @@ SchemaString.prototype.cast = function (value, doc, init) {
|
||||
if (value._id && 'string' == typeof value._id) {
|
||||
return value._id;
|
||||
}
|
||||
if (value.toString) {
|
||||
|
||||
// Re: gh-647 and gh-3030, we're ok with casting using `toString()`
|
||||
// **unless** its the default Object.toString, because "[object Object]"
|
||||
// doesn't really qualify as useful data
|
||||
if (value.toString && value.toString !== Object.prototype.toString) {
|
||||
return value.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
throw new CastError('string', value, this.path);
|
||||
};
|
||||
|
||||
@ -259,29 +438,30 @@ SchemaString.prototype.cast = function (value, doc, init) {
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function handleSingle (val) {
|
||||
function handleSingle(val) {
|
||||
return this.castForQuery(val);
|
||||
}
|
||||
|
||||
function handleArray (val) {
|
||||
function handleArray(val) {
|
||||
var self = this;
|
||||
return val.map(function (m) {
|
||||
if (!Array.isArray(val)) {
|
||||
return [this.castForQuery(val)];
|
||||
}
|
||||
return val.map(function(m) {
|
||||
return self.castForQuery(m);
|
||||
});
|
||||
}
|
||||
|
||||
SchemaString.prototype.$conditionalHandlers = {
|
||||
'$ne' : handleSingle
|
||||
, '$in' : handleArray
|
||||
, '$nin': handleArray
|
||||
, '$gt' : handleSingle
|
||||
, '$lt' : handleSingle
|
||||
, '$gte': handleSingle
|
||||
, '$lte': handleSingle
|
||||
, '$all': handleArray
|
||||
, '$regex': handleSingle
|
||||
, '$options': handleSingle
|
||||
};
|
||||
SchemaString.prototype.$conditionalHandlers =
|
||||
utils.options(SchemaType.prototype.$conditionalHandlers, {
|
||||
'$all': handleArray,
|
||||
'$gt' : handleSingle,
|
||||
'$gte': handleSingle,
|
||||
'$lt' : handleSingle,
|
||||
'$lte': handleSingle,
|
||||
'$options': handleSingle,
|
||||
'$regex': handleSingle
|
||||
});
|
||||
|
||||
/**
|
||||
* Casts contents for queries.
|
||||
@ -291,7 +471,7 @@ SchemaString.prototype.$conditionalHandlers = {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaString.prototype.castForQuery = function ($conditional, val) {
|
||||
SchemaString.prototype.castForQuery = function($conditional, val) {
|
||||
var handler;
|
||||
if (arguments.length === 2) {
|
||||
handler = this.$conditionalHandlers[$conditional];
|
||||
|
34
node_modules/mongoose/lib/schemadefault.js
generated
vendored
34
node_modules/mongoose/lib/schemadefault.js
generated
vendored
@ -1,34 +0,0 @@
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var Schema = require('./schema')
|
||||
|
||||
/**
|
||||
* Default model for querying the system.profiles collection.
|
||||
*
|
||||
* @property system.profile
|
||||
* @receiver exports
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports['system.profile'] = new Schema({
|
||||
ts: Date
|
||||
, info: String // deprecated
|
||||
, millis: Number
|
||||
, op: String
|
||||
, ns: String
|
||||
, query: Schema.Types.Mixed
|
||||
, updateobj: Schema.Types.Mixed
|
||||
, ntoreturn: Number
|
||||
, nreturned: Number
|
||||
, nscanned: Number
|
||||
, responseLength: Number
|
||||
, client: String
|
||||
, user: String
|
||||
, idhack: Boolean
|
||||
, scanAndOrder: Boolean
|
||||
, keyUpdates: Number
|
||||
, cursorid: Number
|
||||
}, { noVirtualId: true, noId: true });
|
411
node_modules/mongoose/lib/schematype.js
generated
vendored
411
node_modules/mongoose/lib/schematype.js
generated
vendored
@ -3,8 +3,10 @@
|
||||
*/
|
||||
|
||||
var utils = require('./utils');
|
||||
var CastError = require('./error').CastError;
|
||||
var ValidatorError = require('./error').ValidatorError;
|
||||
var error = require('./error');
|
||||
var errorMessages = error.messages;
|
||||
var CastError = error.CastError;
|
||||
var ValidatorError = error.ValidatorError;
|
||||
|
||||
/**
|
||||
* SchemaType constructor
|
||||
@ -15,7 +17,7 @@ var ValidatorError = require('./error').ValidatorError;
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function SchemaType (path, options, instance) {
|
||||
function SchemaType(path, options, instance) {
|
||||
this.path = path;
|
||||
this.instance = instance;
|
||||
this.validators = [];
|
||||
@ -25,17 +27,19 @@ function SchemaType (path, options, instance) {
|
||||
this._index = null;
|
||||
this.selected;
|
||||
|
||||
for (var i in options) if (this[i] && 'function' == typeof this[i]) {
|
||||
// { unique: true, index: true }
|
||||
if ('index' == i && this._index) continue;
|
||||
for (var i in options) {
|
||||
if (this[i] && 'function' == typeof this[i]) {
|
||||
// { unique: true, index: true }
|
||||
if ('index' == i && this._index) continue;
|
||||
|
||||
var opts = Array.isArray(options[i])
|
||||
? options[i]
|
||||
: [options[i]];
|
||||
var opts = Array.isArray(options[i])
|
||||
? options[i]
|
||||
: [options[i]];
|
||||
|
||||
this[i].apply(this, opts);
|
||||
this[i].apply(this, opts);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a default value for this SchemaType.
|
||||
@ -52,17 +56,35 @@ function SchemaType (path, options, instance) {
|
||||
* ####Example:
|
||||
*
|
||||
* // values are cast:
|
||||
* var schema = new Schema({ aNumber: Number, default: "4.815162342" })
|
||||
* var schema = new Schema({ aNumber: { type: Number, default: 4.815162342 }})
|
||||
* var M = db.model('M', schema)
|
||||
* var m = new M;
|
||||
* console.log(m.aNumber, typeof m.aNumber) // 4.815162342 "number"
|
||||
* console.log(m.aNumber) // 4.815162342
|
||||
*
|
||||
* // default unique objects for Mixed types:
|
||||
* var schema = new Schema({ mixed: Schema.Types.Mixed });
|
||||
* schema.path('mixed').default(function () {
|
||||
* return {};
|
||||
* });
|
||||
*
|
||||
* // if we don't use a function to return object literals for Mixed defaults,
|
||||
* // each document will receive a reference to the same object literal creating
|
||||
* // a "shared" object instance:
|
||||
* var schema = new Schema({ mixed: Schema.Types.Mixed });
|
||||
* schema.path('mixed').default({});
|
||||
* var M = db.model('M', schema);
|
||||
* var m1 = new M;
|
||||
* m1.mixed.added = 1;
|
||||
* console.log(m1.mixed); // { added: 1 }
|
||||
* var m2 = new M;
|
||||
* console.log(m2.mixed); // { added: 1 }
|
||||
*
|
||||
* @param {Function|any} val the default value
|
||||
* @return {defaultValue}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaType.prototype.default = function (val) {
|
||||
SchemaType.prototype.default = function(val) {
|
||||
if (1 === arguments.length) {
|
||||
this.defaultValue = typeof val === 'function'
|
||||
? val
|
||||
@ -99,7 +121,7 @@ SchemaType.prototype.default = function (val) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaType.prototype.index = function (options) {
|
||||
SchemaType.prototype.index = function(options) {
|
||||
this._index = options;
|
||||
utils.expires(this._index);
|
||||
return this;
|
||||
@ -110,7 +132,7 @@ SchemaType.prototype.index = function (options) {
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var s = new Schema({ name: { type: String, unique: true })
|
||||
* var s = new Schema({ name: { type: String, unique: true }});
|
||||
* Schema.path('name').index({ unique: true });
|
||||
*
|
||||
* _NOTE: violating the constraint returns an `E11000` error from MongoDB when saving, not a Mongoose validation error._
|
||||
@ -120,7 +142,7 @@ SchemaType.prototype.index = function (options) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaType.prototype.unique = function (bool) {
|
||||
SchemaType.prototype.unique = function(bool) {
|
||||
if (null == this._index || 'boolean' == typeof this._index) {
|
||||
this._index = {};
|
||||
} else if ('string' == typeof this._index) {
|
||||
@ -131,6 +153,29 @@ SchemaType.prototype.unique = function (bool) {
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Declares a full text index.
|
||||
*
|
||||
* ###Example:
|
||||
*
|
||||
* var s = new Schema({name : {type: String, text : true })
|
||||
* Schema.path('name').index({text : true});
|
||||
* @param bool
|
||||
* @return {SchemaType} this
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaType.prototype.text = function(bool) {
|
||||
if (null == this._index || 'boolean' == typeof this._index) {
|
||||
this._index = {};
|
||||
} else if ('string' == typeof this._index) {
|
||||
this._index = { type: this._index };
|
||||
}
|
||||
|
||||
this._index.text = bool;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Declares a sparse index.
|
||||
*
|
||||
@ -144,7 +189,7 @@ SchemaType.prototype.unique = function (bool) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaType.prototype.sparse = function (bool) {
|
||||
SchemaType.prototype.sparse = function(bool) {
|
||||
if (null == this._index || 'boolean' == typeof this._index) {
|
||||
this._index = {};
|
||||
} else if ('string' == typeof this._index) {
|
||||
@ -228,7 +273,7 @@ SchemaType.prototype.sparse = function (bool) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaType.prototype.set = function (fn) {
|
||||
SchemaType.prototype.set = function(fn) {
|
||||
if ('function' != typeof fn)
|
||||
throw new TypeError('A setter must be a function.');
|
||||
this.setters.push(fn);
|
||||
@ -297,7 +342,7 @@ SchemaType.prototype.set = function (fn) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaType.prototype.get = function (fn) {
|
||||
SchemaType.prototype.get = function(fn) {
|
||||
if ('function' != typeof fn)
|
||||
throw new TypeError('A getter must be a function.');
|
||||
this.getters.push(fn);
|
||||
@ -307,43 +352,60 @@ SchemaType.prototype.get = function (fn) {
|
||||
/**
|
||||
* Adds validator(s) for this document path.
|
||||
*
|
||||
* Validators always receive the value to validate as their first argument and must return `Boolean`. Returning false is interpreted as validation failure.
|
||||
* Validators always receive the value to validate as their first argument and must return `Boolean`. Returning `false` means validation failed.
|
||||
*
|
||||
* The error message argument is optional. If not passed, the [default generic error message template](#error_messages_MongooseError-messages) will be used.
|
||||
*
|
||||
* ####Examples:
|
||||
*
|
||||
* // make sure every value is equal to "something"
|
||||
* function validator (val) {
|
||||
* return val == 'something';
|
||||
* }
|
||||
*
|
||||
* new Schema({ name: { type: String, validate: validator }});
|
||||
*
|
||||
* // with a custom error message
|
||||
*
|
||||
* var custom = [validator, 'validation failed']
|
||||
* var custom = [validator, 'Uh oh, {PATH} does not equal "something".']
|
||||
* new Schema({ name: { type: String, validate: custom }});
|
||||
*
|
||||
* // adding many validators at a time
|
||||
*
|
||||
* var many = [
|
||||
* { validator: validator, msg: 'uh oh' }
|
||||
* , { validator: fn, msg: 'failed' }
|
||||
* , { validator: anotherValidator, msg: 'failed' }
|
||||
* ]
|
||||
* new Schema({ name: { type: String, validate: many }});
|
||||
*
|
||||
* // or utilizing SchemaType methods directly:
|
||||
*
|
||||
* var schema = new Schema({ name: 'string' });
|
||||
* schema.path('name').validate(validator, 'validation failed');
|
||||
* schema.path('name').validate(validator, 'validation of `{PATH}` failed with value `{VALUE}`');
|
||||
*
|
||||
* ####Error message templates:
|
||||
*
|
||||
* From the examples above, you may have noticed that error messages support baseic templating. There are a few other template keywords besides `{PATH}` and `{VALUE}` too. To find out more, details are available [here](#error_messages_MongooseError-messages)
|
||||
*
|
||||
* ####Asynchronous validation:
|
||||
*
|
||||
* Passing a validator function that receives two arguments tells mongoose that the validator is an asynchronous validator. The second argument is an callback function that must be passed either `true` or `false` when validation is complete.
|
||||
* Passing a validator function that receives two arguments tells mongoose that the validator is an asynchronous validator. The first argument passed to the validator function is the value being validated. The second argument is a callback function that must called when you finish validating the value and passed either `true` or `false` to communicate either success or failure respectively.
|
||||
*
|
||||
* schema.path('name').validate(function (value, respond) {
|
||||
* doStuff(value, function () {
|
||||
* ...
|
||||
* respond(false); // validation failed
|
||||
* })
|
||||
* }, 'my error type');
|
||||
*
|
||||
* }, '{PATH} failed validation.');
|
||||
*
|
||||
* // or with dynamic message
|
||||
*
|
||||
* schema.path('name').validate(function (value, respond) {
|
||||
* doStuff(value, function () {
|
||||
* ...
|
||||
* respond(false, 'this message gets to the validation error');
|
||||
* });
|
||||
* }, 'this message does not matter');
|
||||
*
|
||||
* You might use asynchronous validators to retreive other documents from the database to validate against or to meet other I/O bound validation needs.
|
||||
*
|
||||
* Validation occurs `pre('save')` or whenever you manually execute [document#validate](#document_Document-validate).
|
||||
@ -363,71 +425,113 @@ SchemaType.prototype.get = function (fn) {
|
||||
* Product.on('error', handleError);
|
||||
*
|
||||
* @param {RegExp|Function|Object} obj validator
|
||||
* @param {String} [error] optional error message
|
||||
* @param {String} [errorMsg] optional error message
|
||||
* @param {String} [type] optional validator type
|
||||
* @return {SchemaType} this
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaType.prototype.validate = function (obj, error) {
|
||||
if ('function' == typeof obj || obj && 'RegExp' === obj.constructor.name) {
|
||||
this.validators.push([obj, error]);
|
||||
SchemaType.prototype.validate = function(obj, message, type) {
|
||||
if ('function' == typeof obj || obj && 'RegExp' === utils.getFunctionName(obj.constructor)) {
|
||||
var properties;
|
||||
if (message instanceof Object && !type) {
|
||||
properties = utils.clone(message);
|
||||
if (!properties.message) {
|
||||
properties.message = properties.msg;
|
||||
}
|
||||
properties.validator = obj;
|
||||
properties.type = properties.type || 'user defined';
|
||||
} else {
|
||||
if (!message) message = errorMessages.general.default;
|
||||
if (!type) type = 'user defined';
|
||||
properties = { message: message, type: type, validator: obj };
|
||||
}
|
||||
this.validators.push(properties);
|
||||
return this;
|
||||
}
|
||||
|
||||
var i = arguments.length
|
||||
, arg
|
||||
var i,
|
||||
length,
|
||||
arg;
|
||||
|
||||
while (i--) {
|
||||
for (i = 0, length = arguments.length; i < length; i++) {
|
||||
arg = arguments[i];
|
||||
if (!(arg && 'Object' == arg.constructor.name)) {
|
||||
if (!(arg && 'Object' === utils.getFunctionName(arg.constructor))) {
|
||||
var msg = 'Invalid validator. Received (' + typeof arg + ') '
|
||||
+ arg
|
||||
+ '. See http://mongoosejs.com/docs/api.html#schematype_SchemaType-validate';
|
||||
|
||||
throw new Error(msg);
|
||||
}
|
||||
this.validate(arg.validator, arg.msg);
|
||||
this.validate(arg.validator, arg);
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds a required validator to this schematype.
|
||||
* Adds a required validator to this schematype. The required validator is added
|
||||
* to the front of the validators array using `unshift()`.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var s = new Schema({ born: { type: Date, required: true })
|
||||
* // or
|
||||
*
|
||||
* // or with custom error message
|
||||
*
|
||||
* var s = new Schema({ born: { type: Date, required: '{PATH} is required!' })
|
||||
*
|
||||
* // or through the path API
|
||||
*
|
||||
* Schema.path('name').required(true);
|
||||
*
|
||||
* // with custom error messaging
|
||||
*
|
||||
* Schema.path('name').required(true, 'grrr :( ');
|
||||
*
|
||||
*
|
||||
* @param {Boolean} required enable/disable the validator
|
||||
* @param {String} [message] optional custom error message
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaType.prototype.required = function (required) {
|
||||
var self = this;
|
||||
SchemaType.prototype.required = function(required, message) {
|
||||
if (false === required) {
|
||||
this.validators = this.validators.filter(function(v) {
|
||||
return v.validator != this.requiredValidator;
|
||||
}, this);
|
||||
|
||||
function __checkRequired (v) {
|
||||
this.isRequired = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
var self = this;
|
||||
this.isRequired = true;
|
||||
|
||||
this.requiredValidator = function(v) {
|
||||
// in here, `this` refers to the validating document.
|
||||
// no validation when this path wasn't selected in the query.
|
||||
if ('isSelected' in this &&
|
||||
!this.isSelected(self.path) &&
|
||||
!this.isModified(self.path)) return true;
|
||||
return self.checkRequired(v, this);
|
||||
|
||||
return (('function' === typeof required) && !required.apply(this)) ||
|
||||
self.checkRequired(v, this);
|
||||
};
|
||||
|
||||
if ('string' == typeof required) {
|
||||
message = required;
|
||||
required = undefined;
|
||||
}
|
||||
|
||||
if (false === required) {
|
||||
this.isRequired = false;
|
||||
this.validators = this.validators.filter(function (v) {
|
||||
return v[0].name !== '__checkRequired';
|
||||
});
|
||||
} else {
|
||||
this.isRequired = true;
|
||||
this.validators.push([__checkRequired, 'required']);
|
||||
}
|
||||
var msg = message || errorMessages.general.required;
|
||||
this.validators.unshift({
|
||||
validator: this.requiredValidator,
|
||||
message: msg,
|
||||
type: 'required'
|
||||
});
|
||||
|
||||
return this;
|
||||
};
|
||||
@ -440,7 +544,7 @@ SchemaType.prototype.required = function (required) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaType.prototype.getDefault = function (scope, init) {
|
||||
SchemaType.prototype.getDefault = function(scope, init) {
|
||||
var ret = 'function' === typeof this.defaultValue
|
||||
? this.defaultValue.call(scope)
|
||||
: this.defaultValue;
|
||||
@ -461,26 +565,24 @@ SchemaType.prototype.getDefault = function (scope, init) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaType.prototype.applySetters = function (value, scope, init, priorVal) {
|
||||
if (SchemaType._isRef(this, value, scope, init)) {
|
||||
return init
|
||||
? value
|
||||
: this.cast(value, scope, init, priorVal);
|
||||
}
|
||||
|
||||
var v = value
|
||||
, setters = this.setters
|
||||
, len = setters.length
|
||||
|
||||
if (!len) {
|
||||
if (null === v || undefined === v) return v;
|
||||
return this.cast(v, scope, init, priorVal)
|
||||
}
|
||||
SchemaType.prototype.applySetters = function(value, scope, init, priorVal) {
|
||||
var v = value,
|
||||
setters = this.setters,
|
||||
len = setters.length,
|
||||
caster = this.caster;
|
||||
|
||||
while (len--) {
|
||||
v = setters[len].call(scope, v, this);
|
||||
}
|
||||
|
||||
if (Array.isArray(v) && caster && caster.setters) {
|
||||
var newVal = [];
|
||||
for (var i = 0; i < v.length; i++) {
|
||||
newVal.push(caster.applySetters(v[i], scope, init, priorVal));
|
||||
}
|
||||
v = newVal;
|
||||
}
|
||||
|
||||
if (null === v || undefined === v) return v;
|
||||
|
||||
// do not cast until all setters are applied #665
|
||||
@ -497,12 +599,10 @@ SchemaType.prototype.applySetters = function (value, scope, init, priorVal) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaType.prototype.applyGetters = function (value, scope) {
|
||||
if (SchemaType._isRef(this, value, scope, true)) return value;
|
||||
|
||||
var v = value
|
||||
, getters = this.getters
|
||||
, len = getters.length;
|
||||
SchemaType.prototype.applyGetters = function(value, scope) {
|
||||
var v = value,
|
||||
getters = this.getters,
|
||||
len = getters.length;
|
||||
|
||||
if (!len) {
|
||||
return v;
|
||||
@ -532,10 +632,10 @@ SchemaType.prototype.applyGetters = function (value, scope) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaType.prototype.select = function select (val) {
|
||||
this.selected = !! val;
|
||||
SchemaType.prototype.select = function select(val) {
|
||||
this.selected = !!val;
|
||||
return this;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Performs a validation of `value` using the validators declared for this SchemaType.
|
||||
@ -546,40 +646,111 @@ SchemaType.prototype.select = function select (val) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaType.prototype.doValidate = function (value, fn, scope) {
|
||||
var err = false
|
||||
, path = this.path
|
||||
, count = this.validators.length;
|
||||
SchemaType.prototype.doValidate = function(value, fn, scope) {
|
||||
var err = false,
|
||||
path = this.path,
|
||||
count = this.validators.length;
|
||||
|
||||
if (!count) return fn(null);
|
||||
|
||||
function validate (ok, msg, val) {
|
||||
var validate = function(ok, validatorProperties) {
|
||||
if (err) return;
|
||||
if (ok === undefined || ok) {
|
||||
--count || fn(null);
|
||||
} else {
|
||||
fn(err = new ValidatorError(path, msg, val));
|
||||
err = new ValidatorError(validatorProperties);
|
||||
fn(err);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.validators.forEach(function (v) {
|
||||
var validator = v[0]
|
||||
, message = v[1];
|
||||
var self = this;
|
||||
this.validators.forEach(function(v) {
|
||||
if (err) {
|
||||
return;
|
||||
}
|
||||
|
||||
var validator = v.validator;
|
||||
|
||||
var validatorProperties = utils.clone(v);
|
||||
validatorProperties.path = path;
|
||||
validatorProperties.value = value;
|
||||
|
||||
if (validator instanceof RegExp) {
|
||||
validate(validator.test(value), message, value);
|
||||
validate(validator.test(value), validatorProperties);
|
||||
} else if ('function' === typeof validator) {
|
||||
if (value === undefined && !self.isRequired) {
|
||||
validate(true, validatorProperties);
|
||||
return;
|
||||
}
|
||||
if (2 === validator.length) {
|
||||
validator.call(scope, value, function (ok) {
|
||||
validate(ok, message, value);
|
||||
validator.call(scope, value, function(ok, customMsg) {
|
||||
if (customMsg) {
|
||||
validatorProperties.message = customMsg;
|
||||
}
|
||||
validate(ok, validatorProperties);
|
||||
});
|
||||
} else {
|
||||
validate(validator.call(scope, value), message, value);
|
||||
validate(validator.call(scope, value), validatorProperties);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Performs a validation of `value` using the validators declared for this SchemaType.
|
||||
*
|
||||
* ####Note:
|
||||
*
|
||||
* This method ignores the asynchronous validators.
|
||||
*
|
||||
* @param {any} value
|
||||
* @param {Object} scope
|
||||
* @return {MongooseError|undefined}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaType.prototype.doValidateSync = function(value, scope) {
|
||||
var err = null,
|
||||
path = this.path,
|
||||
count = this.validators.length;
|
||||
|
||||
if (!count) return null;
|
||||
|
||||
var validate = function(ok, validatorProperties) {
|
||||
if (err) return;
|
||||
if (ok !== undefined && !ok) {
|
||||
err = new ValidatorError(validatorProperties);
|
||||
}
|
||||
};
|
||||
|
||||
var self = this;
|
||||
if (value === undefined && !self.isRequired) {
|
||||
return null;
|
||||
}
|
||||
|
||||
this.validators.forEach(function(v) {
|
||||
if (err) {
|
||||
return;
|
||||
}
|
||||
|
||||
var validator = v.validator;
|
||||
var validatorProperties = utils.clone(v);
|
||||
validatorProperties.path = path;
|
||||
validatorProperties.value = value;
|
||||
|
||||
if (validator instanceof RegExp) {
|
||||
validate(validator.test(value), validatorProperties);
|
||||
} else if ('function' === typeof validator) {
|
||||
// if not async validators
|
||||
if (2 !== validator.length) {
|
||||
validate(validator.call(scope, value), validatorProperties);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return err;
|
||||
};
|
||||
|
||||
/**
|
||||
* Determines if value is a valid Reference.
|
||||
*
|
||||
@ -591,7 +762,7 @@ SchemaType.prototype.doValidate = function (value, fn, scope) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaType._isRef = function (self, value, doc, init) {
|
||||
SchemaType._isRef = function(self, value, doc, init) {
|
||||
// fast path
|
||||
var ref = init && self.options && self.options.ref;
|
||||
|
||||
@ -615,8 +786,64 @@ SchemaType._isRef = function (self, value, doc, init) {
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function handleSingle(val) {
|
||||
return this.castForQuery(val);
|
||||
}
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function handleArray(val) {
|
||||
var _this = this;
|
||||
if (!Array.isArray(val)) {
|
||||
return [this.castForQuery(val)];
|
||||
}
|
||||
return val.map(function(m) {
|
||||
return _this.castForQuery(m);
|
||||
});
|
||||
}
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaType.prototype.$conditionalHandlers = {
|
||||
'$all': handleArray,
|
||||
'$eq': handleSingle,
|
||||
'$in' : handleArray,
|
||||
'$ne' : handleSingle,
|
||||
'$nin': handleArray
|
||||
};
|
||||
|
||||
/**
|
||||
* Cast the given value with the given optional query operator.
|
||||
*
|
||||
* @param {String} [$conditional] query operator, like `$eq` or `$in`
|
||||
* @param {any} val
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaType.prototype.castForQuery = function($conditional, val) {
|
||||
var handler;
|
||||
if (arguments.length === 2) {
|
||||
handler = this.$conditionalHandlers[$conditional];
|
||||
if (!handler) {
|
||||
throw new Error('Can\'t use ' + $conditional);
|
||||
}
|
||||
return handler.call(this, val);
|
||||
} else {
|
||||
val = $conditional;
|
||||
return this.cast(val);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
199
node_modules/mongoose/lib/services/updateValidators.js
generated
vendored
Normal file
199
node_modules/mongoose/lib/services/updateValidators.js
generated
vendored
Normal file
@ -0,0 +1,199 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var async = require('async');
|
||||
var ValidationError = require('../error/validation.js');
|
||||
var ObjectId = require('../types/objectid');
|
||||
|
||||
/**
|
||||
* Applies validators and defaults to update and findOneAndUpdate operations,
|
||||
* specifically passing a null doc as `this` to validators and defaults
|
||||
*
|
||||
* @param {Query} query
|
||||
* @param {Schema} schema
|
||||
* @param {Object} castedDoc
|
||||
* @param {Object} options
|
||||
* @method runValidatorsOnUpdate
|
||||
* @api private
|
||||
*/
|
||||
|
||||
module.exports = function(query, schema, castedDoc, options) {
|
||||
var keys = Object.keys(castedDoc || {});
|
||||
var updatedKeys = {};
|
||||
var updatedValues = {};
|
||||
var numKeys = keys.length;
|
||||
var hasDollarUpdate = false;
|
||||
var modified = {};
|
||||
|
||||
for (var i = 0; i < numKeys; ++i) {
|
||||
if (keys[i].charAt(0) === '$') {
|
||||
modifiedPaths(castedDoc[keys[i]], '', modified);
|
||||
var flat = flatten(castedDoc[keys[i]]);
|
||||
var paths = Object.keys(flat);
|
||||
var numPaths = paths.length;
|
||||
for (var j = 0; j < numPaths; ++j) {
|
||||
var updatedPath = paths[j].replace('.$.', '.0.');
|
||||
updatedPath = updatedPath.replace(/\.\$$/, '.0');
|
||||
if (keys[i] === '$set' || keys[i] === '$setOnInsert') {
|
||||
updatedValues[updatedPath] = flat[paths[j]];
|
||||
} else if (keys[i] === '$unset') {
|
||||
updatedValues[updatedPath] = undefined;
|
||||
}
|
||||
updatedKeys[updatedPath] = true;
|
||||
}
|
||||
hasDollarUpdate = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasDollarUpdate) {
|
||||
modifiedPaths(castedDoc, '', modified);
|
||||
updatedValues = flatten(castedDoc);
|
||||
updatedKeys = Object.keys(updatedValues);
|
||||
}
|
||||
|
||||
if (options && options.upsert) {
|
||||
paths = Object.keys(query._conditions);
|
||||
numPaths = keys.length;
|
||||
for (i = 0; i < numPaths; ++i) {
|
||||
var path = paths[i];
|
||||
var condition = query._conditions[path];
|
||||
if (condition && typeof condition === 'object') {
|
||||
var conditionKeys = Object.keys(condition);
|
||||
var numConditionKeys = conditionKeys.length;
|
||||
var hasDollarKey = false;
|
||||
for (j = 0; j < numConditionKeys; ++j) {
|
||||
if (conditionKeys[j].charAt(0) === '$') {
|
||||
hasDollarKey = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (hasDollarKey) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
updatedKeys[path] = true;
|
||||
modified[path] = true;
|
||||
}
|
||||
|
||||
if (options.setDefaultsOnInsert) {
|
||||
schema.eachPath(function(path, schemaType) {
|
||||
if (path === '_id') {
|
||||
// Ignore _id for now because it causes bugs in 2.4
|
||||
return;
|
||||
}
|
||||
if (schemaType.$isSingleNested) {
|
||||
// Only handle nested schemas 1-level deep to avoid infinite
|
||||
// recursion re: https://github.com/mongodb-js/mongoose-autopopulate/issues/11
|
||||
schemaType.schema.eachPath(function(_path, _schemaType) {
|
||||
if (path === '_id') {
|
||||
// Ignore _id for now because it causes bugs in 2.4
|
||||
return;
|
||||
}
|
||||
|
||||
var def = _schemaType.getDefault(null, true);
|
||||
if (!modified[path + '.' + _path] && typeof def !== 'undefined') {
|
||||
castedDoc.$setOnInsert = castedDoc.$setOnInsert || {};
|
||||
castedDoc.$setOnInsert[path + '.' + _path] = def;
|
||||
updatedValues[path + '.' + _path] = def;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
var def = schemaType.getDefault(null, true);
|
||||
if (!modified[path] && typeof def !== 'undefined') {
|
||||
castedDoc.$setOnInsert = castedDoc.$setOnInsert || {};
|
||||
castedDoc.$setOnInsert[path] = def;
|
||||
updatedValues[path] = def;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var updates = Object.keys(updatedValues);
|
||||
var numUpdates = updates.length;
|
||||
var validatorsToExecute = [];
|
||||
var validationErrors = [];
|
||||
for (i = 0; i < numUpdates; ++i) {
|
||||
(function(i) {
|
||||
var schemaPath = schema._getSchema(updates[i]);
|
||||
if (schemaPath) {
|
||||
validatorsToExecute.push(function(callback) {
|
||||
schemaPath.doValidate(
|
||||
updatedValues[updates[i]],
|
||||
function(err) {
|
||||
if (err) {
|
||||
err.path = updates[i];
|
||||
validationErrors.push(err);
|
||||
}
|
||||
callback(null);
|
||||
},
|
||||
options && options.context === 'query' ? query : null);
|
||||
});
|
||||
}
|
||||
})(i);
|
||||
}
|
||||
|
||||
return function(callback) {
|
||||
async.parallel(validatorsToExecute, function() {
|
||||
if (validationErrors.length) {
|
||||
var err = new ValidationError(null);
|
||||
for (var i = 0; i < validationErrors.length; ++i) {
|
||||
err.errors[validationErrors[i].path] = validationErrors[i];
|
||||
}
|
||||
return callback(err);
|
||||
}
|
||||
callback(null);
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
function modifiedPaths(update, path, result) {
|
||||
var keys = Object.keys(update);
|
||||
var numKeys = keys.length;
|
||||
result = result || {};
|
||||
path = path ? path + '.' : '';
|
||||
|
||||
for (var i = 0; i < numKeys; ++i) {
|
||||
var key = keys[i];
|
||||
var val = update[key];
|
||||
|
||||
result[path + key] = true;
|
||||
if (shouldFlatten(val)) {
|
||||
modifiedPaths(val, path + key, result);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function flatten(update, path) {
|
||||
var keys = Object.keys(update);
|
||||
var numKeys = keys.length;
|
||||
var result = {};
|
||||
path = path ? path + '.' : '';
|
||||
|
||||
for (var i = 0; i < numKeys; ++i) {
|
||||
var key = keys[i];
|
||||
var val = update[key];
|
||||
if (shouldFlatten(val)) {
|
||||
var flat = flatten(val, path + key);
|
||||
for (var k in flat) {
|
||||
result[k] = flat[k];
|
||||
}
|
||||
} else {
|
||||
result[path + key] = val;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function shouldFlatten(val) {
|
||||
return val &&
|
||||
typeof val === 'object' &&
|
||||
!(val instanceof Date) &&
|
||||
!(val instanceof ObjectId) &&
|
||||
(!Array.isArray(val) || val.length > 0) &&
|
||||
!(val instanceof Buffer);
|
||||
}
|
69
node_modules/mongoose/lib/statemachine.js
generated
vendored
69
node_modules/mongoose/lib/statemachine.js
generated
vendored
@ -12,10 +12,8 @@ var utils = require('./utils');
|
||||
* @api private
|
||||
*/
|
||||
|
||||
var StateMachine = module.exports = exports = function StateMachine () {
|
||||
this.paths = {};
|
||||
this.states = {};
|
||||
}
|
||||
var StateMachine = module.exports = exports = function StateMachine() {
|
||||
};
|
||||
|
||||
/*!
|
||||
* StateMachine.ctor('state1', 'state2', ...)
|
||||
@ -31,15 +29,17 @@ var StateMachine = module.exports = exports = function StateMachine () {
|
||||
* @private
|
||||
*/
|
||||
|
||||
StateMachine.ctor = function () {
|
||||
StateMachine.ctor = function() {
|
||||
var states = utils.args(arguments);
|
||||
|
||||
var ctor = function () {
|
||||
var ctor = function() {
|
||||
StateMachine.apply(this, arguments);
|
||||
this.paths = {};
|
||||
this.states = {};
|
||||
this.stateNames = states;
|
||||
|
||||
var i = states.length
|
||||
, state;
|
||||
var i = states.length,
|
||||
state;
|
||||
|
||||
while (i--) {
|
||||
state = states[i];
|
||||
@ -47,13 +47,13 @@ StateMachine.ctor = function () {
|
||||
}
|
||||
};
|
||||
|
||||
ctor.prototype.__proto__ = StateMachine.prototype;
|
||||
ctor.prototype = new StateMachine();
|
||||
|
||||
states.forEach(function (state) {
|
||||
states.forEach(function(state) {
|
||||
// Changes the `path`'s state to `state`.
|
||||
ctor.prototype[state] = function (path) {
|
||||
ctor.prototype[state] = function(path) {
|
||||
this._changeState(path, state);
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
return ctor;
|
||||
@ -69,29 +69,29 @@ StateMachine.ctor = function () {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
StateMachine.prototype._changeState = function _changeState (path, nextState) {
|
||||
StateMachine.prototype._changeState = function _changeState(path, nextState) {
|
||||
var prevBucket = this.states[this.paths[path]];
|
||||
if (prevBucket) delete prevBucket[path];
|
||||
|
||||
this.paths[path] = nextState;
|
||||
this.states[nextState][path] = true;
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
StateMachine.prototype.clear = function clear (state) {
|
||||
var keys = Object.keys(this.states[state])
|
||||
, i = keys.length
|
||||
, path
|
||||
StateMachine.prototype.clear = function clear(state) {
|
||||
var keys = Object.keys(this.states[state]),
|
||||
i = keys.length,
|
||||
path;
|
||||
|
||||
while (i--) {
|
||||
path = keys[i];
|
||||
delete this.states[state][path];
|
||||
delete this.paths[path];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Checks to see if at least one path is in the states passed in via `arguments`
|
||||
@ -101,13 +101,13 @@ StateMachine.prototype.clear = function clear (state) {
|
||||
* @private
|
||||
*/
|
||||
|
||||
StateMachine.prototype.some = function some () {
|
||||
StateMachine.prototype.some = function some() {
|
||||
var self = this;
|
||||
var what = arguments.length ? arguments : this.stateNames;
|
||||
return Array.prototype.some.call(what, function (state) {
|
||||
return Array.prototype.some.call(what, function(state) {
|
||||
return Object.keys(self.states[state]).length;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* This function builds the functions that get assigned to `forEach` and `map`,
|
||||
@ -118,25 +118,25 @@ StateMachine.prototype.some = function some () {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
StateMachine.prototype._iter = function _iter (iterMethod) {
|
||||
return function () {
|
||||
var numArgs = arguments.length
|
||||
, states = utils.args(arguments, 0, numArgs-1)
|
||||
, callback = arguments[numArgs-1];
|
||||
StateMachine.prototype._iter = function _iter(iterMethod) {
|
||||
return function() {
|
||||
var numArgs = arguments.length,
|
||||
states = utils.args(arguments, 0, numArgs - 1),
|
||||
callback = arguments[numArgs - 1];
|
||||
|
||||
if (!states.length) states = this.stateNames;
|
||||
|
||||
var self = this;
|
||||
|
||||
var paths = states.reduce(function (paths, state) {
|
||||
var paths = states.reduce(function(paths, state) {
|
||||
return paths.concat(Object.keys(self.states[state]));
|
||||
}, []);
|
||||
|
||||
return paths[iterMethod](function (path, i, paths) {
|
||||
return paths[iterMethod](function(path, i, paths) {
|
||||
return callback(path, i, paths);
|
||||
});
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Iterates over the paths that belong to one of the parameter states.
|
||||
@ -152,10 +152,10 @@ StateMachine.prototype._iter = function _iter (iterMethod) {
|
||||
* @private
|
||||
*/
|
||||
|
||||
StateMachine.prototype.forEach = function forEach () {
|
||||
StateMachine.prototype.forEach = function forEach() {
|
||||
this.forEach = this._iter('forEach');
|
||||
return this.forEach.apply(this, arguments);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Maps over the paths that belong to one of the parameter states.
|
||||
@ -172,8 +172,7 @@ StateMachine.prototype.forEach = function forEach () {
|
||||
* @private
|
||||
*/
|
||||
|
||||
StateMachine.prototype.map = function map () {
|
||||
StateMachine.prototype.map = function map() {
|
||||
this.map = this._iter('map');
|
||||
return this.map.apply(this, arguments);
|
||||
}
|
||||
|
||||
};
|
||||
|
1233
node_modules/mongoose/lib/types/array.js
generated
vendored
1233
node_modules/mongoose/lib/types/array.js
generated
vendored
File diff suppressed because it is too large
Load Diff
209
node_modules/mongoose/lib/types/buffer.js
generated
vendored
209
node_modules/mongoose/lib/types/buffer.js
generated
vendored
@ -1,15 +1,9 @@
|
||||
|
||||
/*!
|
||||
* Access driver.
|
||||
*/
|
||||
|
||||
var driver = global.MONGOOSE_DRIVER_PATH || '../drivers/node-mongodb-native';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var Binary = require(driver + '/binary');
|
||||
var Binary = require('../drivers').Binary,
|
||||
utils = require('../utils');
|
||||
|
||||
/**
|
||||
* Mongoose Buffer constructor.
|
||||
@ -24,7 +18,7 @@ var Binary = require(driver + '/binary');
|
||||
* @see http://bit.ly/f6CnZU
|
||||
*/
|
||||
|
||||
function MongooseBuffer (value, encode, offset) {
|
||||
function MongooseBuffer(value, encode, offset) {
|
||||
var length = arguments.length;
|
||||
var val;
|
||||
|
||||
@ -47,95 +41,118 @@ function MongooseBuffer (value, encode, offset) {
|
||||
}
|
||||
|
||||
var buf = new Buffer(val, encoding, offset);
|
||||
buf.__proto__ = MongooseBuffer.prototype;
|
||||
utils.decorate( buf, MongooseBuffer.mixin );
|
||||
buf.isMongooseBuffer = true;
|
||||
|
||||
// make sure these internal props don't show up in Object.keys()
|
||||
Object.defineProperties(buf, {
|
||||
validators: { value: [] }
|
||||
, _path: { value: path }
|
||||
, _parent: { value: doc }
|
||||
validators: { value: [] },
|
||||
_path: { value: path },
|
||||
_parent: { value: doc }
|
||||
});
|
||||
|
||||
if (doc && "string" === typeof path) {
|
||||
Object.defineProperty(buf, '_schema', {
|
||||
value: doc.schema.path(path)
|
||||
value: doc.schema.path(path)
|
||||
});
|
||||
}
|
||||
|
||||
buf._subtype = 0;
|
||||
return buf;
|
||||
};
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherit from Buffer.
|
||||
*/
|
||||
|
||||
MongooseBuffer.prototype = new Buffer(0);
|
||||
//MongooseBuffer.prototype = new Buffer(0);
|
||||
|
||||
/**
|
||||
* Parent owner document
|
||||
*
|
||||
* @api private
|
||||
* @property _parent
|
||||
*/
|
||||
MongooseBuffer.mixin = {
|
||||
|
||||
MongooseBuffer.prototype._parent;
|
||||
/**
|
||||
* Parent owner document
|
||||
*
|
||||
* @api private
|
||||
* @property _parent
|
||||
* @receiver MongooseBuffer
|
||||
*/
|
||||
|
||||
/**
|
||||
* Marks this buffer as modified.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
_parent: undefined,
|
||||
|
||||
MongooseBuffer.prototype._markModified = function () {
|
||||
var parent = this._parent;
|
||||
/**
|
||||
* Default subtype for the Binary representing this Buffer
|
||||
*
|
||||
* @api private
|
||||
* @property _subtype
|
||||
* @receiver MongooseBuffer
|
||||
*/
|
||||
|
||||
if (parent) {
|
||||
parent.markModified(this._path);
|
||||
_subtype: undefined,
|
||||
|
||||
/**
|
||||
* Marks this buffer as modified.
|
||||
*
|
||||
* @api private
|
||||
* @method _markModified
|
||||
* @receiver MongooseBuffer
|
||||
*/
|
||||
|
||||
_markModified: function() {
|
||||
var parent = this._parent;
|
||||
|
||||
if (parent) {
|
||||
parent.markModified(this._path);
|
||||
}
|
||||
return this;
|
||||
},
|
||||
|
||||
/**
|
||||
* Writes the buffer.
|
||||
*
|
||||
* @api public
|
||||
* @method write
|
||||
* @receiver MongooseBuffer
|
||||
*/
|
||||
|
||||
write: function() {
|
||||
var written = Buffer.prototype.write.apply(this, arguments);
|
||||
|
||||
if (written > 0) {
|
||||
this._markModified();
|
||||
}
|
||||
|
||||
return written;
|
||||
},
|
||||
|
||||
/**
|
||||
* Copies the buffer.
|
||||
*
|
||||
* ####Note:
|
||||
*
|
||||
* `Buffer#copy` does not mark `target` as modified so you must copy from a `MongooseBuffer` for it to work as expected. This is a work around since `copy` modifies the target, not this.
|
||||
*
|
||||
* @return {MongooseBuffer}
|
||||
* @param {Buffer} target
|
||||
* @method copy
|
||||
* @receiver MongooseBuffer
|
||||
*/
|
||||
|
||||
copy: function(target) {
|
||||
var ret = Buffer.prototype.copy.apply(this, arguments);
|
||||
|
||||
if (target && target.isMongooseBuffer) {
|
||||
target._markModified();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Writes the buffer.
|
||||
*/
|
||||
|
||||
MongooseBuffer.prototype.write = function () {
|
||||
var written = Buffer.prototype.write.apply(this, arguments);
|
||||
|
||||
if (written > 0) {
|
||||
this._markModified();
|
||||
}
|
||||
|
||||
return written;
|
||||
};
|
||||
|
||||
/**
|
||||
* Copies the buffer.
|
||||
*
|
||||
* ####Note:
|
||||
*
|
||||
* `Buffer#copy` does not mark `target` as modified so you must copy from a `MongooseBuffer` for it to work as expected. This is a work around since `copy` modifies the target, not this.
|
||||
*
|
||||
* @return {MongooseBuffer}
|
||||
* @param {Buffer} target
|
||||
*/
|
||||
|
||||
MongooseBuffer.prototype.copy = function (target) {
|
||||
var ret = Buffer.prototype.copy.apply(this, arguments);
|
||||
|
||||
if (target instanceof MongooseBuffer) {
|
||||
target._markModified();
|
||||
}
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Compile other Buffer methods marking this buffer as modified.
|
||||
*/
|
||||
|
||||
;(
|
||||
(
|
||||
// node < 0.5
|
||||
'writeUInt8 writeUInt16 writeUInt32 writeInt8 writeInt16 writeInt32 ' +
|
||||
'writeFloat writeDouble fill ' +
|
||||
@ -145,13 +162,13 @@ MongooseBuffer.prototype.copy = function (target) {
|
||||
'writeUInt16LE writeUInt16BE writeUInt32LE writeUInt32BE ' +
|
||||
'writeInt16LE writeInt16BE writeInt32LE writeInt32BE ' +
|
||||
'writeFloatLE writeFloatBE writeDoubleLE writeDoubleBE'
|
||||
).split(' ').forEach(function (method) {
|
||||
).split(' ').forEach(function(method) {
|
||||
if (!Buffer.prototype[method]) return;
|
||||
MongooseBuffer.prototype[method] = new Function(
|
||||
'var ret = Buffer.prototype.'+method+'.apply(this, arguments);' +
|
||||
MongooseBuffer.mixin[method] = new Function(
|
||||
'var ret = Buffer.prototype.' + method + '.apply(this, arguments);' +
|
||||
'this._markModified();' +
|
||||
'return ret;'
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
/**
|
||||
@ -173,12 +190,14 @@ MongooseBuffer.prototype.copy = function (target) {
|
||||
* @param {Hex} [subtype]
|
||||
* @return {Binary}
|
||||
* @api public
|
||||
* @method toObject
|
||||
* @receiver MongooseBuffer
|
||||
*/
|
||||
|
||||
MongooseBuffer.prototype.toObject = function (options) {
|
||||
MongooseBuffer.mixin.toObject = function(options) {
|
||||
var subtype = 'number' == typeof options
|
||||
? options
|
||||
: (this._subtype || 0x00);
|
||||
: (this._subtype || 0);
|
||||
return new Binary(this, subtype);
|
||||
};
|
||||
|
||||
@ -187,9 +206,11 @@ MongooseBuffer.prototype.toObject = function (options) {
|
||||
*
|
||||
* @param {Buffer} other
|
||||
* @return {Boolean}
|
||||
* @method equals
|
||||
* @receiver MongooseBuffer
|
||||
*/
|
||||
|
||||
MongooseBuffer.prototype.equals = function (other) {
|
||||
MongooseBuffer.mixin.equals = function(other) {
|
||||
if (!Buffer.isBuffer(other)) {
|
||||
return false;
|
||||
}
|
||||
@ -203,7 +224,41 @@ MongooseBuffer.prototype.equals = function (other) {
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets the subtype option and marks the buffer modified.
|
||||
*
|
||||
* ####SubTypes:
|
||||
*
|
||||
* var bson = require('bson')
|
||||
* bson.BSON_BINARY_SUBTYPE_DEFAULT
|
||||
* bson.BSON_BINARY_SUBTYPE_FUNCTION
|
||||
* bson.BSON_BINARY_SUBTYPE_BYTE_ARRAY
|
||||
* bson.BSON_BINARY_SUBTYPE_UUID
|
||||
* bson.BSON_BINARY_SUBTYPE_MD5
|
||||
* bson.BSON_BINARY_SUBTYPE_USER_DEFINED
|
||||
*
|
||||
* doc.buffer.subtype(bson.BSON_BINARY_SUBTYPE_UUID);
|
||||
*
|
||||
* @see http://bsonspec.org/#/specification
|
||||
* @param {Hex} subtype
|
||||
* @api public
|
||||
* @method subtype
|
||||
* @receiver MongooseBuffer
|
||||
*/
|
||||
|
||||
MongooseBuffer.mixin.subtype = function(subtype) {
|
||||
if ('number' != typeof subtype) {
|
||||
throw new TypeError('Invalid subtype. Expected a number');
|
||||
}
|
||||
|
||||
if (this._subtype != subtype) {
|
||||
this._markModified();
|
||||
}
|
||||
|
||||
this._subtype = subtype;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
|
115
node_modules/mongoose/lib/types/documentarray.js
generated
vendored
115
node_modules/mongoose/lib/types/documentarray.js
generated
vendored
@ -1,15 +1,13 @@
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var MongooseArray = require('./array')
|
||||
, driver = global.MONGOOSE_DRIVER_PATH || '../drivers/node-mongodb-native'
|
||||
, ObjectId = require(driver + '/objectid')
|
||||
, ObjectIdSchema = require('../schema/objectid')
|
||||
, utils = require('../utils')
|
||||
, util = require('util')
|
||||
, Document = require('../document')
|
||||
var MongooseArray = require('./array'),
|
||||
ObjectId = require('./objectid'),
|
||||
ObjectIdSchema = require('../schema/objectid'),
|
||||
utils = require('../utils'),
|
||||
util = require('util'),
|
||||
Document = require('../document');
|
||||
|
||||
/**
|
||||
* DocumentArray constructor
|
||||
@ -23,47 +21,59 @@ var MongooseArray = require('./array')
|
||||
* @see http://bit.ly/f6CnZU
|
||||
*/
|
||||
|
||||
function MongooseDocumentArray (values, path, doc) {
|
||||
var arr = [];
|
||||
function MongooseDocumentArray(values, path, doc) {
|
||||
var arr = [].concat(values);
|
||||
|
||||
// Values always have to be passed to the constructor to initialize, since
|
||||
// otherwise MongooseArray#push will mark the array as modified to the parent.
|
||||
arr.push.apply(arr, values);
|
||||
arr.__proto__ = MongooseDocumentArray.prototype;
|
||||
utils.decorate( arr, MongooseDocumentArray.mixin );
|
||||
arr.isMongooseArray = true;
|
||||
arr.isMongooseDocumentArray = true;
|
||||
|
||||
arr._atomics = {};
|
||||
arr.validators = [];
|
||||
arr._path = path;
|
||||
|
||||
if (doc) {
|
||||
// Because doc comes from the context of another function, doc === global
|
||||
// can happen if there was a null somewhere up the chain (see #3020 && #3034)
|
||||
// RB Jun 17, 2015 updated to check for presence of expected paths instead
|
||||
// to make more proof against unusual node environments
|
||||
if (doc && doc instanceof Document) {
|
||||
arr._parent = doc;
|
||||
arr._schema = doc.schema.path(path);
|
||||
doc.on('save', arr.notify('save'));
|
||||
doc.on('isNew', arr.notify('isNew'));
|
||||
arr._handlers = {
|
||||
isNew: arr.notify('isNew'),
|
||||
save: arr.notify('save')
|
||||
};
|
||||
|
||||
doc.on('save', arr._handlers.save);
|
||||
doc.on('isNew', arr._handlers.isNew);
|
||||
}
|
||||
|
||||
return arr;
|
||||
};
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherits from MongooseArray
|
||||
*/
|
||||
|
||||
MongooseDocumentArray.prototype.__proto__ = MongooseArray.prototype;
|
||||
MongooseDocumentArray.mixin = Object.create( MongooseArray.mixin );
|
||||
|
||||
/**
|
||||
* Overrides MongooseArray#cast
|
||||
*
|
||||
* @method _cast
|
||||
* @api private
|
||||
* @receiver MongooseDocumentArray
|
||||
*/
|
||||
|
||||
MongooseDocumentArray.prototype._cast = function (value) {
|
||||
MongooseDocumentArray.mixin._cast = function(value, index) {
|
||||
if (value instanceof this._schema.casterConstructor) {
|
||||
if (!(value.__parent && value.__parentArray)) {
|
||||
// value may have been created using array.create()
|
||||
value.__parent = this._parent;
|
||||
value.__parentArray = this;
|
||||
}
|
||||
value.__index = index;
|
||||
return value;
|
||||
}
|
||||
|
||||
@ -74,8 +84,7 @@ MongooseDocumentArray.prototype._cast = function (value) {
|
||||
value instanceof ObjectId || !utils.isObject(value)) {
|
||||
value = { _id: value };
|
||||
}
|
||||
|
||||
return new this._schema.casterConstructor(value, this);
|
||||
return new this._schema.casterConstructor(value, this, undefined, undefined, index);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -85,18 +94,22 @@ MongooseDocumentArray.prototype._cast = function (value) {
|
||||
*
|
||||
* var embeddedDoc = m.array.id(some_id);
|
||||
*
|
||||
* @return {EmbeddedDocument|null} the subdocuent or null if not found.
|
||||
* @return {EmbeddedDocument|null} the subdocument or null if not found.
|
||||
* @param {ObjectId|String|Number|Buffer} id
|
||||
* @TODO cast to the _id based on schema for proper comparison
|
||||
* @method id
|
||||
* @api public
|
||||
* @receiver MongooseDocumentArray
|
||||
*/
|
||||
|
||||
MongooseDocumentArray.prototype.id = function (id) {
|
||||
var casted
|
||||
, sid
|
||||
, _id
|
||||
MongooseDocumentArray.mixin.id = function(id) {
|
||||
var casted,
|
||||
sid,
|
||||
_id;
|
||||
|
||||
try {
|
||||
casted = ObjectId.toString(ObjectIdSchema.prototype.cast.call({}, id));
|
||||
var casted_ = ObjectIdSchema.prototype.cast.call({}, id);
|
||||
if (casted_) casted = String(casted_);
|
||||
} catch (e) {
|
||||
casted = null;
|
||||
}
|
||||
@ -104,12 +117,13 @@ MongooseDocumentArray.prototype.id = function (id) {
|
||||
for (var i = 0, l = this.length; i < l; i++) {
|
||||
_id = this[i].get('_id');
|
||||
|
||||
if (_id instanceof Document) {
|
||||
if (_id === null || typeof _id === 'undefined') {
|
||||
continue;
|
||||
} else if (_id instanceof Document) {
|
||||
sid || (sid = String(id));
|
||||
if (sid == _id._id) return this[i];
|
||||
} else if (!(_id instanceof ObjectId)) {
|
||||
sid || (sid = String(id));
|
||||
if (sid == _id) return this[i];
|
||||
if (utils.deepEqual(id, _id)) return this[i];
|
||||
} else if (casted == _id) {
|
||||
return this[i];
|
||||
}
|
||||
@ -127,11 +141,13 @@ MongooseDocumentArray.prototype.id = function (id) {
|
||||
*
|
||||
* @param {Object} [options] optional options to pass to each documents `toObject` method call during conversion
|
||||
* @return {Array}
|
||||
* @method toObject
|
||||
* @api public
|
||||
* @receiver MongooseDocumentArray
|
||||
*/
|
||||
|
||||
MongooseDocumentArray.prototype.toObject = function (options) {
|
||||
return this.map(function (doc) {
|
||||
MongooseDocumentArray.mixin.toObject = function(options) {
|
||||
return this.map(function(doc) {
|
||||
return doc && doc.toObject(options) || null;
|
||||
});
|
||||
};
|
||||
@ -139,17 +155,19 @@ MongooseDocumentArray.prototype.toObject = function (options) {
|
||||
/**
|
||||
* Helper for console.log
|
||||
*
|
||||
* @method inspect
|
||||
* @api public
|
||||
* @receiver MongooseDocumentArray
|
||||
*/
|
||||
|
||||
MongooseDocumentArray.prototype.inspect = function () {
|
||||
return '[' + this.map(function (doc) {
|
||||
MongooseDocumentArray.mixin.inspect = function() {
|
||||
return '[' + Array.prototype.map.call(this, function(doc) {
|
||||
if (doc) {
|
||||
return doc.inspect
|
||||
? doc.inspect()
|
||||
: util.inspect(doc)
|
||||
: util.inspect(doc);
|
||||
}
|
||||
return 'null'
|
||||
return 'null';
|
||||
}).join('\n') + ']';
|
||||
};
|
||||
|
||||
@ -159,31 +177,44 @@ MongooseDocumentArray.prototype.inspect = function () {
|
||||
* This is the same subdocument constructor used for casting.
|
||||
*
|
||||
* @param {Object} obj the value to cast to this arrays SubDocument schema
|
||||
* @method create
|
||||
* @api public
|
||||
* @receiver MongooseDocumentArray
|
||||
*/
|
||||
|
||||
MongooseDocumentArray.prototype.create = function (obj) {
|
||||
MongooseDocumentArray.mixin.create = function(obj) {
|
||||
return new this._schema.casterConstructor(obj);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a fn that notifies all child docs of `event`.
|
||||
*
|
||||
* @param {String} event
|
||||
* @return {Function}
|
||||
* @method notify
|
||||
* @api private
|
||||
* @receiver MongooseDocumentArray
|
||||
*/
|
||||
|
||||
MongooseDocumentArray.prototype.notify = function notify (event) {
|
||||
MongooseDocumentArray.mixin.notify = function notify(event) {
|
||||
var self = this;
|
||||
return function notify (val) {
|
||||
return function notify(val) {
|
||||
var i = self.length;
|
||||
while (i--) {
|
||||
if (!self[i]) continue;
|
||||
switch (event) {
|
||||
// only swap for save event for now, we may change this to all event types later
|
||||
case 'save':
|
||||
val = self[i];
|
||||
break;
|
||||
default:
|
||||
// NO-OP
|
||||
break;
|
||||
}
|
||||
self[i].emit(event, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
|
150
node_modules/mongoose/lib/types/embedded.js
generated
vendored
150
node_modules/mongoose/lib/types/embedded.js
generated
vendored
@ -1,9 +1,12 @@
|
||||
/* eslint no-func-assign: 1 */
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var Document = require('../document')
|
||||
, inspect = require('util').inspect;
|
||||
var Document = require('../document_provider')();
|
||||
var inspect = require('util').inspect;
|
||||
var PromiseProvider = require('../promise_provider');
|
||||
|
||||
/**
|
||||
* EmbeddedDocument constructor.
|
||||
@ -15,7 +18,7 @@ var Document = require('../document')
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function EmbeddedDocument (obj, parentArr, skipId, fields) {
|
||||
function EmbeddedDocument(obj, parentArr, skipId, fields, index) {
|
||||
if (parentArr) {
|
||||
this.__parentArray = parentArr;
|
||||
this.__parent = parentArr._parent;
|
||||
@ -23,20 +26,21 @@ function EmbeddedDocument (obj, parentArr, skipId, fields) {
|
||||
this.__parentArray = undefined;
|
||||
this.__parent = undefined;
|
||||
}
|
||||
this.__index = index;
|
||||
|
||||
Document.call(this, obj, fields, skipId);
|
||||
|
||||
var self = this;
|
||||
this.on('isNew', function (val) {
|
||||
this.on('isNew', function(val) {
|
||||
self.isNew = val;
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/*!
|
||||
* Inherit from Document
|
||||
*/
|
||||
|
||||
EmbeddedDocument.prototype.__proto__ = Document.prototype;
|
||||
EmbeddedDocument.prototype = Object.create( Document.prototype );
|
||||
EmbeddedDocument.prototype.constructor = EmbeddedDocument;
|
||||
|
||||
/**
|
||||
* Marks the embedded doc modified.
|
||||
@ -49,20 +53,21 @@ EmbeddedDocument.prototype.__proto__ = Document.prototype;
|
||||
*
|
||||
* @param {String} path the path which changed
|
||||
* @api public
|
||||
* @receiver EmbeddedDocument
|
||||
*/
|
||||
|
||||
EmbeddedDocument.prototype.markModified = function (path) {
|
||||
if (!this.__parentArray) return;
|
||||
|
||||
EmbeddedDocument.prototype.markModified = function(path) {
|
||||
this.$__.activePaths.modify(path);
|
||||
if (!this.__parentArray) return;
|
||||
|
||||
if (this.isNew) {
|
||||
// Mark the WHOLE parent array as modified
|
||||
// if this is a new document (i.e., we are initializing
|
||||
// a document),
|
||||
this.__parentArray._markModified();
|
||||
} else
|
||||
} else {
|
||||
this.__parentArray._markModified(this, path);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@ -73,14 +78,16 @@ EmbeddedDocument.prototype.markModified = function (path) {
|
||||
* _This is a no-op. Does not actually save the doc to the db._
|
||||
*
|
||||
* @param {Function} [fn]
|
||||
* @return {EmbeddedDocument} this
|
||||
* @return {Promise} resolved Promise
|
||||
* @api private
|
||||
*/
|
||||
|
||||
EmbeddedDocument.prototype.save = function(fn) {
|
||||
if (fn)
|
||||
fn(null);
|
||||
return this;
|
||||
var Promise = PromiseProvider.get();
|
||||
return new Promise.ES6(function(resolve) {
|
||||
fn && fn();
|
||||
resolve();
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@ -90,7 +97,7 @@ EmbeddedDocument.prototype.save = function(fn) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
EmbeddedDocument.prototype.remove = function (fn) {
|
||||
EmbeddedDocument.prototype.remove = function(fn) {
|
||||
if (!this.__parentArray) return this;
|
||||
|
||||
var _id;
|
||||
@ -102,6 +109,7 @@ EmbeddedDocument.prototype.remove = function (fn) {
|
||||
}
|
||||
this.__parentArray.pull({ _id: _id });
|
||||
this.willRemove = true;
|
||||
registerRemoveListener(this);
|
||||
}
|
||||
|
||||
if (fn)
|
||||
@ -110,14 +118,36 @@ EmbeddedDocument.prototype.remove = function (fn) {
|
||||
return this;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Registers remove event listeners for triggering
|
||||
* on subdocuments.
|
||||
*
|
||||
* @param {EmbeddedDocument} sub
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function registerRemoveListener(sub) {
|
||||
var owner = sub.ownerDocument();
|
||||
|
||||
owner.on('save', emitRemove);
|
||||
owner.on('remove', emitRemove);
|
||||
|
||||
function emitRemove() {
|
||||
owner.removeListener('save', emitRemove);
|
||||
owner.removeListener('remove', emitRemove);
|
||||
sub.emit('remove', sub);
|
||||
owner = sub = emitRemove = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Override #update method of parent documents.
|
||||
* @api private
|
||||
*/
|
||||
|
||||
EmbeddedDocument.prototype.update = function () {
|
||||
EmbeddedDocument.prototype.update = function() {
|
||||
throw new Error('The #update method is not available on EmbeddedDocuments');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Helper for console.log
|
||||
@ -125,7 +155,7 @@ EmbeddedDocument.prototype.update = function () {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
EmbeddedDocument.prototype.inspect = function () {
|
||||
EmbeddedDocument.prototype.inspect = function() {
|
||||
return inspect(this.toObject());
|
||||
};
|
||||
|
||||
@ -138,29 +168,67 @@ EmbeddedDocument.prototype.inspect = function () {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
EmbeddedDocument.prototype.invalidate = function (path, err, val, first) {
|
||||
EmbeddedDocument.prototype.invalidate = function(path, err, val, first) {
|
||||
if (!this.__parent) {
|
||||
var msg = 'Unable to invalidate a subdocument that has not been added to an array.'
|
||||
var msg = 'Unable to invalidate a subdocument that has not been added to an array.';
|
||||
throw new Error(msg);
|
||||
}
|
||||
|
||||
var index = this.__parentArray.indexOf(this);
|
||||
var parentPath = this.__parentArray._path;
|
||||
var fullPath = [parentPath, index, path].join('.');
|
||||
|
||||
// sniffing arguments:
|
||||
// need to check if user passed a value to keep
|
||||
// our error message clean.
|
||||
if (2 < arguments.length) {
|
||||
var index = this.__index;
|
||||
if (typeof index !== 'undefined') {
|
||||
var parentPath = this.__parentArray._path;
|
||||
var fullPath = [parentPath, index, path].join('.');
|
||||
this.__parent.invalidate(fullPath, err, val);
|
||||
} else {
|
||||
this.__parent.invalidate(fullPath, err);
|
||||
}
|
||||
|
||||
if (first)
|
||||
if (first) {
|
||||
this.$__.validationError = this.ownerDocument().$__.validationError;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Marks a path as valid, removing existing validation errors.
|
||||
*
|
||||
* @param {String} path the field to mark as valid
|
||||
* @api private
|
||||
* @method $markValid
|
||||
* @receiver EmbeddedDocument
|
||||
*/
|
||||
|
||||
EmbeddedDocument.prototype.$markValid = function(path) {
|
||||
if (!this.__parent) {
|
||||
return;
|
||||
}
|
||||
|
||||
var index = this.__index;
|
||||
if (typeof index !== 'undefined') {
|
||||
var parentPath = this.__parentArray._path;
|
||||
var fullPath = [parentPath, index, path].join('.');
|
||||
this.__parent.$markValid(fullPath);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if a path is invalid
|
||||
*
|
||||
* @param {String} path the field to check
|
||||
* @api private
|
||||
* @method $isValid
|
||||
* @receiver EmbeddedDocument
|
||||
*/
|
||||
|
||||
EmbeddedDocument.prototype.$isValid = function(path) {
|
||||
var index = this.__index;
|
||||
if (typeof index !== 'undefined') {
|
||||
|
||||
return !this.__parent.$__.validationError ||
|
||||
!this.__parent.$__.validationError.errors[path];
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the top level document of this sub-document.
|
||||
@ -168,7 +236,7 @@ EmbeddedDocument.prototype.invalidate = function (path, err, val, first) {
|
||||
* @return {Document}
|
||||
*/
|
||||
|
||||
EmbeddedDocument.prototype.ownerDocument = function () {
|
||||
EmbeddedDocument.prototype.ownerDocument = function() {
|
||||
if (this.$__.ownerDocument) {
|
||||
return this.$__.ownerDocument;
|
||||
}
|
||||
@ -181,7 +249,7 @@ EmbeddedDocument.prototype.ownerDocument = function () {
|
||||
}
|
||||
|
||||
return this.$__.ownerDocument = parent;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the full path to this document. If optional `path` is passed, it is appended to the full path.
|
||||
@ -193,7 +261,7 @@ EmbeddedDocument.prototype.ownerDocument = function () {
|
||||
* @memberOf EmbeddedDocument
|
||||
*/
|
||||
|
||||
EmbeddedDocument.prototype.$__fullPath = function (path) {
|
||||
EmbeddedDocument.prototype.$__fullPath = function(path) {
|
||||
if (!this.$__.fullPath) {
|
||||
var parent = this;
|
||||
if (!parent.__parent) return path;
|
||||
@ -215,7 +283,7 @@ EmbeddedDocument.prototype.$__fullPath = function (path) {
|
||||
return path
|
||||
? this.$__.fullPath + '.' + path
|
||||
: this.$__.fullPath;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns this sub-documents parent document.
|
||||
@ -223,9 +291,9 @@ EmbeddedDocument.prototype.$__fullPath = function (path) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
EmbeddedDocument.prototype.parent = function () {
|
||||
EmbeddedDocument.prototype.parent = function() {
|
||||
return this.__parent;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns this sub-documents parent array.
|
||||
@ -233,9 +301,9 @@ EmbeddedDocument.prototype.parent = function () {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
EmbeddedDocument.prototype.parentArray = function () {
|
||||
EmbeddedDocument.prototype.parentArray = function() {
|
||||
return this.__parentArray;
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
|
2
node_modules/mongoose/lib/types/index.js
generated
vendored
2
node_modules/mongoose/lib/types/index.js
generated
vendored
@ -11,3 +11,5 @@ exports.Embedded = require('./embedded');
|
||||
|
||||
exports.DocumentArray = require('./documentarray');
|
||||
exports.ObjectId = require('./objectid');
|
||||
|
||||
exports.Subdocument = require('./subdocument');
|
||||
|
34
node_modules/mongoose/lib/types/objectid.js
generated
vendored
34
node_modules/mongoose/lib/types/objectid.js
generated
vendored
@ -1,10 +1,3 @@
|
||||
|
||||
/*!
|
||||
* Access driver.
|
||||
*/
|
||||
|
||||
var driver = global.MONGOOSE_DRIVER_PATH || '../drivers/node-mongodb-native';
|
||||
|
||||
/**
|
||||
* ObjectId type constructor
|
||||
*
|
||||
@ -15,29 +8,6 @@ var driver = global.MONGOOSE_DRIVER_PATH || '../drivers/node-mongodb-native';
|
||||
* @constructor ObjectId
|
||||
*/
|
||||
|
||||
var ObjectId = require(driver + '/objectid');
|
||||
var ObjectId = require('../drivers').ObjectId;
|
||||
|
||||
module.exports = ObjectId;
|
||||
|
||||
/**
|
||||
* Creates an ObjectId from `str`
|
||||
*
|
||||
* @param {ObjectId|HexString} str
|
||||
* @static fromString
|
||||
* @receiver ObjectId
|
||||
* @return {ObjectId}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
ObjectId.fromString;
|
||||
|
||||
/**
|
||||
* Converts `oid` to a string.
|
||||
*
|
||||
* @param {ObjectId} oid ObjectId instance
|
||||
* @static toString
|
||||
* @receiver ObjectId
|
||||
* @return {String}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
ObjectId.toString;
|
||||
|
62
node_modules/mongoose/lib/types/subdocument.js
generated
vendored
Normal file
62
node_modules/mongoose/lib/types/subdocument.js
generated
vendored
Normal file
@ -0,0 +1,62 @@
|
||||
var Document = require('../document');
|
||||
var PromiseProvider = require('../promise_provider');
|
||||
|
||||
module.exports = Subdocument;
|
||||
|
||||
/**
|
||||
* Subdocument constructor.
|
||||
*
|
||||
* @inherits Document
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function Subdocument() {
|
||||
Document.apply(this, arguments);
|
||||
this.$isSingleNested = true;
|
||||
}
|
||||
|
||||
Subdocument.prototype = Object.create(Document.prototype);
|
||||
|
||||
/**
|
||||
* Used as a stub for [hooks.js](https://github.com/bnoguchi/hooks-js/tree/31ec571cef0332e21121ee7157e0cf9728572cc3)
|
||||
*
|
||||
* ####NOTE:
|
||||
*
|
||||
* _This is a no-op. Does not actually save the doc to the db._
|
||||
*
|
||||
* @param {Function} [fn]
|
||||
* @return {Promise} resolved Promise
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Subdocument.prototype.save = function(fn) {
|
||||
var Promise = PromiseProvider.get();
|
||||
return new Promise.ES6(function(resolve) {
|
||||
fn && fn();
|
||||
resolve();
|
||||
});
|
||||
};
|
||||
|
||||
Subdocument.prototype.$isValid = function(path) {
|
||||
if (this.$parent) {
|
||||
return this.$parent.$isValid([this.$basePath, path].join('.'));
|
||||
}
|
||||
};
|
||||
|
||||
Subdocument.prototype.markModified = function(path) {
|
||||
if (this.$parent) {
|
||||
this.$parent.markModified([this.$basePath, path].join('.'));
|
||||
}
|
||||
};
|
||||
|
||||
Subdocument.prototype.$markValid = function(path) {
|
||||
if (this.$parent) {
|
||||
this.$parent.$markValid([this.$basePath, path].join('.'));
|
||||
}
|
||||
};
|
||||
|
||||
Subdocument.prototype.invalidate = function(path, err, val) {
|
||||
if (this.$parent) {
|
||||
this.$parent.invalidate([this.$basePath, path].join('.'), err, val);
|
||||
}
|
||||
};
|
376
node_modules/mongoose/lib/utils.js
generated
vendored
376
node_modules/mongoose/lib/utils.js
generated
vendored
@ -2,15 +2,14 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var ReadPref = require('mongodb').ReadPreference
|
||||
, ObjectId = require('./types/objectid')
|
||||
, cloneRegExp = require('regexp-clone')
|
||||
, sliced = require('sliced')
|
||||
, mpath = require('mpath')
|
||||
, ms = require('ms')
|
||||
, MongooseBuffer
|
||||
, MongooseArray
|
||||
, Document
|
||||
var ObjectId = require('./types/objectid');
|
||||
var cloneRegExp = require('regexp-clone');
|
||||
var sliced = require('sliced');
|
||||
var mpath = require('mpath');
|
||||
var ms = require('ms');
|
||||
var MongooseBuffer;
|
||||
var MongooseArray;
|
||||
var Document;
|
||||
|
||||
/*!
|
||||
* Produces a collection name from model `name`.
|
||||
@ -20,9 +19,11 @@ var ReadPref = require('mongodb').ReadPreference
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.toCollectionName = function (name) {
|
||||
exports.toCollectionName = function(name, options) {
|
||||
options = options || {};
|
||||
if ('system.profile' === name) return name;
|
||||
if ('system.indexes' === name) return name;
|
||||
if (options.pluralization === false) return name;
|
||||
return pluralize(name.toLowerCase());
|
||||
};
|
||||
|
||||
@ -52,8 +53,10 @@ exports.pluralization = [
|
||||
[/(x|ch|ss|sh)$/gi, '$1es'],
|
||||
[/(matr|vert|ind)ix|ex$/gi, '$1ices'],
|
||||
[/([m|l])ouse$/gi, '$1ice'],
|
||||
[/(kn|w|l)ife$/gi, '$1ives'],
|
||||
[/(quiz)$/gi, '$1zes'],
|
||||
[/s$/gi, 's'],
|
||||
[/([^a-z])$/, '$1'],
|
||||
[/$/gi, 's']
|
||||
];
|
||||
var rules = exports.pluralization;
|
||||
@ -104,16 +107,16 @@ var uncountables = exports.uncountables;
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function pluralize (str) {
|
||||
var rule, found;
|
||||
if (!~uncountables.indexOf(str.toLowerCase())){
|
||||
found = rules.filter(function(rule){
|
||||
function pluralize(str) {
|
||||
var found;
|
||||
if (!~uncountables.indexOf(str.toLowerCase())) {
|
||||
found = rules.filter(function(rule) {
|
||||
return str.match(rule[0]);
|
||||
});
|
||||
if (found[0]) return str.replace(found[0][0], found[0][1]);
|
||||
}
|
||||
return str;
|
||||
};
|
||||
}
|
||||
|
||||
/*!
|
||||
* Determines if `a` and `b` are deep equal.
|
||||
@ -126,7 +129,7 @@ function pluralize (str) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.deepEqual = function deepEqual (a, b) {
|
||||
exports.deepEqual = function deepEqual(a, b) {
|
||||
if (a === b) return true;
|
||||
|
||||
if (a instanceof Date && b instanceof Date)
|
||||
@ -147,7 +150,7 @@ exports.deepEqual = function deepEqual (a, b) {
|
||||
return a == b;
|
||||
|
||||
if (a === null || b === null || a === undefined || b === undefined)
|
||||
return false
|
||||
return false;
|
||||
|
||||
if (a.prototype !== b.prototype) return false;
|
||||
|
||||
@ -157,12 +160,7 @@ exports.deepEqual = function deepEqual (a, b) {
|
||||
}
|
||||
|
||||
if (Buffer.isBuffer(a)) {
|
||||
if (!Buffer.isBuffer(b)) return false;
|
||||
if (a.length !== b.length) return false;
|
||||
for (var i = 0, len = a.length; i < len; ++i) {
|
||||
if (a[i] !== b[i]) return false;
|
||||
}
|
||||
return true;
|
||||
return exports.buffer.areEqual(a, b);
|
||||
}
|
||||
|
||||
if (isMongooseObject(a)) a = a.toObject();
|
||||
@ -214,7 +212,7 @@ exports.deepEqual = function deepEqual (a, b) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.clone = function clone (obj, options) {
|
||||
exports.clone = function clone(obj, options) {
|
||||
if (obj === undefined || obj === null)
|
||||
return obj;
|
||||
|
||||
@ -230,7 +228,7 @@ exports.clone = function clone (obj, options) {
|
||||
}
|
||||
|
||||
if (obj.constructor) {
|
||||
switch (obj.constructor.name) {
|
||||
switch (exports.getFunctionName(obj.constructor)) {
|
||||
case 'Object':
|
||||
return cloneObject(obj, options);
|
||||
case 'Date':
|
||||
@ -260,15 +258,15 @@ var clone = exports.clone;
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function cloneObject (obj, options) {
|
||||
var retainKeyOrder = options && options.retainKeyOrder
|
||||
, minimize = options && options.minimize
|
||||
, ret = {}
|
||||
, hasKeys
|
||||
, keys
|
||||
, val
|
||||
, k
|
||||
, i
|
||||
function cloneObject(obj, options) {
|
||||
var retainKeyOrder = options && options.retainKeyOrder,
|
||||
minimize = options && options.minimize,
|
||||
ret = {},
|
||||
hasKeys,
|
||||
keys,
|
||||
val,
|
||||
k,
|
||||
i;
|
||||
|
||||
if (retainKeyOrder) {
|
||||
for (k in obj) {
|
||||
@ -299,14 +297,14 @@ function cloneObject (obj, options) {
|
||||
return minimize
|
||||
? hasKeys && ret
|
||||
: ret;
|
||||
};
|
||||
}
|
||||
|
||||
function cloneArray (arr, options) {
|
||||
function cloneArray(arr, options) {
|
||||
var ret = [];
|
||||
for (var i = 0, l = arr.length; i < l; i++)
|
||||
ret.push(clone(arr[i], options));
|
||||
return ret;
|
||||
};
|
||||
}
|
||||
|
||||
/*!
|
||||
* Shallow copies defaults into options.
|
||||
@ -317,10 +315,10 @@ function cloneArray (arr, options) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.options = function (defaults, options) {
|
||||
var keys = Object.keys(defaults)
|
||||
, i = keys.length
|
||||
, k ;
|
||||
exports.options = function(defaults, options) {
|
||||
var keys = Object.keys(defaults),
|
||||
i = keys.length,
|
||||
k;
|
||||
|
||||
options = options || {};
|
||||
|
||||
@ -340,7 +338,7 @@ exports.options = function (defaults, options) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.random = function () {
|
||||
exports.random = function() {
|
||||
return Math.random().toString().substr(3);
|
||||
};
|
||||
|
||||
@ -352,21 +350,17 @@ exports.random = function () {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.merge = function merge (to, from) {
|
||||
var keys = Object.keys(from)
|
||||
, i = keys.length
|
||||
, key
|
||||
exports.merge = function merge(to, from) {
|
||||
var keys = Object.keys(from),
|
||||
i = keys.length,
|
||||
key;
|
||||
|
||||
while (i--) {
|
||||
key = keys[i];
|
||||
if ('undefined' === typeof to[key]) {
|
||||
to[key] = from[key];
|
||||
} else {
|
||||
if (exports.isObject(from[key])) {
|
||||
merge(to[key], from[key]);
|
||||
} else {
|
||||
to[key] = from[key];
|
||||
}
|
||||
} else if (exports.isObject(from[key])) {
|
||||
merge(to[key], from[key]);
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -377,6 +371,49 @@ exports.merge = function merge (to, from) {
|
||||
|
||||
var toString = Object.prototype.toString;
|
||||
|
||||
/*!
|
||||
* Applies toObject recursively.
|
||||
*
|
||||
* @param {Document|Array|Object} obj
|
||||
* @return {Object}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.toObject = function toObject(obj) {
|
||||
var ret;
|
||||
|
||||
if (exports.isNullOrUndefined(obj)) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
if (obj instanceof Document) {
|
||||
return obj.toObject();
|
||||
}
|
||||
|
||||
if (Array.isArray(obj)) {
|
||||
ret = [];
|
||||
|
||||
for (var i = 0, len = obj.length; i < len; ++i) {
|
||||
ret.push(toObject(obj[i]));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((obj.constructor && exports.getFunctionName(obj.constructor) === 'Object') ||
|
||||
(!obj.constructor && exports.isObject(obj))) {
|
||||
ret = {};
|
||||
|
||||
for (var k in obj) {
|
||||
ret[k] = toObject(obj[k]);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
return obj;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Determines if `arg` is an object.
|
||||
*
|
||||
@ -385,9 +422,12 @@ var toString = Object.prototype.toString;
|
||||
* @return {Boolean}
|
||||
*/
|
||||
|
||||
exports.isObject = function (arg) {
|
||||
exports.isObject = function(arg) {
|
||||
if (Buffer.isBuffer(arg)) {
|
||||
return true;
|
||||
}
|
||||
return '[object Object]' == toString.call(arg);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* A faster Array.prototype.slice.call(arguments) alternative
|
||||
@ -407,20 +447,20 @@ exports.args = sliced;
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.tick = function tick (callback) {
|
||||
exports.tick = function tick(callback) {
|
||||
if ('function' !== typeof callback) return;
|
||||
return function () {
|
||||
return function() {
|
||||
try {
|
||||
callback.apply(this, arguments);
|
||||
} catch (err) {
|
||||
// only nextTick on err to get out of
|
||||
// the event loop and avoid state corruption.
|
||||
process.nextTick(function () {
|
||||
process.nextTick(function() {
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
/*!
|
||||
* Returns if `v` is a mongoose object that has a `toObject()` method we can use.
|
||||
@ -431,15 +471,15 @@ exports.tick = function tick (callback) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.isMongooseObject = function (v) {
|
||||
exports.isMongooseObject = function(v) {
|
||||
Document || (Document = require('./document'));
|
||||
MongooseArray || (MongooseArray = require('./types').Array);
|
||||
MongooseBuffer || (MongooseBuffer = require('./types').Buffer);
|
||||
|
||||
return v instanceof Document ||
|
||||
v instanceof MongooseArray ||
|
||||
v instanceof MongooseBuffer
|
||||
}
|
||||
(v && v.isMongooseArray) ||
|
||||
(v && v.isMongooseBuffer);
|
||||
};
|
||||
var isMongooseObject = exports.isMongooseObject;
|
||||
|
||||
/*!
|
||||
@ -449,7 +489,7 @@ var isMongooseObject = exports.isMongooseObject;
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.expires = function expires (object) {
|
||||
exports.expires = function expires(object) {
|
||||
if (!(object && 'Object' == object.constructor.name)) return;
|
||||
if (!('expires' in object)) return;
|
||||
|
||||
@ -461,54 +501,21 @@ exports.expires = function expires (object) {
|
||||
}
|
||||
object.expireAfterSeconds = when;
|
||||
delete object.expires;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Converts arguments to ReadPrefs the driver
|
||||
* can understand.
|
||||
*
|
||||
* @TODO move this into the driver layer
|
||||
* @param {String|Array} pref
|
||||
* @param {Array} [tags]
|
||||
*/
|
||||
|
||||
exports.readPref = function readPref (pref, tags) {
|
||||
if (Array.isArray(pref)) {
|
||||
tags = pref[1];
|
||||
pref = pref[0];
|
||||
}
|
||||
|
||||
switch (pref) {
|
||||
case 'p':
|
||||
pref = 'primary';
|
||||
break;
|
||||
case 'pp':
|
||||
pref = 'primaryPreferred';
|
||||
break;
|
||||
case 's':
|
||||
pref = 'secondary';
|
||||
break;
|
||||
case 'sp':
|
||||
pref = 'secondaryPreferred';
|
||||
break;
|
||||
case 'n':
|
||||
pref = 'nearest';
|
||||
break;
|
||||
}
|
||||
|
||||
return new ReadPref(pref, tags);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Populate options constructor
|
||||
*/
|
||||
|
||||
function PopulateOptions (path, select, match, options, model) {
|
||||
function PopulateOptions(path, select, match, options, model, subPopulate) {
|
||||
this.path = path;
|
||||
this.match = match;
|
||||
this.select = select;
|
||||
this.options = options;
|
||||
this.model = model;
|
||||
if (typeof subPopulate === 'object') {
|
||||
this.populate = subPopulate;
|
||||
}
|
||||
this._docs = {};
|
||||
}
|
||||
|
||||
@ -522,7 +529,7 @@ exports.PopulateOptions = PopulateOptions;
|
||||
* populate helper
|
||||
*/
|
||||
|
||||
exports.populate = function populate (path, select, model, match, options) {
|
||||
exports.populate = function populate(path, select, model, match, options, subPopulate) {
|
||||
// The order of select/conditions args is opposite Model.find but
|
||||
// necessary to keep backward compatibility (select could be
|
||||
// an array, string, or object literal).
|
||||
@ -534,7 +541,7 @@ exports.populate = function populate (path, select, model, match, options) {
|
||||
}
|
||||
|
||||
if (Array.isArray(path)) {
|
||||
return path.map(function(o){
|
||||
return path.map(function(o) {
|
||||
return exports.populate(o)[0];
|
||||
});
|
||||
}
|
||||
@ -544,9 +551,10 @@ exports.populate = function populate (path, select, model, match, options) {
|
||||
options = path.options;
|
||||
select = path.select;
|
||||
model = path.model;
|
||||
subPopulate = path.populate;
|
||||
path = path.path;
|
||||
}
|
||||
} else if ('string' !== typeof model) {
|
||||
} else if ('string' !== typeof model && 'function' !== typeof model) {
|
||||
options = match;
|
||||
match = model;
|
||||
model = undefined;
|
||||
@ -556,14 +564,18 @@ exports.populate = function populate (path, select, model, match, options) {
|
||||
throw new TypeError('utils.populate: invalid path. Expected string. Got typeof `' + typeof path + '`');
|
||||
}
|
||||
|
||||
if (typeof subPopulate === 'object') {
|
||||
subPopulate = exports.populate(subPopulate);
|
||||
}
|
||||
|
||||
var ret = [];
|
||||
var paths = path.split(' ');
|
||||
for (var i = 0; i < paths.length; ++i) {
|
||||
ret.push(new PopulateOptions(paths[i], select, match, options, model));
|
||||
ret.push(new PopulateOptions(paths[i], select, match, options, model, subPopulate));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Return the value of `obj` at the given `path`.
|
||||
@ -572,9 +584,9 @@ exports.populate = function populate (path, select, model, match, options) {
|
||||
* @param {Object} obj
|
||||
*/
|
||||
|
||||
exports.getValue = function (path, obj, map) {
|
||||
exports.getValue = function(path, obj, map) {
|
||||
return mpath.get(path, obj, '_doc', map);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Sets the value of `obj` at the given `path`.
|
||||
@ -584,9 +596,9 @@ exports.getValue = function (path, obj, map) {
|
||||
* @param {Object} obj
|
||||
*/
|
||||
|
||||
exports.setValue = function (path, val, obj, map) {
|
||||
exports.setValue = function(path, val, obj, map) {
|
||||
mpath.set(path, val, obj, '_doc', map);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Returns an array of values from object `o`.
|
||||
@ -597,17 +609,17 @@ exports.setValue = function (path, val, obj, map) {
|
||||
*/
|
||||
|
||||
exports.object = {};
|
||||
exports.object.vals = function vals (o) {
|
||||
var keys = Object.keys(o)
|
||||
, i = keys.length
|
||||
, ret = [];
|
||||
exports.object.vals = function vals(o) {
|
||||
var keys = Object.keys(o),
|
||||
i = keys.length,
|
||||
ret = [];
|
||||
|
||||
while (i--) {
|
||||
ret.push(o[keys[i]]);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* @see exports.options
|
||||
@ -622,9 +634,10 @@ exports.object.shallowCopy = exports.options;
|
||||
* @param {String} prop
|
||||
*/
|
||||
|
||||
exports.object.hasOwnProperty = function (obj, prop) {
|
||||
return Object.prototype.hasOwnProperty.call(obj, prop);
|
||||
}
|
||||
var hop = Object.prototype.hasOwnProperty;
|
||||
exports.object.hasOwnProperty = function(obj, prop) {
|
||||
return hop.call(obj, prop);
|
||||
};
|
||||
|
||||
/*!
|
||||
* Determine if `val` is null or undefined
|
||||
@ -632,9 +645,9 @@ exports.object.hasOwnProperty = function (obj, prop) {
|
||||
* @return {Boolean}
|
||||
*/
|
||||
|
||||
exports.isNullOrUndefined = function (val) {
|
||||
return null == val
|
||||
}
|
||||
exports.isNullOrUndefined = function(val) {
|
||||
return null == val;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
@ -653,10 +666,10 @@ exports.array = {};
|
||||
* @private
|
||||
*/
|
||||
|
||||
exports.array.flatten = function flatten (arr, filter, ret) {
|
||||
exports.array.flatten = function flatten(arr, filter, ret) {
|
||||
ret || (ret = []);
|
||||
|
||||
arr.forEach(function (item) {
|
||||
arr.forEach(function(item) {
|
||||
if (Array.isArray(item)) {
|
||||
flatten(item, filter, ret);
|
||||
} else {
|
||||
@ -667,5 +680,118 @@ exports.array.flatten = function flatten (arr, filter, ret) {
|
||||
});
|
||||
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Removes duplicate values from an array
|
||||
*
|
||||
* [1, 2, 3, 3, 5] => [1, 2, 3, 5]
|
||||
* [ ObjectId("550988ba0c19d57f697dc45e"), ObjectId("550988ba0c19d57f697dc45e") ]
|
||||
* => [ObjectId("550988ba0c19d57f697dc45e")]
|
||||
*
|
||||
* @param {Array} arr
|
||||
* @return {Array}
|
||||
* @private
|
||||
*/
|
||||
|
||||
exports.array.unique = function(arr) {
|
||||
var primitives = {};
|
||||
var ids = {};
|
||||
var ret = [];
|
||||
var length = arr.length;
|
||||
for (var i = 0; i < length; ++i) {
|
||||
if (typeof arr[i] === 'number' || typeof arr[i] === 'string') {
|
||||
if (primitives[arr[i]]) {
|
||||
continue;
|
||||
}
|
||||
ret.push(arr[i]);
|
||||
primitives[arr[i]] = true;
|
||||
} else if (arr[i] instanceof ObjectId) {
|
||||
if (ids[arr[i].toString()]) {
|
||||
continue;
|
||||
}
|
||||
ret.push(arr[i]);
|
||||
ids[arr[i].toString()] = true;
|
||||
} else {
|
||||
ret.push(arr[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Determines if two buffers are equal.
|
||||
*
|
||||
* @param {Buffer} a
|
||||
* @param {Object} b
|
||||
*/
|
||||
|
||||
exports.buffer = {};
|
||||
exports.buffer.areEqual = function(a, b) {
|
||||
if (!Buffer.isBuffer(a)) return false;
|
||||
if (!Buffer.isBuffer(b)) return false;
|
||||
if (a.length !== b.length) return false;
|
||||
for (var i = 0, len = a.length; i < len; ++i) {
|
||||
if (a[i] !== b[i]) return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
exports.getFunctionName = function(fn) {
|
||||
if (fn.name) {
|
||||
return fn.name;
|
||||
}
|
||||
return (fn.toString().trim().match(/^function\s*([^\s(]+)/) || [])[1];
|
||||
};
|
||||
|
||||
exports.decorate = function(destination, source) {
|
||||
for (var key in source) {
|
||||
destination[key] = source[key];
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* merges to with a copy of from
|
||||
*
|
||||
* @param {Object} to
|
||||
* @param {Object} from
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.mergeClone = function(to, from) {
|
||||
var keys = Object.keys(from),
|
||||
i = keys.length,
|
||||
key;
|
||||
|
||||
while (i--) {
|
||||
key = keys[i];
|
||||
if ('undefined' === typeof to[key]) {
|
||||
// make sure to retain key order here because of a bug handling the $each
|
||||
// operator in mongodb 2.4.4
|
||||
to[key] = exports.clone(from[key], { retainKeyOrder : 1});
|
||||
} else {
|
||||
if (exports.isObject(from[key])) {
|
||||
exports.mergeClone(to[key], from[key]);
|
||||
} else {
|
||||
// make sure to retain key order here because of a bug handling the
|
||||
// $each operator in mongodb 2.4.4
|
||||
to[key] = exports.clone(from[key], { retainKeyOrder : 1});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Executes a function on each element of an array (like _.each)
|
||||
*
|
||||
* @param {Array} arr
|
||||
* @param {Function} fn
|
||||
* @api private
|
||||
*/
|
||||
|
||||
exports.each = function(arr, fn) {
|
||||
for (var i = 0; i < arr.length; ++i) {
|
||||
fn(arr[i]);
|
||||
}
|
||||
};
|
||||
|
10
node_modules/mongoose/lib/virtualtype.js
generated
vendored
10
node_modules/mongoose/lib/virtualtype.js
generated
vendored
@ -13,7 +13,7 @@
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function VirtualType (options, name) {
|
||||
function VirtualType(options, name) {
|
||||
this.path = name;
|
||||
this.getters = [];
|
||||
this.setters = [];
|
||||
@ -35,7 +35,7 @@ function VirtualType (options, name) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
VirtualType.prototype.get = function (fn) {
|
||||
VirtualType.prototype.get = function(fn) {
|
||||
this.getters.push(fn);
|
||||
return this;
|
||||
};
|
||||
@ -57,7 +57,7 @@ VirtualType.prototype.get = function (fn) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
VirtualType.prototype.set = function (fn) {
|
||||
VirtualType.prototype.set = function(fn) {
|
||||
this.setters.push(fn);
|
||||
return this;
|
||||
};
|
||||
@ -71,7 +71,7 @@ VirtualType.prototype.set = function (fn) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
VirtualType.prototype.applyGetters = function (value, scope) {
|
||||
VirtualType.prototype.applyGetters = function(value, scope) {
|
||||
var v = value;
|
||||
for (var l = this.getters.length - 1; l >= 0; l--) {
|
||||
v = this.getters[l].call(scope, v, this);
|
||||
@ -88,7 +88,7 @@ VirtualType.prototype.applyGetters = function (value, scope) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
VirtualType.prototype.applySetters = function (value, scope) {
|
||||
VirtualType.prototype.applySetters = function(value, scope) {
|
||||
var v = value;
|
||||
for (var l = this.setters.length - 1; l >= 0; l--) {
|
||||
v = this.setters[l].call(scope, v, this);
|
||||
|
Reference in New Issue
Block a user