@datanimbus/mongoose 6.13.13 → 9.13.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +80 -70
- package/eslint.config.mjs +202 -0
- package/index.js +2 -1
- package/lib/aggregate.js +276 -174
- package/lib/cast/bigint.js +46 -0
- package/lib/cast/boolean.js +3 -3
- package/lib/cast/decimal128.js +4 -1
- package/lib/cast/double.js +56 -0
- package/lib/cast/int32.js +42 -0
- package/lib/cast/number.js +6 -6
- package/lib/cast/objectid.js +3 -3
- package/lib/cast/string.js +3 -3
- package/lib/cast/uuid.js +35 -0
- package/lib/cast.js +100 -92
- package/lib/collection.js +31 -22
- package/lib/connection.js +792 -507
- package/lib/constants.js +73 -0
- package/lib/cursor/{AggregationCursor.js → aggregationCursor.js} +205 -52
- package/lib/cursor/changeStream.js +218 -0
- package/lib/cursor/queryCursor.js +676 -0
- package/lib/defaults/schemaDefaultsCache.js +62 -0
- package/lib/document.js +2419 -1306
- package/lib/drivers/node-mongodb-native/bulkWriteResult.js +5 -0
- package/lib/drivers/node-mongodb-native/collection.js +101 -135
- package/lib/drivers/node-mongodb-native/connection.js +386 -28
- package/lib/drivers/node-mongodb-native/index.js +3 -5
- package/lib/error/browserMissingSchema.js +5 -4
- package/lib/error/bulkSaveIncompleteError.js +44 -0
- package/lib/error/cast.js +30 -21
- package/lib/error/createCollectionsError.js +26 -0
- package/lib/error/divergentArray.js +11 -9
- package/lib/error/eachAsyncMultiError.js +2 -2
- package/lib/error/index.js +38 -29
- package/lib/error/invalidSchemaOption.js +32 -0
- package/lib/error/messages.js +4 -3
- package/lib/error/missingSchema.js +8 -6
- package/lib/error/notFound.js +7 -5
- package/lib/error/objectExpected.js +9 -8
- package/lib/error/objectParameter.js +12 -11
- package/lib/error/overwriteModel.js +7 -6
- package/lib/error/parallelSave.js +11 -8
- package/lib/error/parallelValidate.js +9 -7
- package/lib/error/serverSelection.js +1 -0
- package/lib/error/setOptionError.js +11 -9
- package/lib/error/strict.js +12 -10
- package/lib/error/strictPopulate.js +11 -9
- package/lib/error/syncIndexes.js +2 -2
- package/lib/error/validation.js +11 -17
- package/lib/error/validator.js +9 -8
- package/lib/error/version.js +12 -10
- package/lib/filters/operators.js +8 -0
- package/lib/helpers/aggregate/prepareDiscriminatorPipeline.js +4 -4
- package/lib/helpers/buildMiddlewareFilter.js +42 -0
- package/lib/helpers/clone.js +121 -35
- package/lib/helpers/common.js +7 -7
- package/lib/helpers/cursor/eachAsync.js +14 -11
- package/lib/helpers/cursor/waitForInitialization.js +17 -0
- package/lib/helpers/discriminator/applyEmbeddedDiscriminators.js +36 -0
- package/lib/helpers/discriminator/areDiscriminatorValuesEqual.js +1 -1
- package/lib/helpers/discriminator/getConstructor.js +9 -6
- package/lib/helpers/discriminator/getDiscriminatorByKey.js +1 -1
- package/lib/helpers/discriminator/getDiscriminatorByValue.js +1 -1
- package/lib/helpers/discriminator/getDiscriminatorValueFromFilter.js +30 -0
- package/lib/helpers/discriminator/getSchemaDiscriminatorByValue.js +1 -1
- package/lib/helpers/discriminator/mergeDiscriminatorSchema.js +41 -9
- package/lib/helpers/document/applyDefaults.js +82 -14
- package/lib/helpers/document/applyTimestamps.js +106 -0
- package/lib/helpers/document/applyVirtuals.js +147 -0
- package/lib/helpers/document/cleanModifiedSubpaths.js +14 -4
- package/lib/helpers/document/compile.js +20 -15
- package/lib/helpers/document/getDeepestSubdocumentForPath.js +38 -0
- package/lib/helpers/document/getEmbeddedDiscriminatorPath.js +6 -3
- package/lib/helpers/document/handleSpreadDoc.js +2 -1
- package/lib/helpers/document/isInPathsToSave.js +24 -0
- package/lib/helpers/document/toObjectRecursive.js +49 -0
- package/lib/helpers/get.js +1 -1
- package/lib/helpers/getDefaultBulkwriteResult.js +11 -20
- package/lib/helpers/getInheritedProperty.js +28 -0
- package/lib/helpers/getOwnProperty.js +19 -0
- package/lib/helpers/indexes/applySchemaCollation.js +1 -1
- package/lib/helpers/indexes/decorateDiscriminatorIndexOptions.js +1 -1
- package/lib/helpers/indexes/getRelatedIndexes.js +8 -4
- package/lib/helpers/indexes/isDefaultIdIndex.js +1 -1
- package/lib/helpers/indexes/isIndexEqual.js +13 -12
- package/lib/helpers/indexes/isIndexSpecEqual.js +32 -0
- package/lib/helpers/indexes/isTimeseriesIndex.js +16 -0
- package/lib/helpers/isBsonType.js +3 -6
- package/lib/helpers/isMongooseDocument.js +24 -0
- package/lib/helpers/isMongooseObject.js +6 -4
- package/lib/helpers/isObject.js +2 -2
- package/lib/helpers/isPOJO.js +12 -0
- package/lib/helpers/isSimpleValidator.js +2 -2
- package/lib/helpers/minimize.js +41 -0
- package/lib/helpers/model/applyDefaultsToPOJO.js +2 -2
- package/lib/helpers/model/applyHooks.js +72 -54
- package/lib/helpers/model/applyMethods.js +3 -3
- package/lib/helpers/model/applyStaticHooks.js +20 -50
- package/lib/helpers/model/castBulkWrite.js +278 -172
- package/lib/helpers/model/decorateBulkWriteResult.js +8 -0
- package/lib/helpers/model/discriminator.js +77 -21
- package/lib/helpers/omitUndefined.js +20 -0
- package/lib/helpers/parallelLimit.js +61 -34
- package/lib/helpers/path/setDottedPath.js +1 -1
- package/lib/helpers/pathTrie.js +167 -0
- package/lib/helpers/pluralize.js +5 -4
- package/lib/helpers/populate/assignRawDocsToIdStructure.js +12 -7
- package/lib/helpers/populate/assignVals.js +46 -21
- package/lib/helpers/populate/createPopulateQueryFilter.js +13 -5
- package/lib/helpers/populate/getModelsMapForPopulate.js +250 -108
- package/lib/helpers/populate/getSchemaTypes.js +27 -30
- package/lib/helpers/populate/getVirtual.js +31 -0
- package/lib/helpers/populate/markArraySubdocsPopulated.js +6 -4
- package/lib/helpers/populate/modelNamesFromRefPath.js +11 -5
- package/lib/helpers/populate/removeDeselectedForeignField.js +3 -2
- package/lib/helpers/populate/setInheritedReadOptions.js +21 -0
- package/lib/helpers/populate/setPopulatedVirtualValue.js +33 -0
- package/lib/helpers/populate/splitPopulateQuery.js +81 -0
- package/lib/helpers/printJestWarning.js +1 -1
- package/lib/helpers/processConnectionOptions.js +7 -2
- package/lib/helpers/projection/applyProjection.js +8 -2
- package/lib/helpers/projection/hasIncludedChildren.js +1 -1
- package/lib/helpers/projection/isExclusive.js +8 -6
- package/lib/helpers/projection/isInclusive.js +2 -1
- package/lib/helpers/projection/isPathExcluded.js +3 -3
- package/lib/helpers/projection/isPathSelectedInclusive.js +1 -11
- package/lib/helpers/projection/isSubpath.js +1 -1
- package/lib/helpers/projection/parseProjection.js +9 -4
- package/lib/helpers/query/applyGlobalOption.js +9 -9
- package/lib/helpers/query/cast$expr.js +52 -23
- package/lib/helpers/query/castFilterPath.js +22 -22
- package/lib/helpers/query/castUpdate.js +207 -79
- package/lib/helpers/query/cloneMongooseOptions.js +44 -0
- package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +27 -13
- package/lib/helpers/query/handleImmutable.js +18 -2
- package/lib/helpers/query/handleReadPreferenceAliases.js +23 -0
- package/lib/helpers/query/sanitizeFilter.js +3 -2
- package/lib/helpers/query/selectPopulatedFields.js +13 -0
- package/lib/helpers/query/validOps.js +1 -22
- package/lib/helpers/schema/applyPlugins.js +2 -2
- package/lib/helpers/schema/applyReadConcern.js +20 -0
- package/lib/helpers/schema/applyWriteConcern.js +12 -3
- package/lib/helpers/schema/cleanPositionalOperators.js +5 -3
- package/lib/helpers/schema/getIndexes.js +20 -13
- package/lib/helpers/schema/getKeysInSchemaOrder.js +13 -16
- package/lib/helpers/schema/getPath.js +11 -3
- package/lib/helpers/schema/getSubdocumentStrictValue.js +32 -0
- package/lib/helpers/schema/handleIdOption.js +1 -1
- package/lib/helpers/schema/idGetter.js +7 -2
- package/lib/helpers/schema/merge.js +9 -1
- package/lib/helpers/schematype/getConditionalHandler.js +23 -0
- package/lib/helpers/schematype/handleImmutable.js +1 -1
- package/lib/helpers/setDefaultsOnInsert.js +85 -43
- package/lib/helpers/symbols.js +4 -4
- package/lib/helpers/timestamps/setDocumentTimestamps.js +2 -2
- package/lib/helpers/timestamps/setupTimestamps.js +69 -37
- package/lib/helpers/update/applyTimestampsToChildren.js +1 -1
- package/lib/helpers/update/applyTimestampsToUpdate.js +53 -37
- package/lib/helpers/update/castArrayFilters.js +68 -29
- package/lib/helpers/update/decorateUpdateWithVersionKey.js +35 -0
- package/lib/helpers/update/modifiedPaths.js +2 -2
- package/lib/helpers/update/removeUnusedArrayFilters.js +11 -2
- package/lib/helpers/updateValidators.js +87 -128
- package/lib/index.js +6 -1320
- package/lib/internal.js +7 -1
- package/lib/model.js +2587 -2618
- package/lib/modifiedPathsSnapshot.js +9 -0
- package/lib/mongoose.js +1456 -0
- package/lib/options/saveOptions.js +2 -0
- package/lib/options/{SchemaArrayOptions.js → schemaArrayOptions.js} +16 -5
- package/lib/options/{SchemaBufferOptions.js → schemaBufferOptions.js} +2 -2
- package/lib/options/{SchemaDateOptions.js → schemaDateOptions.js} +1 -1
- package/lib/options/{SchemaDocumentArrayOptions.js → schemaDocumentArrayOptions.js} +24 -1
- package/lib/options/{SchemaMapOptions.js → schemaMapOptions.js} +1 -1
- package/lib/options/{SchemaNumberOptions.js → schemaNumberOptions.js} +5 -5
- package/lib/options/{SchemaObjectIdOptions.js → schemaObjectIdOptions.js} +4 -4
- package/lib/options/{SchemaStringOptions.js → schemaStringOptions.js} +10 -10
- package/lib/options/schemaSubdocumentOptions.js +89 -0
- package/lib/options/{SchemaTypeOptions.js → schemaTypeOptions.js} +28 -14
- package/lib/options/schemaUnionOptions.js +32 -0
- package/lib/options/{VirtualOptions.js → virtualOptions.js} +13 -13
- package/lib/options.js +4 -1
- package/lib/plugins/index.js +0 -2
- package/lib/plugins/saveSubdocs.js +85 -50
- package/lib/plugins/sharding.js +43 -15
- package/lib/plugins/trackTransaction.js +46 -34
- package/lib/query.js +1844 -1909
- package/lib/{queryhelpers.js → queryHelpers.js} +184 -81
- package/lib/schema/array.js +229 -169
- package/lib/schema/bigint.js +281 -0
- package/lib/schema/boolean.js +90 -26
- package/lib/schema/buffer.js +119 -50
- package/lib/schema/date.js +102 -38
- package/lib/schema/decimal128.js +88 -37
- package/lib/schema/{documentarray.js → documentArray.js} +259 -151
- package/lib/schema/documentArrayElement.js +180 -0
- package/lib/schema/double.js +244 -0
- package/lib/schema/index.js +12 -19
- package/lib/schema/int32.js +288 -0
- package/lib/schema/map.js +129 -20
- package/lib/schema/mixed.js +40 -26
- package/lib/schema/number.js +128 -51
- package/lib/schema/{objectid.js → objectId.js} +89 -47
- package/lib/schema/operators/exists.js +1 -1
- package/lib/schema/operators/geospatial.js +2 -2
- package/lib/schema/operators/text.js +4 -4
- package/lib/schema/string.js +125 -66
- package/lib/schema/subdocument.js +443 -0
- package/lib/schema/symbols.js +2 -0
- package/lib/schema/union.js +167 -0
- package/lib/schema/uuid.js +90 -120
- package/lib/schema.js +1267 -388
- package/lib/{schematype.js → schemaType.js} +481 -246
- package/lib/standardSchema/convertErrorToIssues.js +20 -0
- package/lib/{statemachine.js → stateMachine.js} +66 -17
- package/lib/tracing.js +38 -0
- package/lib/types/array/index.js +13 -10
- package/lib/types/array/methods/index.js +243 -63
- package/lib/types/{ArraySubdocument.js → arraySubdocument.js} +35 -14
- package/lib/types/buffer.js +31 -14
- package/lib/types/decimal128.js +1 -1
- package/lib/types/{DocumentArray → documentArray}/index.js +13 -13
- package/lib/types/{DocumentArray → documentArray}/methods/index.js +197 -31
- package/lib/types/double.js +13 -0
- package/lib/types/index.js +5 -2
- package/lib/types/map.js +62 -25
- package/lib/types/objectid.js +2 -2
- package/lib/types/subdocument.js +79 -58
- package/lib/types/uuid.js +13 -0
- package/lib/utils.js +201 -155
- package/lib/{validoptions.js → validOptions.js} +10 -2
- package/lib/{virtualtype.js → virtualType.js} +47 -18
- package/package.json +75 -92
- package/types/aggregate.d.ts +31 -7
- package/types/augmentations.d.ts +2 -2
- package/types/collection.d.ts +21 -16
- package/types/connection.d.ts +82 -27
- package/types/cursor.d.ts +15 -10
- package/types/document.d.ts +137 -56
- package/types/error.d.ts +11 -6
- package/types/expressions.d.ts +33 -2
- package/types/index.d.ts +805 -190
- package/types/indexes.d.ts +85 -13
- package/types/inferhydrateddoctype.d.ts +151 -0
- package/types/inferrawdoctype.d.ts +201 -0
- package/types/inferschematype.d.ts +287 -128
- package/types/middlewares.d.ts +50 -7
- package/types/models.d.ts +1555 -268
- package/types/mongooseoptions.d.ts +56 -7
- package/types/pipelinestage.d.ts +42 -3
- package/types/populate.d.ts +63 -1
- package/types/query.d.ts +592 -255
- package/types/schemaoptions.d.ts +78 -12
- package/types/schematypes.d.ts +254 -65
- package/types/session.d.ts +0 -4
- package/types/tracing.d.ts +10 -0
- package/types/types.d.ts +25 -8
- package/types/utility.d.ts +329 -30
- package/types/validation.d.ts +19 -14
- package/types/virtuals.d.ts +10 -10
- package/.eslintrc.js +0 -232
- package/.mocharc.yml +0 -4
- package/browser.js +0 -8
- package/lgtm.yml +0 -12
- package/lib/browser.js +0 -158
- package/lib/browserDocument.js +0 -101
- package/lib/cursor/ChangeStream.js +0 -154
- package/lib/cursor/QueryCursor.js +0 -545
- package/lib/document_provider.js +0 -30
- package/lib/drivers/browser/ReadPreference.js +0 -7
- package/lib/drivers/browser/binary.js +0 -14
- package/lib/drivers/browser/decimal128.js +0 -7
- package/lib/drivers/browser/index.js +0 -16
- package/lib/drivers/browser/objectid.js +0 -29
- package/lib/drivers/node-mongodb-native/ReadPreference.js +0 -47
- package/lib/drivers/node-mongodb-native/binary.js +0 -10
- package/lib/drivers/node-mongodb-native/decimal128.js +0 -7
- package/lib/drivers/node-mongodb-native/objectid.js +0 -16
- package/lib/error/disconnected.js +0 -33
- package/lib/helpers/printStrictQueryWarning.js +0 -11
- package/lib/helpers/promiseOrCallback.js +0 -55
- package/lib/helpers/query/applyQueryMiddleware.js +0 -79
- package/lib/helpers/query/completeMany.js +0 -52
- package/lib/helpers/query/wrapThunk.js +0 -36
- package/lib/options/SchemaSubdocumentOptions.js +0 -42
- package/lib/options/removeOptions.js +0 -14
- package/lib/plugins/removeSubdocs.js +0 -31
- package/lib/plugins/validateBeforeSave.js +0 -45
- package/lib/promise_provider.js +0 -49
- package/lib/schema/DocumentArrayElement.js +0 -100
- package/lib/schema/SubdocumentPath.js +0 -369
- package/scripts/build-browser.js +0 -18
- package/scripts/create-tarball.js +0 -7
- package/scripts/generateSearch.js +0 -154
- package/scripts/tsc-diagnostics-check.js +0 -15
- package/tools/auth.js +0 -31
- package/tools/repl.js +0 -35
- package/tools/sharded.js +0 -46
- package/tsconfig.json +0 -9
- /package/lib/{connectionstate.js → connectionState.js} +0 -0
- /package/lib/helpers/populate/{SkipPopulateValue.js → skipPopulateValue.js} +0 -0
- /package/lib/options/{PopulateOptions.js → populateOptions.js} +0 -0
- /package/lib/types/{DocumentArray → documentArray}/isMongooseDocumentArray.js +0 -0
package/lib/schema/array.js
CHANGED
|
@@ -7,12 +7,16 @@
|
|
|
7
7
|
const $exists = require('./operators/exists');
|
|
8
8
|
const $type = require('./operators/type');
|
|
9
9
|
const MongooseError = require('../error/mongooseError');
|
|
10
|
-
const SchemaArrayOptions = require('../options/
|
|
11
|
-
const SchemaType = require('../
|
|
10
|
+
const SchemaArrayOptions = require('../options/schemaArrayOptions');
|
|
11
|
+
const SchemaType = require('../schemaType');
|
|
12
12
|
const CastError = SchemaType.CastError;
|
|
13
13
|
const Mixed = require('./mixed');
|
|
14
|
+
const VirtualOptions = require('../options/virtualOptions');
|
|
15
|
+
const VirtualType = require('../virtualType');
|
|
14
16
|
const arrayDepth = require('../helpers/arrayDepth');
|
|
15
17
|
const cast = require('../cast');
|
|
18
|
+
const clone = require('../helpers/clone');
|
|
19
|
+
const getConstructorName = require('../helpers/getConstructorName');
|
|
16
20
|
const isOperator = require('../helpers/query/isOperator');
|
|
17
21
|
const util = require('util');
|
|
18
22
|
const utils = require('../utils');
|
|
@@ -20,30 +24,31 @@ const castToNumber = require('./operators/helpers').castToNumber;
|
|
|
20
24
|
const geospatial = require('./operators/geospatial');
|
|
21
25
|
const getDiscriminatorByKey = require('../helpers/discriminator/getDiscriminatorByKey');
|
|
22
26
|
const getDiscriminatorByValue = require('../helpers/discriminator/getDiscriminatorByValue');
|
|
27
|
+
const getConditionalHandler = require('../helpers/schematype/getConditionalHandler');
|
|
23
28
|
|
|
24
29
|
let MongooseArray;
|
|
25
30
|
let EmbeddedDoc;
|
|
26
31
|
|
|
27
|
-
const isNestedArraySymbol = Symbol('mongoose#isNestedArray');
|
|
28
32
|
const emptyOpts = Object.freeze({});
|
|
29
33
|
|
|
30
34
|
/**
|
|
31
35
|
* Array SchemaType constructor
|
|
32
36
|
*
|
|
33
|
-
* @param {
|
|
37
|
+
* @param {string} key
|
|
34
38
|
* @param {SchemaType} cast
|
|
35
|
-
* @param {
|
|
36
|
-
* @param {
|
|
39
|
+
* @param {object} options
|
|
40
|
+
* @param {object} schemaOptions
|
|
41
|
+
* @param {Schema} parentSchema
|
|
37
42
|
* @inherits SchemaType
|
|
38
43
|
* @api public
|
|
39
44
|
*/
|
|
40
45
|
|
|
41
|
-
function SchemaArray(key, cast, options, schemaOptions) {
|
|
46
|
+
function SchemaArray(key, cast, options, schemaOptions, parentSchema) {
|
|
42
47
|
// lazy load
|
|
43
48
|
EmbeddedDoc || (EmbeddedDoc = require('../types').Embedded);
|
|
44
49
|
|
|
45
50
|
let typeKey = 'type';
|
|
46
|
-
if (schemaOptions
|
|
51
|
+
if (schemaOptions?.typeKey) {
|
|
47
52
|
typeKey = schemaOptions.typeKey;
|
|
48
53
|
}
|
|
49
54
|
this.schemaOptions = schemaOptions;
|
|
@@ -54,7 +59,7 @@ function SchemaArray(key, cast, options, schemaOptions) {
|
|
|
54
59
|
if (utils.isPOJO(cast)) {
|
|
55
60
|
if (cast[typeKey]) {
|
|
56
61
|
// support { type: Woot }
|
|
57
|
-
castOptions =
|
|
62
|
+
castOptions = clone(cast); // do not alter user arguments
|
|
58
63
|
delete castOptions[typeKey];
|
|
59
64
|
cast = cast[typeKey];
|
|
60
65
|
} else {
|
|
@@ -62,7 +67,7 @@ function SchemaArray(key, cast, options, schemaOptions) {
|
|
|
62
67
|
}
|
|
63
68
|
}
|
|
64
69
|
|
|
65
|
-
if (options
|
|
70
|
+
if (options?.ref != null && castOptions.ref == null) {
|
|
66
71
|
castOptions.ref = options.ref;
|
|
67
72
|
}
|
|
68
73
|
|
|
@@ -76,32 +81,26 @@ function SchemaArray(key, cast, options, schemaOptions) {
|
|
|
76
81
|
: utils.getFunctionName(cast);
|
|
77
82
|
|
|
78
83
|
const Types = require('./index.js');
|
|
79
|
-
const
|
|
84
|
+
const schemaTypeDefinition = Object.hasOwn(Types, name) ? Types[name] : cast;
|
|
80
85
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if (
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
this.caster = new caster(path, castOptions);
|
|
92
|
-
} else {
|
|
93
|
-
this.caster = caster;
|
|
94
|
-
if (!(this.caster instanceof EmbeddedDoc)) {
|
|
95
|
-
this.caster.path = key;
|
|
86
|
+
if (typeof schemaTypeDefinition === 'function') {
|
|
87
|
+
if (schemaTypeDefinition === SchemaArray) {
|
|
88
|
+
this.embeddedSchemaType = new schemaTypeDefinition(key, castOptions, schemaOptions, null, parentSchema);
|
|
89
|
+
} else {
|
|
90
|
+
this.embeddedSchemaType = new schemaTypeDefinition(key, castOptions, schemaOptions, parentSchema);
|
|
91
|
+
}
|
|
92
|
+
} else if (schemaTypeDefinition instanceof SchemaType) {
|
|
93
|
+
this.embeddedSchemaType = schemaTypeDefinition;
|
|
94
|
+
if (!(this.embeddedSchemaType instanceof EmbeddedDoc)) {
|
|
95
|
+
this.embeddedSchemaType.path = key;
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
this.$embeddedSchemaType = this.caster;
|
|
100
99
|
}
|
|
101
100
|
|
|
102
101
|
this.$isMongooseArray = true;
|
|
103
102
|
|
|
104
|
-
SchemaType.call(this, key, options, 'Array');
|
|
103
|
+
SchemaType.call(this, key, options, 'Array', parentSchema);
|
|
105
104
|
|
|
106
105
|
let defaultArr;
|
|
107
106
|
let fn;
|
|
@@ -111,7 +110,7 @@ function SchemaArray(key, cast, options, schemaOptions) {
|
|
|
111
110
|
fn = typeof defaultArr === 'function';
|
|
112
111
|
}
|
|
113
112
|
|
|
114
|
-
if (!('defaultValue' in this) || this.defaultValue
|
|
113
|
+
if (!('defaultValue' in this) || this.defaultValue != null) {
|
|
115
114
|
const defaultFn = function() {
|
|
116
115
|
// Leave it up to `cast()` to convert the array
|
|
117
116
|
return fn
|
|
@@ -157,19 +156,33 @@ SchemaArray.defaultOptions = {};
|
|
|
157
156
|
* #### Example:
|
|
158
157
|
*
|
|
159
158
|
* // Make all Array instances have `required` of true by default.
|
|
160
|
-
* mongoose.Schema.Array.set('required', true);
|
|
159
|
+
* mongoose.Schema.Types.Array.set('required', true);
|
|
161
160
|
*
|
|
162
161
|
* const User = mongoose.model('User', new Schema({ test: Array }));
|
|
163
162
|
* new User({ }).validateSync().errors.test.message; // Path `test` is required.
|
|
164
163
|
*
|
|
165
|
-
* @param {
|
|
166
|
-
* @param {
|
|
164
|
+
* @param {string} option The option you'd like to set the value for
|
|
165
|
+
* @param {any} value value for option
|
|
167
166
|
* @return {undefined}
|
|
168
167
|
* @function set
|
|
169
168
|
* @api public
|
|
170
169
|
*/
|
|
171
170
|
SchemaArray.set = SchemaType.set;
|
|
172
171
|
|
|
172
|
+
SchemaArray.setters = [];
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Attaches a getter for all Array instances
|
|
176
|
+
*
|
|
177
|
+
* @param {Function} getter
|
|
178
|
+
* @return {this}
|
|
179
|
+
* @function get
|
|
180
|
+
* @static
|
|
181
|
+
* @api public
|
|
182
|
+
*/
|
|
183
|
+
|
|
184
|
+
SchemaArray.get = SchemaType.get;
|
|
185
|
+
|
|
173
186
|
/*!
|
|
174
187
|
* Inherits from SchemaType.
|
|
175
188
|
*/
|
|
@@ -203,12 +216,18 @@ SchemaArray._checkRequired = SchemaType.prototype.checkRequired;
|
|
|
203
216
|
|
|
204
217
|
SchemaArray.checkRequired = SchemaType.checkRequired;
|
|
205
218
|
|
|
219
|
+
/*!
|
|
220
|
+
* Virtuals defined on this array itself.
|
|
221
|
+
*/
|
|
222
|
+
|
|
223
|
+
SchemaArray.prototype.virtuals = null;
|
|
224
|
+
|
|
206
225
|
/**
|
|
207
226
|
* Check if the given value satisfies the `required` validator.
|
|
208
227
|
*
|
|
209
|
-
* @param {
|
|
228
|
+
* @param {any} value
|
|
210
229
|
* @param {Document} doc
|
|
211
|
-
* @return {
|
|
230
|
+
* @return {boolean}
|
|
212
231
|
* @api public
|
|
213
232
|
*/
|
|
214
233
|
|
|
@@ -230,18 +249,16 @@ SchemaArray.prototype.checkRequired = function checkRequired(value, doc) {
|
|
|
230
249
|
* Adds an enum validator if this is an array of strings or numbers. Equivalent to
|
|
231
250
|
* `SchemaString.prototype.enum()` or `SchemaNumber.prototype.enum()`
|
|
232
251
|
*
|
|
233
|
-
* @param {...
|
|
252
|
+
* @param {...string|object} [args] enumeration values
|
|
234
253
|
* @return {SchemaArray} this
|
|
235
254
|
*/
|
|
236
255
|
|
|
237
256
|
SchemaArray.prototype.enum = function() {
|
|
238
257
|
let arr = this;
|
|
239
258
|
while (true) {
|
|
240
|
-
const instance = arr
|
|
241
|
-
arr.caster &&
|
|
242
|
-
arr.caster.instance;
|
|
259
|
+
const instance = arr?.embeddedSchemaType?.instance;
|
|
243
260
|
if (instance === 'Array') {
|
|
244
|
-
arr = arr.
|
|
261
|
+
arr = arr.embeddedSchemaType;
|
|
245
262
|
continue;
|
|
246
263
|
}
|
|
247
264
|
if (instance !== 'String' && instance !== 'Number') {
|
|
@@ -256,56 +273,51 @@ SchemaArray.prototype.enum = function() {
|
|
|
256
273
|
enumArray = utils.object.vals(enumArray);
|
|
257
274
|
}
|
|
258
275
|
|
|
259
|
-
arr.
|
|
276
|
+
arr.embeddedSchemaType.enum.apply(arr.embeddedSchemaType, enumArray);
|
|
260
277
|
return this;
|
|
261
278
|
};
|
|
262
279
|
|
|
263
280
|
/**
|
|
264
281
|
* Overrides the getters application for the population special-case
|
|
265
282
|
*
|
|
266
|
-
* @param {
|
|
267
|
-
* @param {
|
|
283
|
+
* @param {object} value
|
|
284
|
+
* @param {object} scope
|
|
268
285
|
* @api private
|
|
269
286
|
*/
|
|
270
287
|
|
|
271
288
|
SchemaArray.prototype.applyGetters = function(value, scope) {
|
|
272
|
-
if (scope
|
|
289
|
+
if (scope?.$__ != null && scope.$populated(this.path)) {
|
|
273
290
|
// means the object id was populated
|
|
274
291
|
return value;
|
|
275
292
|
}
|
|
276
293
|
|
|
277
294
|
const ret = SchemaType.prototype.applyGetters.call(this, value, scope);
|
|
278
|
-
if (Array.isArray(ret)) {
|
|
279
|
-
const rawValue = utils.isMongooseArray(ret) ? ret.__array : ret;
|
|
280
|
-
const len = rawValue.length;
|
|
281
|
-
for (let i = 0; i < len; ++i) {
|
|
282
|
-
rawValue[i] = this.caster.applyGetters(rawValue[i], scope);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
295
|
return ret;
|
|
286
296
|
};
|
|
287
297
|
|
|
288
298
|
SchemaArray.prototype._applySetters = function(value, scope, init, priorVal) {
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
let depth = 0;
|
|
299
|
+
// No need to wrap empty arrays
|
|
300
|
+
if (this.embeddedSchemaType.$isMongooseArray && value != null && value.length !== 0) {
|
|
301
|
+
// This array and its nested arrays, outermost first
|
|
302
|
+
const levels = [];
|
|
294
303
|
let arr = this;
|
|
295
304
|
while (arr != null &&
|
|
296
305
|
arr.$isMongooseArray &&
|
|
297
306
|
!arr.$isMongooseDocumentArray) {
|
|
298
|
-
|
|
299
|
-
arr = arr.
|
|
307
|
+
levels.push(arr);
|
|
308
|
+
arr = arr.embeddedSchemaType;
|
|
300
309
|
}
|
|
301
310
|
|
|
302
|
-
//
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
311
|
+
// Check nesting levels and wrap in array if necessary. A value nested
|
|
312
|
+
// `valueDepth.max` levels deep puts non-array values in every level from
|
|
313
|
+
// `valueDepth.max` inward, so each of those levels must allow wrapping.
|
|
314
|
+
const valueDepth = arrayDepth(value);
|
|
315
|
+
if (valueDepth.min === valueDepth.max &&
|
|
316
|
+
valueDepth.max < levels.length &&
|
|
317
|
+
valueDepth.containsNonArrayItem &&
|
|
318
|
+
levels.slice(valueDepth.max).every(level => level.options.castNonArrays ?? SchemaArray.options.castNonArrays)) {
|
|
319
|
+
for (let i = valueDepth.max; i < levels.length; ++i) {
|
|
320
|
+
value = [value];
|
|
309
321
|
}
|
|
310
322
|
}
|
|
311
323
|
}
|
|
@@ -316,9 +328,9 @@ SchemaArray.prototype._applySetters = function(value, scope, init, priorVal) {
|
|
|
316
328
|
/**
|
|
317
329
|
* Casts values for set().
|
|
318
330
|
*
|
|
319
|
-
* @param {
|
|
331
|
+
* @param {object} value
|
|
320
332
|
* @param {Document} doc document that triggers the casting
|
|
321
|
-
* @param {
|
|
333
|
+
* @param {boolean} init whether this is an initialization cast
|
|
322
334
|
* @api private
|
|
323
335
|
*/
|
|
324
336
|
|
|
@@ -359,16 +371,20 @@ SchemaArray.prototype.cast = function(value, doc, init, prev, options) {
|
|
|
359
371
|
options = options || emptyOpts;
|
|
360
372
|
|
|
361
373
|
let rawValue = utils.isMongooseArray(value) ? value.__array : value;
|
|
362
|
-
|
|
374
|
+
let path = options.path || this.path;
|
|
375
|
+
if (options.arrayPathIndex != null) {
|
|
376
|
+
path += '.' + options.arrayPathIndex;
|
|
377
|
+
}
|
|
378
|
+
value = MongooseArray(rawValue, path, doc, this);
|
|
363
379
|
rawValue = value.__array;
|
|
364
380
|
|
|
365
|
-
if (init && doc
|
|
381
|
+
if (init && doc?.$__ != null && doc.$populated(this.path)) {
|
|
366
382
|
return value;
|
|
367
383
|
}
|
|
368
384
|
|
|
369
|
-
const caster = this.
|
|
385
|
+
const caster = this.embeddedSchemaType;
|
|
370
386
|
const isMongooseArray = caster.$isMongooseArray;
|
|
371
|
-
if (caster && this.
|
|
387
|
+
if (caster && this.embeddedSchemaType.constructor !== Mixed) {
|
|
372
388
|
try {
|
|
373
389
|
const len = rawValue.length;
|
|
374
390
|
for (i = 0; i < len; i++) {
|
|
@@ -383,7 +399,21 @@ SchemaArray.prototype.cast = function(value, doc, init, prev, options) {
|
|
|
383
399
|
opts.arrayPathIndex = i;
|
|
384
400
|
}
|
|
385
401
|
}
|
|
386
|
-
|
|
402
|
+
if (options.hydratedPopulatedDocs) {
|
|
403
|
+
opts.hydratedPopulatedDocs = options.hydratedPopulatedDocs;
|
|
404
|
+
}
|
|
405
|
+
if (options.virtuals) {
|
|
406
|
+
opts.virtuals = options.virtuals;
|
|
407
|
+
}
|
|
408
|
+
if (rawValue[i] === undefined && caster.defaultValue !== undefined) {
|
|
409
|
+
// No need to add _skipMarkModified to opts here (fallback for `getDefault()` opts parameter)
|
|
410
|
+
// because array setters don't rely on Document.prototype.set() - subdocuments do rely on
|
|
411
|
+
// Document.prototype.set() when applying setters, which is why _skipMarkModified is necessary
|
|
412
|
+
// for subdocuments. But here we know that `caster` is an array not a subdocument.
|
|
413
|
+
rawValue[i] = caster.getDefault(doc, init, null, opts);
|
|
414
|
+
} else {
|
|
415
|
+
rawValue[i] = caster.applySetters(rawValue[i], doc, init, void 0, opts);
|
|
416
|
+
}
|
|
387
417
|
}
|
|
388
418
|
} catch (e) {
|
|
389
419
|
// rethrow
|
|
@@ -394,11 +424,11 @@ SchemaArray.prototype.cast = function(value, doc, init, prev, options) {
|
|
|
394
424
|
return value;
|
|
395
425
|
}
|
|
396
426
|
|
|
397
|
-
const castNonArraysOption = this.options.castNonArrays
|
|
427
|
+
const castNonArraysOption = this.options.castNonArrays ?? SchemaArray.options.castNonArrays;
|
|
398
428
|
if (init || castNonArraysOption) {
|
|
399
429
|
// gh-2442: if we're loading this from the db and its not an array, mark
|
|
400
430
|
// the whole array as modified.
|
|
401
|
-
if (
|
|
431
|
+
if (doc && init) {
|
|
402
432
|
doc.markModified(this.path);
|
|
403
433
|
}
|
|
404
434
|
return this.cast([value], doc, init);
|
|
@@ -420,19 +450,18 @@ SchemaArray.prototype._castForPopulate = function _castForPopulate(value, doc) {
|
|
|
420
450
|
const rawValue = value.__array ? value.__array : value;
|
|
421
451
|
const len = rawValue.length;
|
|
422
452
|
|
|
423
|
-
|
|
424
|
-
if (caster && this.casterConstructor !== Mixed) {
|
|
453
|
+
if (this.embeddedSchemaType && this.embeddedSchemaType.constructor !== Mixed) {
|
|
425
454
|
try {
|
|
426
455
|
for (i = 0; i < len; i++) {
|
|
427
456
|
const opts = {};
|
|
428
457
|
// Perf: creating `arrayPath` is expensive for large arrays.
|
|
429
458
|
// We only need `arrayPath` if this is a nested array, so
|
|
430
459
|
// skip if possible.
|
|
431
|
-
if (
|
|
460
|
+
if (this.embeddedSchemaType.$isMongooseArray && this.embeddedSchemaType._arrayParentPath != null) {
|
|
432
461
|
opts.arrayPathIndex = i;
|
|
433
462
|
}
|
|
434
463
|
|
|
435
|
-
rawValue[i] =
|
|
464
|
+
rawValue[i] = this.embeddedSchemaType.cast(rawValue[i], doc, false, void 0, opts);
|
|
436
465
|
}
|
|
437
466
|
} catch (e) {
|
|
438
467
|
// rethrow
|
|
@@ -455,11 +484,10 @@ SchemaArray.prototype.$toObject = SchemaArray.prototype.toObject;
|
|
|
455
484
|
SchemaArray.prototype.discriminator = function(...args) {
|
|
456
485
|
let arr = this;
|
|
457
486
|
while (arr.$isMongooseArray && !arr.$isMongooseDocumentArray) {
|
|
458
|
-
arr = arr.
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
}
|
|
487
|
+
arr = arr.embeddedSchemaType;
|
|
488
|
+
}
|
|
489
|
+
if (!arr.$isMongooseDocumentArray) {
|
|
490
|
+
throw new MongooseError('You can only add an embedded discriminator on a document array, ' + this.path + ' is a plain array');
|
|
463
491
|
}
|
|
464
492
|
return arr.discriminator(...args);
|
|
465
493
|
};
|
|
@@ -470,7 +498,7 @@ SchemaArray.prototype.discriminator = function(...args) {
|
|
|
470
498
|
|
|
471
499
|
SchemaArray.prototype.clone = function() {
|
|
472
500
|
const options = Object.assign({}, this.options);
|
|
473
|
-
const schematype = new this.constructor(this.path, this.
|
|
501
|
+
const schematype = new this.constructor(this.path, this.embeddedSchemaType, options, this.schemaOptions, this.parentSchema);
|
|
474
502
|
schematype.validators = this.validators.slice();
|
|
475
503
|
if (this.requiredValidator !== undefined) {
|
|
476
504
|
schematype.requiredValidator = this.requiredValidator;
|
|
@@ -478,84 +506,91 @@ SchemaArray.prototype.clone = function() {
|
|
|
478
506
|
return schematype;
|
|
479
507
|
};
|
|
480
508
|
|
|
509
|
+
SchemaArray.prototype._castForQuery = function(val, context) {
|
|
510
|
+
let embeddedSchemaType = this.embeddedSchemaType;
|
|
511
|
+
const discriminatorKey = embeddedSchemaType?.schema?.options?.discriminatorKey;
|
|
512
|
+
const discriminators = embeddedSchemaType?.discriminators;
|
|
513
|
+
|
|
514
|
+
if (val && discriminators && typeof discriminatorKey === 'string') {
|
|
515
|
+
const discriminator = getDiscriminatorByKey(discriminators, val[discriminatorKey]);
|
|
516
|
+
if (discriminator) {
|
|
517
|
+
embeddedSchemaType = discriminator;
|
|
518
|
+
} else {
|
|
519
|
+
const constructorByValue = getDiscriminatorByValue(discriminators, val[discriminatorKey]);
|
|
520
|
+
if (constructorByValue) {
|
|
521
|
+
embeddedSchemaType = constructorByValue;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
if (Array.isArray(val)) {
|
|
527
|
+
for (let i = this.setters.length - 1; i >= 0; i--) {
|
|
528
|
+
val = this.setters[i].call(this, val, this);
|
|
529
|
+
}
|
|
530
|
+
val = val.map(function(v) {
|
|
531
|
+
if (utils.isObject(v) && v.$elemMatch) {
|
|
532
|
+
return v;
|
|
533
|
+
}
|
|
534
|
+
return embeddedSchemaType.castForQuery(null, v, context);
|
|
535
|
+
});
|
|
536
|
+
} else {
|
|
537
|
+
val = embeddedSchemaType.castForQuery(null, val, context);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
return val;
|
|
541
|
+
};
|
|
542
|
+
|
|
481
543
|
/**
|
|
482
544
|
* Casts values for queries.
|
|
483
545
|
*
|
|
484
|
-
* @param {
|
|
546
|
+
* @param {string} $conditional
|
|
485
547
|
* @param {any} [value]
|
|
486
548
|
* @api private
|
|
487
549
|
*/
|
|
488
550
|
|
|
489
|
-
SchemaArray.prototype.castForQuery = function($conditional,
|
|
551
|
+
SchemaArray.prototype.castForQuery = function($conditional, val, context) {
|
|
490
552
|
let handler;
|
|
491
|
-
let val;
|
|
492
553
|
|
|
493
|
-
if (
|
|
494
|
-
handler = this.$conditionalHandlers
|
|
554
|
+
if ($conditional != null) {
|
|
555
|
+
handler = getConditionalHandler(this.$conditionalHandlers, $conditional);
|
|
495
556
|
|
|
496
557
|
if (!handler) {
|
|
497
558
|
throw new Error('Can\'t use ' + $conditional + ' with Array.');
|
|
498
559
|
}
|
|
499
560
|
|
|
500
|
-
|
|
561
|
+
return handler.call(this, val, context);
|
|
501
562
|
} else {
|
|
502
|
-
val
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
if (val &&
|
|
506
|
-
Constructor.discriminators &&
|
|
507
|
-
Constructor.schema &&
|
|
508
|
-
Constructor.schema.options &&
|
|
509
|
-
Constructor.schema.options.discriminatorKey) {
|
|
510
|
-
const discriminator = typeof val[Constructor.schema.options.discriminatorKey] === 'string' ?
|
|
511
|
-
getDiscriminatorByKey(Constructor.discriminators, val[Constructor.schema.options.discriminatorKey]) :
|
|
512
|
-
null;
|
|
513
|
-
if (discriminator) {
|
|
514
|
-
Constructor = discriminator;
|
|
515
|
-
} else {
|
|
516
|
-
const constructorByValue = getDiscriminatorByValue(Constructor.discriminators, val[Constructor.schema.options.discriminatorKey]);
|
|
517
|
-
if (constructorByValue) {
|
|
518
|
-
Constructor = constructorByValue;
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
}
|
|
563
|
+
return this._castForQuery(val, context);
|
|
564
|
+
}
|
|
565
|
+
};
|
|
522
566
|
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
val = val.map(function(v) {
|
|
535
|
-
if (utils.isObject(v) && v.$elemMatch) {
|
|
536
|
-
return v;
|
|
537
|
-
}
|
|
538
|
-
if (method) {
|
|
539
|
-
v = method.call(caster, v);
|
|
540
|
-
return v;
|
|
541
|
-
}
|
|
542
|
-
if (v != null) {
|
|
543
|
-
v = new Constructor(v);
|
|
544
|
-
return v;
|
|
545
|
-
}
|
|
546
|
-
return v;
|
|
547
|
-
});
|
|
548
|
-
} else if (method) {
|
|
549
|
-
val = method.call(caster, val);
|
|
550
|
-
} else if (val != null) {
|
|
551
|
-
val = new Constructor(val);
|
|
552
|
-
}
|
|
567
|
+
/**
|
|
568
|
+
* Add a virtual to this array. Specifically to this array, not the individual elements.
|
|
569
|
+
*
|
|
570
|
+
* @param {string} name
|
|
571
|
+
* @param {object} [options]
|
|
572
|
+
* @api private
|
|
573
|
+
*/
|
|
574
|
+
|
|
575
|
+
SchemaArray.prototype.virtual = function virtual(name, options) {
|
|
576
|
+
if (name instanceof VirtualType || getConstructorName(name) === 'VirtualType') {
|
|
577
|
+
return this.virtual(name.path, name.options);
|
|
553
578
|
}
|
|
579
|
+
options = new VirtualOptions(options);
|
|
554
580
|
|
|
555
|
-
|
|
581
|
+
if (utils.hasUserDefinedProperty(options, ['ref', 'refPath'])) {
|
|
582
|
+
throw new MongooseError('Cannot set populate virtual as a property of an array');
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
const virtual = new VirtualType(options, name);
|
|
586
|
+
if (this.virtuals === null) {
|
|
587
|
+
this.virtuals = {};
|
|
588
|
+
}
|
|
589
|
+
this.virtuals[name] = virtual;
|
|
590
|
+
return virtual;
|
|
556
591
|
};
|
|
557
592
|
|
|
558
|
-
function cast$all(val) {
|
|
593
|
+
function cast$all(val, context) {
|
|
559
594
|
if (!Array.isArray(val)) {
|
|
560
595
|
val = [val];
|
|
561
596
|
}
|
|
@@ -565,49 +600,54 @@ function cast$all(val) {
|
|
|
565
600
|
return v;
|
|
566
601
|
}
|
|
567
602
|
if (v.$elemMatch != null) {
|
|
568
|
-
return { $elemMatch: cast(this.
|
|
603
|
+
return { $elemMatch: cast(this.embeddedSchemaType.schema, v.$elemMatch, null, this?.$$context) };
|
|
569
604
|
}
|
|
570
605
|
|
|
571
606
|
const o = {};
|
|
572
607
|
o[this.path] = v;
|
|
573
|
-
return cast(this.
|
|
608
|
+
return cast(this.embeddedSchemaType.schema, o, null, this?.$$context)[this.path];
|
|
574
609
|
}, this);
|
|
575
610
|
|
|
576
|
-
return this.castForQuery(val);
|
|
611
|
+
return this.castForQuery(null, val, context);
|
|
577
612
|
}
|
|
578
613
|
|
|
579
|
-
function cast$elemMatch(val) {
|
|
614
|
+
function cast$elemMatch(val, context) {
|
|
580
615
|
const keys = Object.keys(val);
|
|
581
616
|
const numKeys = keys.length;
|
|
582
617
|
for (let i = 0; i < numKeys; ++i) {
|
|
583
618
|
const key = keys[i];
|
|
584
619
|
const value = val[key];
|
|
620
|
+
if (key === '$not') {
|
|
621
|
+
// `$not` wraps other operators, like `{ $not: { $gt: 5 } }` or
|
|
622
|
+
// `{ $not: { $elemMatch: ... } }`, so cast each wrapped operator.
|
|
623
|
+
// A RegExp is left as-is.
|
|
624
|
+
if (utils.isPOJO(value)) {
|
|
625
|
+
for (const notKey of Object.keys(value)) {
|
|
626
|
+
if (isOperator(notKey) && value[notKey] != null) {
|
|
627
|
+
value[notKey] = this.castForQuery(notKey, value[notKey], context);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
continue;
|
|
632
|
+
}
|
|
585
633
|
if (isOperator(key) && value != null) {
|
|
586
|
-
val[key] = this.castForQuery(key, value);
|
|
634
|
+
val[key] = this.castForQuery(key, value, context);
|
|
587
635
|
}
|
|
588
636
|
}
|
|
589
637
|
|
|
590
|
-
|
|
591
|
-
// If so, use the discriminator schema. See gh-7449
|
|
592
|
-
const discriminatorKey = this &&
|
|
593
|
-
this.casterConstructor &&
|
|
594
|
-
this.casterConstructor.schema &&
|
|
595
|
-
this.casterConstructor.schema.options &&
|
|
596
|
-
this.casterConstructor.schema.options.discriminatorKey;
|
|
597
|
-
const discriminators = this &&
|
|
598
|
-
this.casterConstructor &&
|
|
599
|
-
this.casterConstructor.schema &&
|
|
600
|
-
this.casterConstructor.schema.discriminators || {};
|
|
601
|
-
const discriminator = discriminatorKey != null && val[discriminatorKey] != null ?
|
|
602
|
-
getDiscriminatorByKey(discriminators, val[discriminatorKey]) :
|
|
603
|
-
null;
|
|
604
|
-
if (discriminator != null) {
|
|
605
|
-
return cast(discriminator, val, null, this && this.$$context);
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
return cast(this.casterConstructor.schema, val, null, this && this.$$context);
|
|
638
|
+
return val;
|
|
609
639
|
}
|
|
610
640
|
|
|
641
|
+
/**
|
|
642
|
+
* Contains the handlers for different query operators for this schema type.
|
|
643
|
+
* For example, `$conditionalHandlers.$all` is the function Mongoose calls to cast `$all` filter operators.
|
|
644
|
+
*
|
|
645
|
+
* @property $conditionalHandlers
|
|
646
|
+
* @memberOf SchemaArray
|
|
647
|
+
* @instance
|
|
648
|
+
* @api public
|
|
649
|
+
*/
|
|
650
|
+
|
|
611
651
|
const handle = SchemaArray.prototype.$conditionalHandlers = {};
|
|
612
652
|
|
|
613
653
|
handle.$all = cast$all;
|
|
@@ -619,14 +659,14 @@ handle.$and = createLogicalQueryOperatorHandler('$and');
|
|
|
619
659
|
handle.$nor = createLogicalQueryOperatorHandler('$nor');
|
|
620
660
|
|
|
621
661
|
function createLogicalQueryOperatorHandler(op) {
|
|
622
|
-
return function logicalQueryOperatorHandler(val) {
|
|
662
|
+
return function logicalQueryOperatorHandler(val, context) {
|
|
623
663
|
if (!Array.isArray(val)) {
|
|
624
664
|
throw new TypeError('conditional ' + op + ' requires an array');
|
|
625
665
|
}
|
|
626
666
|
|
|
627
667
|
const ret = [];
|
|
628
668
|
for (const obj of val) {
|
|
629
|
-
ret.push(cast(this.
|
|
669
|
+
ret.push(cast(this.embeddedSchemaType.schema ?? context.schema, obj, null, this?.$$context));
|
|
630
670
|
}
|
|
631
671
|
|
|
632
672
|
return ret;
|
|
@@ -651,15 +691,35 @@ handle.$gt =
|
|
|
651
691
|
handle.$gte =
|
|
652
692
|
handle.$lt =
|
|
653
693
|
handle.$lte =
|
|
654
|
-
handle.$ne =
|
|
655
694
|
handle.$not =
|
|
656
|
-
handle.$regex =
|
|
695
|
+
handle.$regex =
|
|
696
|
+
handle.$ne = SchemaArray.prototype._castForQuery;
|
|
657
697
|
|
|
658
698
|
// `$in` is special because you can also include an empty array in the query
|
|
659
699
|
// like `$in: [1, []]`, see gh-5913
|
|
660
700
|
handle.$nin = SchemaType.prototype.$conditionalHandlers.$nin;
|
|
661
701
|
handle.$in = SchemaType.prototype.$conditionalHandlers.$in;
|
|
662
702
|
|
|
703
|
+
/**
|
|
704
|
+
* Returns this schema type's representation in a JSON schema.
|
|
705
|
+
*
|
|
706
|
+
* @param {object} [options]
|
|
707
|
+
* @param {boolean} [options.useBsonType=false] If true, return a representation with `bsonType` for use with MongoDB's `$jsonSchema`.
|
|
708
|
+
* @returns {object} JSON schema properties
|
|
709
|
+
*/
|
|
710
|
+
|
|
711
|
+
SchemaArray.prototype.toJSONSchema = function toJSONSchema(options) {
|
|
712
|
+
const embeddedSchemaType = this.getEmbeddedSchemaType();
|
|
713
|
+
return {
|
|
714
|
+
...this._createJSONSchemaTypeDefinition('array', 'array', options),
|
|
715
|
+
items: embeddedSchemaType.toJSONSchema(options)
|
|
716
|
+
};
|
|
717
|
+
};
|
|
718
|
+
|
|
719
|
+
SchemaArray.prototype.autoEncryptionType = function autoEncryptionType() {
|
|
720
|
+
return 'array';
|
|
721
|
+
};
|
|
722
|
+
|
|
663
723
|
/*!
|
|
664
724
|
* Module exports.
|
|
665
725
|
*/
|