Updated mongoose

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

View File

@ -2,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.
*/