123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- /*!
- * Module dependencies.
- */
- var ArrayType = require('./array');
- var Document = require('../document');
- var MongooseDocumentArray = require('../types/documentarray');
- var SchemaType = require('../schematype');
- var Subdocument = require('../types/embedded');
- /**
- * SubdocsArray SchemaType constructor
- *
- * @param {String} key
- * @param {Schema} schema
- * @param {Object} options
- * @inherits SchemaArray
- * @api private
- */
- function DocumentArray (key, schema, options) {
- // compile an embedded document for this schema
- function EmbeddedDocument () {
- Subdocument.apply(this, arguments);
- }
- EmbeddedDocument.prototype = Object.create(Subdocument.prototype);
- EmbeddedDocument.prototype.$__setSchema(schema);
- EmbeddedDocument.schema = schema;
- // apply methods
- for (var i in schema.methods)
- EmbeddedDocument.prototype[i] = schema.methods[i];
- // apply statics
- for (var i in schema.statics)
- EmbeddedDocument[i] = schema.statics[i];
- EmbeddedDocument.options = options;
- this.schema = schema;
- ArrayType.call(this, key, EmbeddedDocument, options);
- this.schema = schema;
- var path = this.path;
- var fn = this.defaultValue;
- 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 = Object.create( ArrayType.prototype );
- DocumentArray.prototype.constructor = DocumentArray;
- /**
- * Performs local validations first, then validations on each embedded doc
- *
- * @api private
- */
- DocumentArray.prototype.doValidate = function (array, fn, scope) {
- SchemaType.prototype.doValidate.call(this, array, function (err) {
- if (err) {
- return fn(err);
- }
- var count = array && array.length;
- var error;
- if (!count) return fn();
- // 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) {
- // sidestep sparse entries
- var doc = array[i];
- if (!doc) {
- --count || fn(error);
- continue;
- }
- 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
- *
- * @param {Object} value
- * @param {Document} document that triggers the casting
- * @api private
- */
- 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 && 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;
- while (i--) {
- if (!(value[i] instanceof Subdocument) && value[i]) {
- if (init) {
- selected || (selected = scopePaths(this, doc.$__.selected, init));
- subdoc = new this.casterConstructor(null, value, true, selected, i);
- value[i] = subdoc.init(value[i]);
- } else {
- 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, undefined, undefined, i);
- // 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.
- * Necessary for proper default application of subdocument values.
- *
- * @param {DocumentArray} array - the array to scope `fields` paths
- * @param {Object|undefined} fields - the root fields selected in the query
- * @param {Boolean|undefined} init - if we are being created part of a query result
- */
- function scopePaths (array, fields, init) {
- if (!(init && fields)) return undefined;
- var path = array.path + '.'
- , keys = Object.keys(fields)
- , i = keys.length
- , selected = {}
- , hasKeys
- , key
- while (i--) {
- key = keys[i];
- if (0 === key.indexOf(path)) {
- hasKeys || (hasKeys = true);
- selected[key.substring(path.length)] = fields[key];
- }
- }
- return hasKeys && selected || undefined;
- }
- /*!
- * Module exports.
- */
- module.exports = DocumentArray;
|