This commit is contained in:
Jonasz Bigda
2023-03-25 21:51:42 +01:00
parent 0db1d5117e
commit b332e9ceb0
1044 changed files with 37502 additions and 63938 deletions

View File

@@ -25,6 +25,7 @@ let MongooseArray;
let EmbeddedDoc;
const isNestedArraySymbol = Symbol('mongoose#isNestedArray');
const emptyOpts = Object.freeze({});
/**
* Array SchemaType constructor
@@ -60,6 +61,10 @@ function SchemaArray(key, cast, options, schemaOptions) {
}
}
if (options != null && options.ref != null && castOptions.ref == null) {
castOptions.ref = options.ref;
}
if (cast === Object) {
cast = Mixed;
}
@@ -81,16 +86,16 @@ function SchemaArray(key, cast, options, schemaOptions) {
if (typeof caster === 'function' &&
!caster.$isArraySubdocument &&
!caster.$isSchemaMap) {
this.caster = new caster(null, castOptions);
const path = this.caster instanceof EmbeddedDoc ? null : key;
this.caster = new caster(path, castOptions);
} else {
this.caster = caster;
if (!(this.caster instanceof EmbeddedDoc)) {
this.caster.path = key;
}
}
this.$embeddedSchemaType = this.caster;
if (!(this.caster instanceof EmbeddedDoc)) {
this.caster.path = key;
}
}
this.$isMongooseArray = true;
@@ -116,7 +121,7 @@ function SchemaArray(key, cast, options, schemaOptions) {
// Leave it up to `cast()` to convert the array
return arr;
};
defaultFn.$runBeforeSetters = true;
defaultFn.$runBeforeSetters = !fn;
this.default(defaultFn);
}
}
@@ -141,6 +146,10 @@ SchemaArray.schemaName = 'Array';
SchemaArray.options = { castNonArrays: true };
/*!
* ignore
*/
SchemaArray.defaultOptions = {};
/**
@@ -158,7 +167,6 @@ SchemaArray.defaultOptions = {};
* @param {*} value - value for option
* @return {undefined}
* @function set
* @static
* @api public
*/
SchemaArray.set = SchemaType.set;
@@ -191,7 +199,6 @@ SchemaArray._checkRequired = SchemaType.prototype.checkRequired;
* @param {Function} fn
* @return {Function}
* @function checkRequired
* @static
* @api public
*/
@@ -242,7 +249,13 @@ SchemaArray.prototype.enum = function() {
}
break;
}
arr.caster.enum.apply(arr.caster, arguments);
let enumArray = arguments;
if (!Array.isArray(arguments) && utils.isObject(arguments)) {
enumArray = utils.object.vals(enumArray);
}
arr.caster.enum.apply(arr.caster, enumArray);
return this;
};
@@ -255,23 +268,30 @@ SchemaArray.prototype.enum = function() {
*/
SchemaArray.prototype.applyGetters = function(value, scope) {
if (this.caster.options && this.caster.options.ref) {
if (scope != null && scope.$__ != null && scope.populated(this.path)) {
// means the object id was populated
return value;
}
return SchemaType.prototype.applyGetters.call(this, value, scope);
const ret = SchemaType.prototype.applyGetters.call(this, value, scope);
if (Array.isArray(ret)) {
const len = ret.length;
for (let i = 0; i < len; ++i) {
ret[i] = this.caster.applyGetters(ret[i], scope);
}
}
return ret;
};
SchemaArray.prototype._applySetters = function(value, scope, init, priorVal) {
if (this.casterConstructor instanceof SchemaArray &&
if (this.casterConstructor.$isMongooseArray &&
SchemaArray.options.castNonArrays &&
!this[isNestedArraySymbol]) {
// Check nesting levels and wrap in array if necessary
let depth = 0;
let arr = this;
while (arr != null &&
arr instanceof SchemaArray &&
arr.$isMongooseArray &&
!arr.$isMongooseDocumentArray) {
++depth;
arr = arr.casterConstructor;
@@ -280,7 +300,7 @@ SchemaArray.prototype._applySetters = function(value, scope, init, priorVal) {
// No need to wrap empty arrays
if (value != null && value.length > 0) {
const valueDepth = arrayDepth(value);
if (valueDepth.min === valueDepth.max && valueDepth.max < depth) {
if (valueDepth.min === valueDepth.max && valueDepth.max < depth && valueDepth.containsNonArrayItem) {
for (let i = valueDepth.max; i < depth; ++i) {
value = [value];
}
@@ -308,7 +328,8 @@ SchemaArray.prototype.cast = function(value, doc, init, prev, options) {
let l;
if (Array.isArray(value)) {
if (!value.length && doc) {
const len = value.length;
if (!len && doc) {
const indexes = doc.schema.indexedPaths();
const arrayPath = this.path;
@@ -333,39 +354,42 @@ SchemaArray.prototype.cast = function(value, doc, init, prev, options) {
}
}
if (!(value && value.isMongooseArray)) {
value = new MongooseArray(value, this.path, doc);
} else if (value && value.isMongooseArray) {
// We need to create a new array, otherwise change tracking will
// update the old doc (gh-4449)
value = new MongooseArray(value, this.path, doc);
}
options = options || emptyOpts;
const isPopulated = doc != null && doc.$__ != null && doc.populated(this.path);
if (isPopulated) {
value = MongooseArray(value, options.path || this._arrayPath || this.path, doc, this);
if (init && doc != null && doc.$__ != null && doc.populated(this.path)) {
return value;
}
if (this.caster && this.casterConstructor !== Mixed) {
const caster = this.caster;
const isMongooseArray = caster.$isMongooseArray;
const isArrayOfNumbers = caster.instance === 'Number';
if (caster && this.casterConstructor !== Mixed) {
try {
for (i = 0, l = value.length; i < l; i++) {
for (i = 0; i < len; i++) {
// Special case: number arrays disallow undefined.
// Re: gh-840
// See commit 1298fe92d2c790a90594bd08199e45a4a09162a6
if (this.caster.instance === 'Number' && value[i] === void 0) {
if (isArrayOfNumbers && value[i] === void 0) {
throw new MongooseError('Mongoose number arrays disallow storing undefined');
}
const opts = {};
if (options != null && options.arrayPath != null) {
opts.arrayPath = options.arrayPath + '.' + i;
} else if (this.caster._arrayPath != null) {
opts.arrayPath = this.caster._arrayPath.slice(0, -2) + '.' + i;
// Perf: creating `arrayPath` is expensive for large arrays.
// We only need `arrayPath` if this is a nested array, so
// skip if possible.
if (isMongooseArray) {
if (options.arrayPath != null) {
opts.arrayPathIndex = i;
} else if (caster._arrayParentPath != null) {
opts.arrayPathIndex = i;
}
}
value[i] = this.caster.cast(value[i], doc, init, void 0, opts);
value[i] = caster.applySetters(value[i], doc, init, void 0, opts);
}
} catch (e) {
// rethrow
throw new CastError('[' + e.kind + ']', util.inspect(value), this.path, e, this);
throw new CastError('[' + e.kind + ']', util.inspect(value), this.path + '.' + i, e, this);
}
}
@@ -384,12 +408,50 @@ SchemaArray.prototype.cast = function(value, doc, init, prev, options) {
throw new CastError('Array', util.inspect(value), this.path, null, this);
};
/*!
* ignore
*/
SchemaArray.prototype._castForPopulate = function _castForPopulate(value, doc) {
// lazy load
MongooseArray || (MongooseArray = require('../types').Array);
if (Array.isArray(value)) {
let i;
const len = value.length;
const caster = this.caster;
if (caster && this.casterConstructor !== Mixed) {
try {
for (i = 0; i < len; i++) {
const opts = {};
// Perf: creating `arrayPath` is expensive for large arrays.
// We only need `arrayPath` if this is a nested array, so
// skip if possible.
if (caster.$isMongooseArray && caster._arrayParentPath != null) {
opts.arrayPathIndex = i;
}
value[i] = caster.cast(value[i], doc, false, void 0, opts);
}
} catch (e) {
// rethrow
throw new CastError('[' + e.kind + ']', util.inspect(value), this.path + '.' + i, e, this);
}
}
return value;
}
throw new CastError('Array', util.inspect(value), this.path, null, this);
};
/*!
* Ignore
*/
SchemaArray.prototype.discriminator = function(name, schema) {
let arr = this; // eslint-disable-line consistent-this
let arr = this;
while (arr.$isMongooseArray && !arr.$isMongooseDocumentArray) {
arr = arr.casterConstructor;
if (arr == null || typeof arr === 'function') {
@@ -447,7 +509,7 @@ SchemaArray.prototype.castForQuery = function($conditional, value) {
Constructor.discriminators[val[Constructor.schema.options.discriminatorKey]]) {
Constructor = Constructor.discriminators[val[Constructor.schema.options.discriminatorKey]];
} else {
const constructorByValue = getDiscriminatorByValue(Constructor, val[Constructor.schema.options.discriminatorKey]);
const constructorByValue = getDiscriminatorByValue(Constructor.discriminators, val[Constructor.schema.options.discriminatorKey]);
if (constructorByValue) {
Constructor = constructorByValue;
}
@@ -537,18 +599,24 @@ handle.$all = cast$all;
handle.$options = String;
handle.$elemMatch = cast$elemMatch;
handle.$geoIntersects = geospatial.cast$geoIntersects;
handle.$or = handle.$and = function(val) {
if (!Array.isArray(val)) {
throw new TypeError('conditional $or/$and require array');
}
handle.$or = createLogicalQueryOperatorHandler('$or');
handle.$and = createLogicalQueryOperatorHandler('$and');
handle.$nor = createLogicalQueryOperatorHandler('$nor');
const ret = [];
for (const obj of val) {
ret.push(cast(this.casterConstructor.schema, obj));
}
function createLogicalQueryOperatorHandler(op) {
return function logicalQueryOperatorHandler(val) {
if (!Array.isArray(val)) {
throw new TypeError('conditional ' + op + ' requires an array');
}
return ret;
};
const ret = [];
for (const obj of val) {
ret.push(cast(this.casterConstructor.schema, obj));
}
return ret;
};
}
handle.$near =
handle.$nearSphere = geospatial.cast$near;