@datanimbus/mongoose 9.13.1 → 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
|
@@ -2,28 +2,52 @@
|
|
|
2
2
|
|
|
3
3
|
const CastError = require('../../error/cast');
|
|
4
4
|
const MongooseError = require('../../error/mongooseError');
|
|
5
|
+
const SchemaString = require('../../schema/string');
|
|
5
6
|
const StrictModeError = require('../../error/strict');
|
|
6
7
|
const ValidationError = require('../../error/validation');
|
|
7
8
|
const castNumber = require('../../cast/number');
|
|
8
9
|
const cast = require('../../cast');
|
|
9
10
|
const getConstructorName = require('../getConstructorName');
|
|
11
|
+
const getDiscriminatorByKey = require('../discriminator/getDiscriminatorByKey');
|
|
12
|
+
const getDiscriminatorByValue = require('../discriminator/getDiscriminatorByValue');
|
|
10
13
|
const getEmbeddedDiscriminatorPath = require('./getEmbeddedDiscriminatorPath');
|
|
14
|
+
const getOwnProperty = require('../getOwnProperty');
|
|
11
15
|
const handleImmutable = require('./handleImmutable');
|
|
16
|
+
const isMongooseDocument = require('../isMongooseDocument');
|
|
17
|
+
const isOperator = require('./isOperator');
|
|
12
18
|
const moveImmutableProperties = require('../update/moveImmutableProperties');
|
|
13
19
|
const schemaMixedSymbol = require('../../schema/symbols').schemaMixedSymbol;
|
|
14
20
|
const setDottedPath = require('../path/setDottedPath');
|
|
15
21
|
const utils = require('../../utils');
|
|
22
|
+
const { internalToObjectOptions } = require('../../options');
|
|
23
|
+
|
|
24
|
+
const mongodbUpdateOperators = new Set([
|
|
25
|
+
'$currentDate',
|
|
26
|
+
'$inc',
|
|
27
|
+
'$min',
|
|
28
|
+
'$max',
|
|
29
|
+
'$mul',
|
|
30
|
+
'$rename',
|
|
31
|
+
'$set',
|
|
32
|
+
'$setOnInsert',
|
|
33
|
+
'$unset',
|
|
34
|
+
'$addToSet',
|
|
35
|
+
'$pop',
|
|
36
|
+
'$pull',
|
|
37
|
+
'$push',
|
|
38
|
+
'$pullAll',
|
|
39
|
+
'$bit'
|
|
40
|
+
]);
|
|
16
41
|
|
|
17
42
|
/**
|
|
18
43
|
* Casts an update op based on the given schema
|
|
19
44
|
*
|
|
20
45
|
* @param {Schema} schema
|
|
21
|
-
* @param {
|
|
22
|
-
* @param {
|
|
23
|
-
* @param {
|
|
24
|
-
* @param {Boolean|String} [options.strict] defaults to true
|
|
46
|
+
* @param {object} obj
|
|
47
|
+
* @param {object} [options]
|
|
48
|
+
* @param {boolean|'throw'} [options.strict] defaults to true
|
|
25
49
|
* @param {Query} context passed to setters
|
|
26
|
-
* @return {
|
|
50
|
+
* @return {boolean} true iff the update is non-empty
|
|
27
51
|
* @api private
|
|
28
52
|
*/
|
|
29
53
|
module.exports = function castUpdate(schema, obj, options, context, filter) {
|
|
@@ -43,22 +67,55 @@ module.exports = function castUpdate(schema, obj, options, context, filter) {
|
|
|
43
67
|
return obj;
|
|
44
68
|
}
|
|
45
69
|
|
|
46
|
-
if (
|
|
70
|
+
if (schema != null &&
|
|
71
|
+
filter != null &&
|
|
72
|
+
utils.hasUserDefinedProperty(filter, schema.options.discriminatorKey) &&
|
|
73
|
+
typeof filter[schema.options.discriminatorKey] !== 'object' &&
|
|
74
|
+
schema.discriminators != null) {
|
|
75
|
+
const discriminatorValue = filter[schema.options.discriminatorKey];
|
|
76
|
+
const byValue = getDiscriminatorByValue(context.model.discriminators, discriminatorValue);
|
|
77
|
+
schema = getDiscriminatorByKey(schema.discriminators, discriminatorValue) ||
|
|
78
|
+
byValue?.schema ||
|
|
79
|
+
schema;
|
|
80
|
+
} else if (schema != null &&
|
|
81
|
+
options.overwriteDiscriminatorKey &&
|
|
82
|
+
utils.hasUserDefinedProperty(obj, schema.options.discriminatorKey) &&
|
|
83
|
+
schema.discriminators != null) {
|
|
84
|
+
const discriminatorValue = obj[schema.options.discriminatorKey];
|
|
85
|
+
const byValue = getDiscriminatorByValue(context.model.discriminators, discriminatorValue);
|
|
86
|
+
schema = getDiscriminatorByKey(schema.discriminators, discriminatorValue) ||
|
|
87
|
+
byValue?.schema ||
|
|
88
|
+
schema;
|
|
89
|
+
} else if (schema != null &&
|
|
90
|
+
options.overwriteDiscriminatorKey &&
|
|
91
|
+
obj.$set != null &&
|
|
92
|
+
utils.hasUserDefinedProperty(obj.$set, schema.options.discriminatorKey) &&
|
|
93
|
+
schema.discriminators != null) {
|
|
94
|
+
const discriminatorValue = obj.$set[schema.options.discriminatorKey];
|
|
95
|
+
const byValue = getDiscriminatorByValue(context.model.discriminators, discriminatorValue);
|
|
96
|
+
schema = getDiscriminatorByKey(schema.discriminators, discriminatorValue) ||
|
|
97
|
+
byValue?.schema ||
|
|
98
|
+
schema;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (options.upsert && !options.overwriteImmutable) {
|
|
47
102
|
moveImmutableProperties(schema, obj, context);
|
|
48
103
|
}
|
|
49
104
|
|
|
105
|
+
if (isMongooseDocument(obj) && typeof obj.toObject === 'function') {
|
|
106
|
+
obj = obj.toObject(internalToObjectOptions);
|
|
107
|
+
}
|
|
108
|
+
|
|
50
109
|
const ops = Object.keys(obj);
|
|
51
110
|
let i = ops.length;
|
|
52
111
|
const ret = {};
|
|
53
112
|
let val;
|
|
54
113
|
let hasDollarKey = false;
|
|
55
|
-
const overwrite = options.overwrite;
|
|
56
114
|
|
|
57
115
|
filter = filter || {};
|
|
58
116
|
while (i--) {
|
|
59
117
|
const op = ops[i];
|
|
60
|
-
|
|
61
|
-
if (op[0] !== '$' && !overwrite) {
|
|
118
|
+
if (!mongodbUpdateOperators.has(op)) {
|
|
62
119
|
// fix up $set sugar
|
|
63
120
|
if (!ret.$set) {
|
|
64
121
|
if (obj.$set) {
|
|
@@ -84,14 +141,15 @@ module.exports = function castUpdate(schema, obj, options, context, filter) {
|
|
|
84
141
|
const op = ops[i];
|
|
85
142
|
val = ret[op];
|
|
86
143
|
hasDollarKey = hasDollarKey || op.startsWith('$');
|
|
87
|
-
|
|
144
|
+
if (isMongooseDocument(val)) {
|
|
145
|
+
val = val.toObject(internalToObjectOptions);
|
|
146
|
+
ret[op] = val;
|
|
147
|
+
}
|
|
88
148
|
if (val &&
|
|
89
149
|
typeof val === 'object' &&
|
|
90
150
|
!Buffer.isBuffer(val) &&
|
|
91
|
-
(
|
|
151
|
+
mongodbUpdateOperators.has(op)) {
|
|
92
152
|
walkUpdatePath(schema, val, op, options, context, filter);
|
|
93
|
-
} else if (overwrite && ret && typeof ret === 'object') {
|
|
94
|
-
walkUpdatePath(schema, ret, '$set', options, context, filter);
|
|
95
153
|
} else {
|
|
96
154
|
const msg = 'Invalid atomic update value for ' + op + '. '
|
|
97
155
|
+ 'Expected an object, received ' + typeof val;
|
|
@@ -103,12 +161,13 @@ module.exports = function castUpdate(schema, obj, options, context, filter) {
|
|
|
103
161
|
}
|
|
104
162
|
}
|
|
105
163
|
|
|
106
|
-
if (
|
|
164
|
+
if (utils.hasOwnKeys(ret) === false &&
|
|
107
165
|
options.upsert &&
|
|
108
|
-
|
|
166
|
+
utils.hasOwnKeys(filter)) {
|
|
109
167
|
// Trick the driver into allowing empty upserts to work around
|
|
110
168
|
// https://github.com/mongodb/node-mongodb-native/pull/2490
|
|
111
|
-
|
|
169
|
+
// Shallow clone to avoid passing defaults in re: gh-13962
|
|
170
|
+
return { $setOnInsert: { ...filter } };
|
|
112
171
|
}
|
|
113
172
|
return ret;
|
|
114
173
|
};
|
|
@@ -151,20 +210,20 @@ function castPipelineOperator(op, val) {
|
|
|
151
210
|
* according to its schema.
|
|
152
211
|
*
|
|
153
212
|
* @param {Schema} schema
|
|
154
|
-
* @param {
|
|
155
|
-
* @param {
|
|
156
|
-
* @param {
|
|
157
|
-
* @param {
|
|
213
|
+
* @param {object} obj part of a query
|
|
214
|
+
* @param {string} op the atomic operator ($pull, $set, etc)
|
|
215
|
+
* @param {object} [options]
|
|
216
|
+
* @param {boolean|'throw'} [options.strict]
|
|
158
217
|
* @param {Query} context
|
|
159
|
-
* @param {
|
|
160
|
-
* @param {
|
|
218
|
+
* @param {object} filter
|
|
219
|
+
* @param {string} pref path prefix (internal only)
|
|
161
220
|
* @return {Bool} true if this path has keys to update
|
|
162
221
|
* @api private
|
|
163
222
|
*/
|
|
164
223
|
|
|
165
|
-
function walkUpdatePath(schema, obj, op, options, context, filter,
|
|
224
|
+
function walkUpdatePath(schema, obj, op, options, context, filter, prefix) {
|
|
166
225
|
const strict = options.strict;
|
|
167
|
-
|
|
226
|
+
prefix = prefix ? prefix + '.' : '';
|
|
168
227
|
const keys = Object.keys(obj);
|
|
169
228
|
let i = keys.length;
|
|
170
229
|
let hasKeys = false;
|
|
@@ -174,18 +233,48 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
|
|
|
174
233
|
|
|
175
234
|
let aggregatedError = null;
|
|
176
235
|
|
|
177
|
-
const strictMode = strict
|
|
236
|
+
const strictMode = strict ?? schema.options.strict;
|
|
178
237
|
|
|
179
238
|
while (i--) {
|
|
180
239
|
key = keys[i];
|
|
181
240
|
val = obj[key];
|
|
182
241
|
|
|
242
|
+
const fullPath = prefix + key;
|
|
243
|
+
const isTopLevelOperator = !prefix && isOperator(key);
|
|
244
|
+
let fullPathSchema = isTopLevelOperator ? schema._getSchema(fullPath) : null;
|
|
245
|
+
const isTopLevelNestedDollarPath = isTopLevelOperator && Object.hasOwn(schema.nested, key);
|
|
246
|
+
if (isTopLevelOperator &&
|
|
247
|
+
fullPathSchema == null &&
|
|
248
|
+
!isTopLevelNestedDollarPath) {
|
|
249
|
+
throw new MongooseError('Invalid update: Unexpected modifier "' + key + '" as a key in operator "' + op + '". '
|
|
250
|
+
+ 'Did you mean something like { ' + op + ': { fieldName: { ' + key + ': [...] } } }? '
|
|
251
|
+
+ 'Modifiers must appear under a valid field path.');
|
|
252
|
+
}
|
|
253
|
+
|
|
183
254
|
// `$pull` is special because we need to cast the RHS as a query, not as
|
|
184
255
|
// an update.
|
|
185
256
|
if (op === '$pull') {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
257
|
+
if (!isTopLevelOperator) {
|
|
258
|
+
fullPathSchema = schema._getSchema(fullPath);
|
|
259
|
+
}
|
|
260
|
+
schematype = fullPathSchema;
|
|
261
|
+
if (schematype == null) {
|
|
262
|
+
const _res = getEmbeddedDiscriminatorPath(schema, obj, filter, prefix + key, options);
|
|
263
|
+
if (_res.schematype != null) {
|
|
264
|
+
schematype = _res.schematype;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
if (schematype?.schema != null) {
|
|
268
|
+
try {
|
|
269
|
+
obj[key] = cast(schematype.schema, obj[key], options, context);
|
|
270
|
+
} catch (error) {
|
|
271
|
+
if (error instanceof CastError) {
|
|
272
|
+
aggregatedError = _appendError(error, context, key, aggregatedError);
|
|
273
|
+
continue;
|
|
274
|
+
} else {
|
|
275
|
+
throw error;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
189
278
|
hasKeys = true;
|
|
190
279
|
continue;
|
|
191
280
|
}
|
|
@@ -208,10 +297,13 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
|
|
|
208
297
|
}
|
|
209
298
|
}
|
|
210
299
|
|
|
300
|
+
if (!isTopLevelOperator && op !== '$pull') {
|
|
301
|
+
fullPathSchema = schema._getSchema(fullPath);
|
|
302
|
+
}
|
|
303
|
+
schematype = fullPathSchema;
|
|
304
|
+
|
|
211
305
|
if (getConstructorName(val) === 'Object') {
|
|
212
306
|
// watch for embedded doc schemas
|
|
213
|
-
schematype = schema._getSchema(prefix + key);
|
|
214
|
-
|
|
215
307
|
if (schematype == null) {
|
|
216
308
|
const _res = getEmbeddedDiscriminatorPath(schema, obj, filter, prefix + key, options);
|
|
217
309
|
if (_res.schematype != null) {
|
|
@@ -220,12 +312,11 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
|
|
|
220
312
|
}
|
|
221
313
|
|
|
222
314
|
if (op !== '$setOnInsert' &&
|
|
223
|
-
|
|
224
|
-
handleImmutable(schematype, strict, obj, key, prefix + key, context)) {
|
|
315
|
+
handleImmutable(schematype, strict, obj, key, prefix + key, options, context)) {
|
|
225
316
|
continue;
|
|
226
317
|
}
|
|
227
318
|
|
|
228
|
-
if (schematype && schematype.
|
|
319
|
+
if (schematype && (schematype.embeddedSchemaType || schematype.Constructor) && op in castOps) {
|
|
229
320
|
// embedded doc schema
|
|
230
321
|
if ('$each' in val) {
|
|
231
322
|
hasKeys = true;
|
|
@@ -249,10 +340,10 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
|
|
|
249
340
|
obj[key].$position = castNumber(val.$position);
|
|
250
341
|
}
|
|
251
342
|
} else {
|
|
252
|
-
if (schematype
|
|
343
|
+
if (schematype?.$isSingleNested) {
|
|
253
344
|
const _strict = strict == null ? schematype.schema.options.strict : strict;
|
|
254
345
|
try {
|
|
255
|
-
obj[key] = schematype.castForQuery(val, context, { strict: _strict });
|
|
346
|
+
obj[key] = schematype.castForQuery(null, val, context, { strict: _strict });
|
|
256
347
|
} catch (error) {
|
|
257
348
|
aggregatedError = _appendError(error, context, key, aggregatedError);
|
|
258
349
|
}
|
|
@@ -284,12 +375,26 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
|
|
|
284
375
|
continue;
|
|
285
376
|
}
|
|
286
377
|
|
|
378
|
+
hasKeys = true;
|
|
379
|
+
} else if (op === '$rename') {
|
|
380
|
+
const schematype = new SchemaString(`${prefix}${key}.$rename`);
|
|
381
|
+
try {
|
|
382
|
+
obj[key] = castUpdateVal(schematype, val, op, key, context, prefix + key);
|
|
383
|
+
} catch (error) {
|
|
384
|
+
aggregatedError = _appendError(error, context, key, aggregatedError);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
if (obj[key] === void 0) {
|
|
388
|
+
delete obj[key];
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
|
|
287
392
|
hasKeys = true;
|
|
288
393
|
} else {
|
|
289
394
|
const pathToCheck = (prefix + key);
|
|
290
395
|
const v = schema._getPathType(pathToCheck);
|
|
291
396
|
let _strict = strict;
|
|
292
|
-
if (v
|
|
397
|
+
if (v?.schema && _strict == null) {
|
|
293
398
|
_strict = v.schema.options.strict;
|
|
294
399
|
}
|
|
295
400
|
|
|
@@ -306,17 +411,19 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
|
|
|
306
411
|
// we should be able to set a schema-less field
|
|
307
412
|
// to an empty object literal
|
|
308
413
|
hasKeys |= walkUpdatePath(schema, val, op, options, context, filter, prefix + key) ||
|
|
309
|
-
(utils.isObject(val) &&
|
|
414
|
+
(utils.isObject(val) && utils.hasOwnKeys(val) === false);
|
|
310
415
|
}
|
|
311
416
|
} else {
|
|
312
|
-
const
|
|
313
|
-
|
|
314
|
-
|
|
417
|
+
const isModifier = !isTopLevelNestedDollarPath && schematype == null &&
|
|
418
|
+
(key === '$each' || key === '$or' || key === '$and' || key === '$in');
|
|
419
|
+
const checkPath = isModifier ? prefix : fullPath;
|
|
420
|
+
if (isModifier) {
|
|
421
|
+
schematype = schema._getSchema(checkPath);
|
|
422
|
+
}
|
|
315
423
|
|
|
316
424
|
// You can use `$setOnInsert` with immutable keys
|
|
317
425
|
if (op !== '$setOnInsert' &&
|
|
318
|
-
|
|
319
|
-
handleImmutable(schematype, strict, obj, key, prefix + key, context)) {
|
|
426
|
+
handleImmutable(schematype, strict, obj, key, prefix + key, options, context)) {
|
|
320
427
|
continue;
|
|
321
428
|
}
|
|
322
429
|
|
|
@@ -333,7 +440,7 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
|
|
|
333
440
|
}
|
|
334
441
|
|
|
335
442
|
let isStrict = strict;
|
|
336
|
-
if (pathDetails
|
|
443
|
+
if (pathDetails?.schema && strict == null) {
|
|
337
444
|
isStrict = pathDetails.schema.options.strict;
|
|
338
445
|
}
|
|
339
446
|
|
|
@@ -344,23 +451,29 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
|
|
|
344
451
|
if (skip) {
|
|
345
452
|
// Even if strict is `throw`, avoid throwing an error because of
|
|
346
453
|
// virtuals because of #6731
|
|
347
|
-
if (isStrict === 'throw' && schema.virtuals
|
|
454
|
+
if (isStrict === 'throw' && getOwnProperty(schema.virtuals, checkPath) == null) {
|
|
348
455
|
throw new StrictModeError(prefix + key);
|
|
349
456
|
} else {
|
|
350
457
|
delete obj[key];
|
|
351
458
|
}
|
|
352
459
|
} else {
|
|
353
|
-
// gh-1845 temporary fix: ignore $rename. See gh-3027 for tracking
|
|
354
|
-
// improving this.
|
|
355
460
|
if (op === '$rename') {
|
|
356
|
-
|
|
461
|
+
try {
|
|
462
|
+
if (obj[key] == null) {
|
|
463
|
+
throw new CastError('String', obj[key], `${prefix}${key}.$rename`);
|
|
464
|
+
}
|
|
465
|
+
const schematype = new SchemaString(`${prefix}${key}.$rename`, null, null, schema);
|
|
466
|
+
obj[key] = schematype.castForQuery(null, obj[key], context);
|
|
467
|
+
} catch (error) {
|
|
468
|
+
aggregatedError = _appendError(error, context, key, aggregatedError);
|
|
469
|
+
}
|
|
357
470
|
continue;
|
|
358
471
|
}
|
|
359
472
|
|
|
360
473
|
try {
|
|
361
474
|
if (prefix.length === 0 || key.indexOf('.') === -1) {
|
|
362
475
|
obj[key] = castUpdateVal(schematype, val, op, key, context, prefix + key);
|
|
363
|
-
} else {
|
|
476
|
+
} else if (isStrict !== false || schematype != null) {
|
|
364
477
|
// Setting a nested dotted path that's in the schema. We don't allow paths with '.' in
|
|
365
478
|
// a schema, so replace the dotted path with a nested object to avoid ending up with
|
|
366
479
|
// dotted properties in the updated object. See (gh-10200)
|
|
@@ -373,9 +486,9 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
|
|
|
373
486
|
|
|
374
487
|
if (Array.isArray(obj[key]) && (op === '$addToSet' || op === '$push') && key !== '$each') {
|
|
375
488
|
if (schematype &&
|
|
376
|
-
schematype.
|
|
377
|
-
!schematype.
|
|
378
|
-
!schematype.
|
|
489
|
+
schematype.embeddedSchemaType &&
|
|
490
|
+
!schematype.embeddedSchemaType.$isMongooseArray &&
|
|
491
|
+
!schematype.embeddedSchemaType[schemaMixedSymbol]) {
|
|
379
492
|
obj[key] = { $each: obj[key] };
|
|
380
493
|
}
|
|
381
494
|
}
|
|
@@ -402,6 +515,14 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
|
|
|
402
515
|
*/
|
|
403
516
|
|
|
404
517
|
function _appendError(error, query, key, aggregatedError) {
|
|
518
|
+
// Set the model on cast errors so `{MODEL}` gets replaced, like it is for
|
|
519
|
+
// query filter casting and document validation (gh-8300). `query` is a
|
|
520
|
+
// Query, or the model itself when casting a `bulkWrite()` operation.
|
|
521
|
+
const model = query?.modelName != null ? query : query?.model;
|
|
522
|
+
if (model != null && typeof error.setModel === 'function') {
|
|
523
|
+
error.setModel(model);
|
|
524
|
+
}
|
|
525
|
+
|
|
405
526
|
if (typeof query !== 'object' || !query.options.multipleCastError) {
|
|
406
527
|
throw error;
|
|
407
528
|
}
|
|
@@ -456,11 +577,11 @@ const overwriteOps = {
|
|
|
456
577
|
* Casts `val` according to `schema` and atomic `op`.
|
|
457
578
|
*
|
|
458
579
|
* @param {SchemaType} schema
|
|
459
|
-
* @param {
|
|
460
|
-
* @param {
|
|
461
|
-
* @param {
|
|
580
|
+
* @param {object} val
|
|
581
|
+
* @param {string} op the atomic operator ($pull, $set, etc)
|
|
582
|
+
* @param {string} $conditional
|
|
462
583
|
* @param {Query} context
|
|
463
|
-
* @param {
|
|
584
|
+
* @param {string} path
|
|
464
585
|
* @api private
|
|
465
586
|
*/
|
|
466
587
|
|
|
@@ -470,17 +591,16 @@ function castUpdateVal(schema, val, op, $conditional, context, path) {
|
|
|
470
591
|
if (op in numberOps) {
|
|
471
592
|
try {
|
|
472
593
|
return castNumber(val);
|
|
473
|
-
} catch
|
|
594
|
+
} catch {
|
|
474
595
|
throw new CastError('number', val, path);
|
|
475
596
|
}
|
|
476
597
|
}
|
|
477
598
|
return val;
|
|
478
599
|
}
|
|
479
600
|
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
(utils.isObject(val) || Array.isArray(val));
|
|
601
|
+
const cond = schema.$isMongooseArray
|
|
602
|
+
&& op in castOps
|
|
603
|
+
&& (utils.isObject(val) || Array.isArray(val));
|
|
484
604
|
if (cond && !overwriteOps[op]) {
|
|
485
605
|
// Cast values for ops that add data to MongoDB.
|
|
486
606
|
// Ensures embedded documents get ObjectIds etc.
|
|
@@ -488,7 +608,7 @@ function castUpdateVal(schema, val, op, $conditional, context, path) {
|
|
|
488
608
|
let cur = schema;
|
|
489
609
|
while (cur.$isMongooseArray) {
|
|
490
610
|
++schemaArrayDepth;
|
|
491
|
-
cur = cur.
|
|
611
|
+
cur = cur.embeddedSchemaType;
|
|
492
612
|
}
|
|
493
613
|
let arrayDepth = 0;
|
|
494
614
|
let _val = val;
|
|
@@ -521,14 +641,15 @@ function castUpdateVal(schema, val, op, $conditional, context, path) {
|
|
|
521
641
|
}
|
|
522
642
|
if (op === '$inc') {
|
|
523
643
|
// Support `$inc` with long, int32, etc. (gh-4283)
|
|
524
|
-
return schema.
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
644
|
+
return schema.castForQuery(
|
|
645
|
+
null,
|
|
646
|
+
val,
|
|
647
|
+
context
|
|
648
|
+
);
|
|
528
649
|
}
|
|
529
650
|
try {
|
|
530
651
|
return castNumber(val);
|
|
531
|
-
} catch
|
|
652
|
+
} catch {
|
|
532
653
|
throw new CastError('number', val, schema.path);
|
|
533
654
|
}
|
|
534
655
|
}
|
|
@@ -539,22 +660,29 @@ function castUpdateVal(schema, val, op, $conditional, context, path) {
|
|
|
539
660
|
return Boolean(val);
|
|
540
661
|
}
|
|
541
662
|
|
|
542
|
-
if (
|
|
543
|
-
return schema.
|
|
544
|
-
$conditional
|
|
545
|
-
val
|
|
546
|
-
context
|
|
547
|
-
|
|
663
|
+
if (mongodbUpdateOperators.has($conditional)) {
|
|
664
|
+
return schema.castForQuery(
|
|
665
|
+
$conditional,
|
|
666
|
+
val,
|
|
667
|
+
context
|
|
668
|
+
);
|
|
548
669
|
}
|
|
549
670
|
|
|
550
671
|
if (overwriteOps[op]) {
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
672
|
+
const skipQueryCastForUpdate = val != null
|
|
673
|
+
&& schema.$isMongooseArray
|
|
674
|
+
&& schema.$fullPath != null
|
|
675
|
+
&& !schema.$fullPath.match(/\d+$/);
|
|
676
|
+
const applySetters = schema[schemaMixedSymbol] != null;
|
|
677
|
+
if (skipQueryCastForUpdate || applySetters) {
|
|
678
|
+
return schema.applySetters(val, context);
|
|
679
|
+
}
|
|
680
|
+
return schema.castForQuery(
|
|
681
|
+
null,
|
|
682
|
+
val,
|
|
683
|
+
context
|
|
684
|
+
);
|
|
557
685
|
}
|
|
558
686
|
|
|
559
|
-
return schema.
|
|
687
|
+
return schema.castForQuery(null, val, context);
|
|
560
688
|
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const PopulateOptions = require('../../options/populateOptions');
|
|
4
|
+
const clone = require('../clone');
|
|
5
|
+
const specialProperties = require('../specialProperties');
|
|
6
|
+
|
|
7
|
+
/*!
|
|
8
|
+
* Deep clone a query's `_mongooseOptions` so a cloned query doesn't share state with the
|
|
9
|
+
* original. `clone()` returns class instances as-is, so explicitly copy `PopulateOptions`,
|
|
10
|
+
* including nested populate: populate options are mutable, and executing a query writes
|
|
11
|
+
* per-execution state like `session` and `lean` into them.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
module.exports = function cloneMongooseOptions(mongooseOptions) {
|
|
15
|
+
const { populate, ...rest } = mongooseOptions;
|
|
16
|
+
const ret = clone(rest);
|
|
17
|
+
if (populate != null) {
|
|
18
|
+
ret.populate = {};
|
|
19
|
+
for (const path of Object.keys(populate)) {
|
|
20
|
+
if (specialProperties.has(path)) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
ret.populate[path] = clonePopulate(populate[path]);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return ret;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function clonePopulate(pop) {
|
|
30
|
+
if (Array.isArray(pop)) {
|
|
31
|
+
return pop.map(clonePopulate);
|
|
32
|
+
}
|
|
33
|
+
if (pop == null || typeof pop !== 'object') {
|
|
34
|
+
return pop;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Skip `_docs` and `_childDocs`: they hold results of previous executions, see gh-11641
|
|
38
|
+
const { populate, _docs, _childDocs, ...rest } = pop;
|
|
39
|
+
const ret = pop instanceof PopulateOptions ? new PopulateOptions(rest) : clone(rest);
|
|
40
|
+
if (populate !== undefined) {
|
|
41
|
+
ret.populate = clonePopulate(populate);
|
|
42
|
+
}
|
|
43
|
+
return ret;
|
|
44
|
+
}
|
|
@@ -3,32 +3,35 @@
|
|
|
3
3
|
const cleanPositionalOperators = require('../schema/cleanPositionalOperators');
|
|
4
4
|
const get = require('../get');
|
|
5
5
|
const getDiscriminatorByValue = require('../discriminator/getDiscriminatorByValue');
|
|
6
|
+
const getDiscriminatorValueFromFilter = require('../discriminator/getDiscriminatorValueFromFilter');
|
|
6
7
|
const updatedPathsByArrayFilter = require('../update/updatedPathsByArrayFilter');
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Like `schema.path()`, except with a document, because impossible to
|
|
10
11
|
* determine path type without knowing the embedded discriminator key.
|
|
11
12
|
* @param {Schema} schema
|
|
12
|
-
* @param {
|
|
13
|
-
* @param {
|
|
14
|
-
* @param {
|
|
15
|
-
* @param {
|
|
13
|
+
* @param {object} [update]
|
|
14
|
+
* @param {object} [filter]
|
|
15
|
+
* @param {string} path
|
|
16
|
+
* @param {object} [options]
|
|
16
17
|
* @api private
|
|
17
18
|
*/
|
|
18
19
|
|
|
19
20
|
module.exports = function getEmbeddedDiscriminatorPath(schema, update, filter, path, options) {
|
|
20
|
-
const parts = path.split('.');
|
|
21
|
+
const parts = path.indexOf('.') === -1 ? [path] : path.split('.');
|
|
21
22
|
let schematype = null;
|
|
22
23
|
let type = 'adhocOrUndefined';
|
|
23
24
|
|
|
24
25
|
filter = filter || {};
|
|
25
26
|
update = update || {};
|
|
26
|
-
const arrayFilters =
|
|
27
|
+
const arrayFilters = Array.isArray(options?.arrayFilters) ?
|
|
27
28
|
options.arrayFilters : [];
|
|
28
29
|
const updatedPathsByFilter = updatedPathsByArrayFilter(update);
|
|
30
|
+
let startIndex = 0;
|
|
29
31
|
|
|
30
32
|
for (let i = 0; i < parts.length; ++i) {
|
|
31
|
-
const
|
|
33
|
+
const originalSubpath = parts.slice(startIndex, i + 1).join('.');
|
|
34
|
+
const subpath = cleanPositionalOperators(originalSubpath);
|
|
32
35
|
schematype = schema.path(subpath);
|
|
33
36
|
if (schematype == null) {
|
|
34
37
|
continue;
|
|
@@ -44,16 +47,21 @@ module.exports = function getEmbeddedDiscriminatorPath(schema, update, filter, p
|
|
|
44
47
|
let discriminatorKey = null;
|
|
45
48
|
|
|
46
49
|
if (discriminatorValuePath in filter) {
|
|
47
|
-
discriminatorKey = filter[discriminatorValuePath];
|
|
50
|
+
discriminatorKey = getDiscriminatorValueFromFilter(filter[discriminatorValuePath]);
|
|
48
51
|
}
|
|
49
52
|
if (discriminatorFilterPath in filter) {
|
|
50
|
-
discriminatorKey = filter[discriminatorFilterPath];
|
|
53
|
+
discriminatorKey = getDiscriminatorValueFromFilter(filter[discriminatorFilterPath]);
|
|
51
54
|
}
|
|
52
55
|
|
|
53
56
|
const wrapperPath = subpath.replace(/\.\d+$/, '');
|
|
54
57
|
if (schematype.$isMongooseDocumentArrayElement &&
|
|
55
58
|
get(filter[wrapperPath], '$elemMatch.' + key) != null) {
|
|
56
|
-
discriminatorKey = filter[wrapperPath].$elemMatch[key];
|
|
59
|
+
discriminatorKey = getDiscriminatorValueFromFilter(filter[wrapperPath].$elemMatch[key]);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const discriminatorKeyUpdatePath = originalSubpath + '.' + key;
|
|
63
|
+
if (discriminatorKeyUpdatePath in update) {
|
|
64
|
+
discriminatorKey = update[discriminatorKeyUpdatePath];
|
|
57
65
|
}
|
|
58
66
|
|
|
59
67
|
if (discriminatorValuePath in update) {
|
|
@@ -64,7 +72,7 @@ module.exports = function getEmbeddedDiscriminatorPath(schema, update, filter, p
|
|
|
64
72
|
const schemaKey = updatedPathsByFilter[filterKey] + '.' + key;
|
|
65
73
|
const arrayFilterKey = filterKey + '.' + key;
|
|
66
74
|
if (schemaKey === discriminatorFilterPath) {
|
|
67
|
-
const filter = arrayFilters.find(filter =>
|
|
75
|
+
const filter = arrayFilters.find(filter => Object.hasOwn(filter, arrayFilterKey));
|
|
68
76
|
if (filter != null) {
|
|
69
77
|
discriminatorKey = filter[arrayFilterKey];
|
|
70
78
|
}
|
|
@@ -75,10 +83,16 @@ module.exports = function getEmbeddedDiscriminatorPath(schema, update, filter, p
|
|
|
75
83
|
continue;
|
|
76
84
|
}
|
|
77
85
|
|
|
78
|
-
const
|
|
86
|
+
const discriminator = getDiscriminatorByValue(schematype.Constructor.discriminators, discriminatorKey);
|
|
87
|
+
const discriminatorSchema = discriminator?.schema;
|
|
88
|
+
if (discriminatorSchema == null) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
79
91
|
|
|
80
92
|
const rest = parts.slice(i + 1).join('.');
|
|
81
|
-
schematype = discriminatorSchema.
|
|
93
|
+
schematype = discriminatorSchema._getSchema(rest);
|
|
94
|
+
schema = discriminatorSchema;
|
|
95
|
+
startIndex = i + 1;
|
|
82
96
|
if (schematype != null) {
|
|
83
97
|
type = discriminatorSchema._getPathType(rest);
|
|
84
98
|
break;
|