2014-09-14 07:04:16 -04:00
|
|
|
/*!
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
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;
|
2014-09-14 07:04:16 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Array SchemaType constructor
|
|
|
|
*
|
|
|
|
* @param {String} key
|
|
|
|
* @param {SchemaType} cast
|
|
|
|
* @param {Object} options
|
|
|
|
* @inherits SchemaType
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
function SchemaArray(key, cast, options) {
|
2014-09-14 07:04:16 -04:00
|
|
|
if (cast) {
|
|
|
|
var castOptions = {};
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
if ('Object' === utils.getFunctionName(cast.constructor)) {
|
2014-09-14 07:04:16 -04:00
|
|
|
if (cast.type) {
|
|
|
|
// support { type: Woot }
|
|
|
|
castOptions = utils.clone(cast); // do not alter user arguments
|
|
|
|
delete castOptions.type;
|
|
|
|
cast = cast.type;
|
|
|
|
} else {
|
|
|
|
cast = Mixed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// support { type: 'String' }
|
|
|
|
var name = 'string' == typeof cast
|
|
|
|
? cast
|
2015-11-24 22:08:58 -08:00
|
|
|
: utils.getFunctionName(cast);
|
2014-09-14 07:04:16 -04:00
|
|
|
|
|
|
|
var caster = name in Types
|
|
|
|
? Types[name]
|
|
|
|
: cast;
|
|
|
|
|
|
|
|
this.casterConstructor = caster;
|
|
|
|
this.caster = new caster(null, castOptions);
|
|
|
|
if (!(this.caster instanceof EmbeddedDoc)) {
|
|
|
|
this.caster.path = key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
SchemaType.call(this, key, options, 'Array');
|
2014-09-14 07:04:16 -04:00
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
var self = this,
|
|
|
|
defaultArr,
|
|
|
|
fn;
|
2014-09-14 07:04:16 -04:00
|
|
|
|
|
|
|
if (this.defaultValue) {
|
|
|
|
defaultArr = this.defaultValue;
|
|
|
|
fn = 'function' == typeof defaultArr;
|
|
|
|
}
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
this.default(function() {
|
2014-09-14 07:04:16 -04:00
|
|
|
var arr = fn ? defaultArr() : defaultArr || [];
|
|
|
|
return new MongooseArray(arr, self.path, this);
|
|
|
|
});
|
2015-11-24 22:08:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This schema type's name, to defend against minifiers that mangle
|
|
|
|
* function names.
|
|
|
|
*
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
SchemaArray.schemaName = 'Array';
|
2014-09-14 07:04:16 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Inherits from SchemaType.
|
|
|
|
*/
|
2015-11-24 22:08:58 -08:00
|
|
|
SchemaArray.prototype = Object.create( SchemaType.prototype );
|
|
|
|
SchemaArray.prototype.constructor = SchemaArray;
|
2014-09-14 07:04:16 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check required
|
|
|
|
*
|
|
|
|
* @param {Array} value
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
SchemaArray.prototype.checkRequired = function(value) {
|
2014-09-14 07:04:16 -04:00
|
|
|
return !!(value && value.length);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overrides the getters application for the population special-case
|
|
|
|
*
|
|
|
|
* @param {Object} value
|
|
|
|
* @param {Object} scope
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
SchemaArray.prototype.applyGetters = function(value, scope) {
|
2014-09-14 07:04:16 -04:00
|
|
|
if (this.caster.options && this.caster.options.ref) {
|
|
|
|
// means the object id was populated
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SchemaType.prototype.applyGetters.call(this, value, scope);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-11-24 22:08:58 -08:00
|
|
|
* Casts values for set().
|
2014-09-14 07:04:16 -04:00
|
|
|
*
|
|
|
|
* @param {Object} value
|
|
|
|
* @param {Document} doc document that triggers the casting
|
|
|
|
* @param {Boolean} init whether this is an initialization cast
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
SchemaArray.prototype.cast = function(value, doc, init) {
|
2014-09-14 07:04:16 -04:00
|
|
|
if (Array.isArray(value)) {
|
2015-11-24 22:08:58 -08:00
|
|
|
|
|
|
|
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)) {
|
2014-09-14 07:04:16 -04:00
|
|
|
value = new MongooseArray(value, this.path, doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.caster) {
|
|
|
|
try {
|
2015-11-24 22:08:58 -08:00
|
|
|
for (i = 0, l = value.length; i < l; i++) {
|
2014-09-14 07:04:16 -04:00
|
|
|
value[i] = this.caster.cast(value[i], doc, init);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// rethrow
|
|
|
|
throw new CastError(e.type, value, this.path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
} else {
|
2015-11-24 22:08:58 -08:00
|
|
|
// 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);
|
|
|
|
}
|
2014-09-14 07:04:16 -04:00
|
|
|
return this.cast([value], doc, init);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-11-24 22:08:58 -08:00
|
|
|
* Casts values for queries.
|
2014-09-14 07:04:16 -04:00
|
|
|
*
|
|
|
|
* @param {String} $conditional
|
|
|
|
* @param {any} [value]
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
SchemaArray.prototype.castForQuery = function($conditional, value) {
|
|
|
|
var handler,
|
|
|
|
val;
|
|
|
|
|
2014-09-14 07:04:16 -04:00
|
|
|
if (arguments.length === 2) {
|
|
|
|
handler = this.$conditionalHandlers[$conditional];
|
2015-11-24 22:08:58 -08:00
|
|
|
|
|
|
|
if (!handler) {
|
2014-09-14 07:04:16 -04:00
|
|
|
throw new Error("Can't use " + $conditional + " with Array.");
|
2015-11-24 22:08:58 -08:00
|
|
|
}
|
|
|
|
|
2014-09-14 07:04:16 -04:00
|
|
|
val = handler.call(this, value);
|
2015-11-24 22:08:58 -08:00
|
|
|
|
2014-09-14 07:04:16 -04:00
|
|
|
} else {
|
2015-11-24 22:08:58 -08:00
|
|
|
|
2014-09-14 07:04:16 -04:00
|
|
|
val = $conditional;
|
|
|
|
var proto = this.casterConstructor.prototype;
|
|
|
|
var method = proto.castForQuery || proto.cast;
|
|
|
|
var caster = this.caster;
|
2015-11-24 22:08:58 -08:00
|
|
|
|
2014-09-14 07:04:16 -04:00
|
|
|
if (Array.isArray(val)) {
|
2015-11-24 22:08:58 -08:00
|
|
|
val = val.map(function(v) {
|
|
|
|
if (utils.isObject(v) && v.$elemMatch) {
|
|
|
|
return v;
|
|
|
|
}
|
2014-09-14 07:04:16 -04:00
|
|
|
if (method) v = method.call(caster, v);
|
2015-11-24 22:08:58 -08:00
|
|
|
return isMongooseObject(v) ?
|
|
|
|
v.toObject({ virtuals: false }) :
|
|
|
|
v;
|
2014-09-14 07:04:16 -04:00
|
|
|
});
|
2015-11-24 22:08:58 -08:00
|
|
|
|
2014-09-14 07:04:16 -04:00
|
|
|
} else if (method) {
|
|
|
|
val = method.call(caster, val);
|
|
|
|
}
|
|
|
|
}
|
2015-11-24 22:08:58 -08:00
|
|
|
|
|
|
|
return val && isMongooseObject(val) ?
|
|
|
|
val.toObject({ virtuals: false }) :
|
|
|
|
val;
|
2014-09-14 07:04:16 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @ignore
|
2015-11-24 22:08:58 -08:00
|
|
|
*
|
|
|
|
* $atomic cast helpers
|
2014-09-14 07:04:16 -04:00
|
|
|
*/
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
function castToNumber(val) {
|
2014-09-14 07:04:16 -04:00
|
|
|
return Types.Number.prototype.cast.call(this, val);
|
|
|
|
}
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
function castArraysOfNumbers(arr, self) {
|
2014-09-14 07:04:16 -04:00
|
|
|
self || (self = this);
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
arr.forEach(function(v, i) {
|
2014-09-14 07:04:16 -04:00
|
|
|
if (Array.isArray(v)) {
|
2015-11-24 22:08:58 -08:00
|
|
|
castArraysOfNumbers(v, self);
|
2014-09-14 07:04:16 -04:00
|
|
|
} else {
|
|
|
|
arr[i] = castToNumber.call(self, v);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
function cast$near(val) {
|
|
|
|
if (Array.isArray(val)) {
|
|
|
|
castArraysOfNumbers(val, this);
|
|
|
|
return val;
|
|
|
|
}
|
2014-09-14 07:04:16 -04:00
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
if (val && val.$geometry) {
|
|
|
|
return cast$geometry(val, this);
|
|
|
|
}
|
2014-09-14 07:04:16 -04:00
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
return SchemaArray.prototype.castForQuery.call(this, val);
|
|
|
|
}
|
2014-09-14 07:04:16 -04:00
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
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;
|
|
|
|
}
|
2014-09-14 07:04:16 -04:00
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
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);
|
2014-09-14 07:04:16 -04:00
|
|
|
}
|
2015-11-24 22:08:58 -08:00
|
|
|
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);
|
|
|
|
}
|
2014-09-14 07:04:16 -04:00
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
return 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];
|
2014-09-14 07:04:16 -04:00
|
|
|
}
|
2015-11-24 22:08:58 -08:00
|
|
|
return v;
|
|
|
|
}, this);
|
2014-09-14 07:04:16 -04:00
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
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);
|
2014-09-14 07:04:16 -04:00
|
|
|
}
|
2015-11-24 22:08:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return cast(this.casterConstructor.schema, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
function cast$geoIntersects(val) {
|
|
|
|
var geo = val.$geometry;
|
|
|
|
if (!geo) return;
|
|
|
|
|
|
|
|
cast$geometry(val, this);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
var handle = SchemaArray.prototype.$conditionalHandlers = {};
|
|
|
|
|
|
|
|
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;
|
2014-09-14 07:04:16 -04:00
|
|
|
};
|
|
|
|
|
2015-11-24 22:08:58 -08:00
|
|
|
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;
|
|
|
|
|
2014-09-14 07:04:16 -04:00
|
|
|
/*!
|
|
|
|
* Module exports.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = SchemaArray;
|