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

@ -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];