@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
|
@@ -1,222 +1,312 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const MongooseError = require('../../error/mongooseError');
|
|
3
4
|
const getDiscriminatorByValue = require('../../helpers/discriminator/getDiscriminatorByValue');
|
|
4
5
|
const applyTimestampsToChildren = require('../update/applyTimestampsToChildren');
|
|
5
6
|
const applyTimestampsToUpdate = require('../update/applyTimestampsToUpdate');
|
|
6
7
|
const cast = require('../../cast');
|
|
7
8
|
const castUpdate = require('../query/castUpdate');
|
|
9
|
+
const { castArrayFiltersForUpdate } = require('../update/castArrayFilters');
|
|
10
|
+
const clone = require('../clone');
|
|
11
|
+
const decorateUpdateWithVersionKey = require('../update/decorateUpdateWithVersionKey');
|
|
12
|
+
const { inspect } = require('util');
|
|
8
13
|
const setDefaultsOnInsert = require('../setDefaultsOnInsert');
|
|
9
14
|
|
|
10
15
|
/**
|
|
11
16
|
* Given a model and a bulkWrite op, return a thunk that handles casting and
|
|
12
17
|
* validating the individual op.
|
|
13
18
|
* @param {Model} originalModel
|
|
14
|
-
* @param {
|
|
15
|
-
* @param {
|
|
19
|
+
* @param {object} op
|
|
20
|
+
* @param {object} [options]
|
|
16
21
|
* @api private
|
|
17
22
|
*/
|
|
18
23
|
|
|
19
24
|
module.exports = function castBulkWrite(originalModel, op, options) {
|
|
20
|
-
|
|
25
|
+
let now;
|
|
26
|
+
const timestampOpts = originalModel.schema.get('timestamps');
|
|
27
|
+
if (typeof timestampOpts?.currentTime === 'function') {
|
|
28
|
+
now = timestampOpts.currentTime();
|
|
29
|
+
} else {
|
|
30
|
+
now = originalModel.base.now();
|
|
31
|
+
}
|
|
21
32
|
|
|
22
33
|
if (op['insertOne']) {
|
|
23
|
-
return (callback) =>
|
|
24
|
-
const model = decideModelByObject(originalModel, op['insertOne']['document']);
|
|
25
|
-
|
|
26
|
-
const doc = new model(op['insertOne']['document']);
|
|
27
|
-
if (model.schema.options.timestamps && options.timestamps !== false) {
|
|
28
|
-
doc.initializeTimestamps();
|
|
29
|
-
}
|
|
30
|
-
if (options.session != null) {
|
|
31
|
-
doc.$session(options.session);
|
|
32
|
-
}
|
|
33
|
-
op['insertOne']['document'] = doc;
|
|
34
|
-
|
|
35
|
-
if (options.skipValidation || op['insertOne'].skipValidation) {
|
|
36
|
-
callback(null);
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
op['insertOne']['document'].$validate({ __noPromise: true }, function(error) {
|
|
41
|
-
if (error) {
|
|
42
|
-
return callback(error, null);
|
|
43
|
-
}
|
|
44
|
-
callback(null);
|
|
45
|
-
});
|
|
46
|
-
};
|
|
34
|
+
return callback => module.exports.castInsertOne(originalModel, op['insertOne'], options).then(() => callback(null), err => callback(err));
|
|
47
35
|
} else if (op['updateOne']) {
|
|
48
36
|
return (callback) => {
|
|
49
37
|
try {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
throw new Error('Must provide an update object.');
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const model = decideModelByObject(originalModel, op['updateOne']['filter']);
|
|
58
|
-
const schema = model.schema;
|
|
59
|
-
const strict = options.strict != null ? options.strict : model.schema.options.strict;
|
|
60
|
-
|
|
61
|
-
_addDiscriminatorToObject(schema, op['updateOne']['filter']);
|
|
62
|
-
|
|
63
|
-
if (model.schema.$timestamps != null && op['updateOne'].timestamps !== false) {
|
|
64
|
-
const createdAt = model.schema.$timestamps.createdAt;
|
|
65
|
-
const updatedAt = model.schema.$timestamps.updatedAt;
|
|
66
|
-
applyTimestampsToUpdate(now, createdAt, updatedAt, op['updateOne']['update'], {});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
applyTimestampsToChildren(now, op['updateOne']['update'], model.schema);
|
|
70
|
-
|
|
71
|
-
if (op['updateOne'].setDefaultsOnInsert !== false) {
|
|
72
|
-
setDefaultsOnInsert(op['updateOne']['filter'], model.schema, op['updateOne']['update'], {
|
|
73
|
-
setDefaultsOnInsert: true,
|
|
74
|
-
upsert: op['updateOne'].upsert
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
op['updateOne']['filter'] = cast(model.schema, op['updateOne']['filter'], {
|
|
79
|
-
strict: strict,
|
|
80
|
-
upsert: op['updateOne'].upsert
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
op['updateOne']['update'] = castUpdate(model.schema, op['updateOne']['update'], {
|
|
84
|
-
strict: strict,
|
|
85
|
-
overwrite: false,
|
|
86
|
-
upsert: op['updateOne'].upsert
|
|
87
|
-
}, model, op['updateOne']['filter']);
|
|
88
|
-
} catch (error) {
|
|
89
|
-
return callback(error, null);
|
|
38
|
+
module.exports.castUpdateOne(originalModel, op['updateOne'], options, now);
|
|
39
|
+
callback(null);
|
|
40
|
+
} catch (err) {
|
|
41
|
+
callback(err);
|
|
90
42
|
}
|
|
91
|
-
|
|
92
|
-
callback(null);
|
|
93
43
|
};
|
|
94
44
|
} else if (op['updateMany']) {
|
|
95
45
|
return (callback) => {
|
|
96
46
|
try {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
throw new Error('Must provide an update object.');
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const model = decideModelByObject(originalModel, op['updateMany']['filter']);
|
|
105
|
-
const schema = model.schema;
|
|
106
|
-
const strict = options.strict != null ? options.strict : model.schema.options.strict;
|
|
107
|
-
|
|
108
|
-
if (op['updateMany'].setDefaultsOnInsert !== false) {
|
|
109
|
-
setDefaultsOnInsert(op['updateMany']['filter'], model.schema, op['updateMany']['update'], {
|
|
110
|
-
setDefaultsOnInsert: true,
|
|
111
|
-
upsert: op['updateMany'].upsert
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (model.schema.$timestamps != null && op['updateMany'].timestamps !== false) {
|
|
116
|
-
const createdAt = model.schema.$timestamps.createdAt;
|
|
117
|
-
const updatedAt = model.schema.$timestamps.updatedAt;
|
|
118
|
-
applyTimestampsToUpdate(now, createdAt, updatedAt, op['updateMany']['update'], {});
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
applyTimestampsToChildren(now, op['updateMany']['update'], model.schema);
|
|
122
|
-
|
|
123
|
-
_addDiscriminatorToObject(schema, op['updateMany']['filter']);
|
|
124
|
-
|
|
125
|
-
op['updateMany']['filter'] = cast(model.schema, op['updateMany']['filter'], {
|
|
126
|
-
strict: strict,
|
|
127
|
-
upsert: op['updateMany'].upsert
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
op['updateMany']['update'] = castUpdate(model.schema, op['updateMany']['update'], {
|
|
131
|
-
strict: strict,
|
|
132
|
-
overwrite: false,
|
|
133
|
-
upsert: op['updateMany'].upsert
|
|
134
|
-
}, model, op['updateMany']['filter']);
|
|
135
|
-
} catch (error) {
|
|
136
|
-
return callback(error, null);
|
|
47
|
+
module.exports.castUpdateMany(originalModel, op['updateMany'], options, now);
|
|
48
|
+
callback(null);
|
|
49
|
+
} catch (err) {
|
|
50
|
+
callback(err);
|
|
137
51
|
}
|
|
138
|
-
|
|
139
|
-
callback(null);
|
|
140
52
|
};
|
|
141
53
|
} else if (op['replaceOne']) {
|
|
142
54
|
return (callback) => {
|
|
143
|
-
|
|
144
|
-
const schema = model.schema;
|
|
145
|
-
const strict = options.strict != null ? options.strict : model.schema.options.strict;
|
|
146
|
-
|
|
147
|
-
_addDiscriminatorToObject(schema, op['replaceOne']['filter']);
|
|
148
|
-
try {
|
|
149
|
-
op['replaceOne']['filter'] = cast(model.schema, op['replaceOne']['filter'], {
|
|
150
|
-
strict: strict,
|
|
151
|
-
upsert: op['replaceOne'].upsert
|
|
152
|
-
});
|
|
153
|
-
} catch (error) {
|
|
154
|
-
return callback(error, null);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// set `skipId`, otherwise we get "_id field cannot be changed"
|
|
158
|
-
const doc = new model(op['replaceOne']['replacement'], strict, true);
|
|
159
|
-
if (model.schema.options.timestamps) {
|
|
160
|
-
doc.initializeTimestamps();
|
|
161
|
-
}
|
|
162
|
-
if (options.session != null) {
|
|
163
|
-
doc.$session(options.session);
|
|
164
|
-
}
|
|
165
|
-
op['replaceOne']['replacement'] = doc;
|
|
166
|
-
|
|
167
|
-
if (options.skipValidation || op['replaceOne'].skipValidation) {
|
|
168
|
-
op['replaceOne']['replacement'] = op['replaceOne']['replacement'].toBSON();
|
|
169
|
-
callback(null);
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
op['replaceOne']['replacement'].$validate({ __noPromise: true }, function(error) {
|
|
174
|
-
if (error) {
|
|
175
|
-
return callback(error, null);
|
|
176
|
-
}
|
|
177
|
-
op['replaceOne']['replacement'] = op['replaceOne']['replacement'].toBSON();
|
|
178
|
-
callback(null);
|
|
179
|
-
});
|
|
55
|
+
module.exports.castReplaceOne(originalModel, op['replaceOne'], options).then(() => callback(null), err => callback(err));
|
|
180
56
|
};
|
|
181
57
|
} else if (op['deleteOne']) {
|
|
182
58
|
return (callback) => {
|
|
183
|
-
const model = decideModelByObject(originalModel, op['deleteOne']['filter']);
|
|
184
|
-
const schema = model.schema;
|
|
185
|
-
|
|
186
|
-
_addDiscriminatorToObject(schema, op['deleteOne']['filter']);
|
|
187
|
-
|
|
188
59
|
try {
|
|
189
|
-
op['deleteOne']
|
|
190
|
-
|
|
191
|
-
} catch (
|
|
192
|
-
|
|
60
|
+
module.exports.castDeleteOne(originalModel, op['deleteOne']);
|
|
61
|
+
callback(null);
|
|
62
|
+
} catch (err) {
|
|
63
|
+
callback(err);
|
|
193
64
|
}
|
|
194
|
-
|
|
195
|
-
callback(null);
|
|
196
65
|
};
|
|
197
66
|
} else if (op['deleteMany']) {
|
|
198
67
|
return (callback) => {
|
|
199
|
-
const model = decideModelByObject(originalModel, op['deleteMany']['filter']);
|
|
200
|
-
const schema = model.schema;
|
|
201
|
-
|
|
202
|
-
_addDiscriminatorToObject(schema, op['deleteMany']['filter']);
|
|
203
|
-
|
|
204
68
|
try {
|
|
205
|
-
op['deleteMany']
|
|
206
|
-
|
|
207
|
-
} catch (
|
|
208
|
-
|
|
69
|
+
module.exports.castDeleteMany(originalModel, op['deleteMany']);
|
|
70
|
+
callback(null);
|
|
71
|
+
} catch (err) {
|
|
72
|
+
callback(err);
|
|
209
73
|
}
|
|
210
|
-
|
|
211
|
-
callback(null);
|
|
212
74
|
};
|
|
213
75
|
} else {
|
|
214
76
|
return (callback) => {
|
|
215
|
-
|
|
77
|
+
const error = new MongooseError(`Invalid op passed to \`bulkWrite()\`: ${inspect(op)}`);
|
|
78
|
+
callback(error, null);
|
|
216
79
|
};
|
|
217
80
|
}
|
|
218
81
|
};
|
|
219
82
|
|
|
83
|
+
module.exports.castInsertOne = async function castInsertOne(originalModel, insertOne, options) {
|
|
84
|
+
const model = decideModelByObject(originalModel, insertOne['document']);
|
|
85
|
+
|
|
86
|
+
const doc = new model(insertOne['document']);
|
|
87
|
+
if (model.schema.options.timestamps && getTimestampsOpt(insertOne, options)) {
|
|
88
|
+
doc.initializeTimestamps();
|
|
89
|
+
}
|
|
90
|
+
if (options.session != null) {
|
|
91
|
+
doc.$session(options.session);
|
|
92
|
+
}
|
|
93
|
+
const versionKey = model?.schema?.options?.versionKey;
|
|
94
|
+
if (versionKey && doc[versionKey] == null) {
|
|
95
|
+
doc[versionKey] = 0;
|
|
96
|
+
}
|
|
97
|
+
insertOne['document'] = doc;
|
|
98
|
+
|
|
99
|
+
if (options.skipValidation || insertOne.skipValidation) {
|
|
100
|
+
return insertOne;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
await insertOne['document'].$validate();
|
|
104
|
+
return insertOne;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
module.exports.castUpdateOne = function castUpdateOne(originalModel, updateOne, options, now) {
|
|
108
|
+
if (!updateOne['filter']) {
|
|
109
|
+
throw new Error('Must provide a filter object.');
|
|
110
|
+
}
|
|
111
|
+
if (!updateOne['update']) {
|
|
112
|
+
throw new Error('Must provide an update object.');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const model = decideModelByObject(originalModel, updateOne['filter']);
|
|
116
|
+
const schema = model.schema;
|
|
117
|
+
const strict = options.strict ?? model.schema.options.strict;
|
|
118
|
+
|
|
119
|
+
const update = clone(updateOne['update']);
|
|
120
|
+
|
|
121
|
+
_addDiscriminatorToObject(schema, updateOne['filter']);
|
|
122
|
+
|
|
123
|
+
const doInitTimestamps = getTimestampsOpt(updateOne, options);
|
|
124
|
+
|
|
125
|
+
if (model.schema.$timestamps != null && doInitTimestamps) {
|
|
126
|
+
const createdAt = model.schema.$timestamps.createdAt;
|
|
127
|
+
const updatedAt = model.schema.$timestamps.updatedAt;
|
|
128
|
+
applyTimestampsToUpdate(now, createdAt, updatedAt, update, {
|
|
129
|
+
timestamps: updateOne.timestamps,
|
|
130
|
+
overwriteImmutable: updateOne.overwriteImmutable
|
|
131
|
+
}, updateOne.upsert);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (doInitTimestamps) {
|
|
135
|
+
applyTimestampsToChildren(now, update, model.schema);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const globalSetDefaultsOnInsert = originalModel.base.options.setDefaultsOnInsert;
|
|
139
|
+
const shouldSetDefaultsOnInsert = updateOne.setDefaultsOnInsert ?? globalSetDefaultsOnInsert;
|
|
140
|
+
if (shouldSetDefaultsOnInsert !== false) {
|
|
141
|
+
setDefaultsOnInsert(updateOne['filter'], model.schema, update, {
|
|
142
|
+
setDefaultsOnInsert: true,
|
|
143
|
+
upsert: updateOne.upsert
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
decorateUpdateWithVersionKey(
|
|
148
|
+
update,
|
|
149
|
+
updateOne,
|
|
150
|
+
model.schema.options.versionKey
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
updateOne['filter'] = _castFilter(model, updateOne['filter'], {
|
|
154
|
+
strict: strict,
|
|
155
|
+
upsert: updateOne.upsert
|
|
156
|
+
});
|
|
157
|
+
if (Array.isArray(updateOne.arrayFilters)) {
|
|
158
|
+
updateOne.arrayFilters = clone(updateOne.arrayFilters);
|
|
159
|
+
castArrayFiltersForUpdate(updateOne.arrayFilters, update, schema, model, options);
|
|
160
|
+
}
|
|
161
|
+
updateOne['update'] = castUpdate(model.schema, update, {
|
|
162
|
+
strict: strict,
|
|
163
|
+
upsert: updateOne.upsert,
|
|
164
|
+
arrayFilters: updateOne.arrayFilters,
|
|
165
|
+
overwriteDiscriminatorKey: updateOne.overwriteDiscriminatorKey,
|
|
166
|
+
overwriteImmutable: updateOne.overwriteImmutable
|
|
167
|
+
}, model, updateOne['filter']);
|
|
168
|
+
|
|
169
|
+
return updateOne;
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
module.exports.castUpdateMany = function castUpdateMany(originalModel, updateMany, options, now) {
|
|
173
|
+
if (!updateMany['filter']) {
|
|
174
|
+
throw new Error('Must provide a filter object.');
|
|
175
|
+
}
|
|
176
|
+
if (!updateMany['update']) {
|
|
177
|
+
throw new Error('Must provide an update object.');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const model = decideModelByObject(originalModel, updateMany['filter']);
|
|
181
|
+
const schema = model.schema;
|
|
182
|
+
const strict = options.strict ?? model.schema.options.strict;
|
|
183
|
+
|
|
184
|
+
const update = clone(updateMany['update']);
|
|
185
|
+
|
|
186
|
+
const globalSetDefaultsOnInsert = originalModel.base.options.setDefaultsOnInsert;
|
|
187
|
+
const shouldSetDefaultsOnInsert = updateMany.setDefaultsOnInsert ?? globalSetDefaultsOnInsert;
|
|
188
|
+
|
|
189
|
+
if (shouldSetDefaultsOnInsert !== false) {
|
|
190
|
+
setDefaultsOnInsert(updateMany['filter'], model.schema, update, {
|
|
191
|
+
setDefaultsOnInsert: true,
|
|
192
|
+
upsert: updateMany.upsert
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const doInitTimestamps = getTimestampsOpt(updateMany, options);
|
|
197
|
+
|
|
198
|
+
if (model.schema.$timestamps != null && doInitTimestamps) {
|
|
199
|
+
const createdAt = model.schema.$timestamps.createdAt;
|
|
200
|
+
const updatedAt = model.schema.$timestamps.updatedAt;
|
|
201
|
+
applyTimestampsToUpdate(now, createdAt, updatedAt, update, {
|
|
202
|
+
timestamps: updateMany.timestamps,
|
|
203
|
+
overwriteImmutable: updateMany.overwriteImmutable
|
|
204
|
+
}, updateMany.upsert);
|
|
205
|
+
}
|
|
206
|
+
if (doInitTimestamps) {
|
|
207
|
+
applyTimestampsToChildren(now, update, model.schema);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
_addDiscriminatorToObject(schema, updateMany['filter']);
|
|
211
|
+
|
|
212
|
+
decorateUpdateWithVersionKey(
|
|
213
|
+
update,
|
|
214
|
+
updateMany,
|
|
215
|
+
model.schema.options.versionKey
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
updateMany['filter'] = _castFilter(model, updateMany['filter'], {
|
|
219
|
+
strict: strict,
|
|
220
|
+
upsert: updateMany.upsert
|
|
221
|
+
});
|
|
222
|
+
if (Array.isArray(updateMany.arrayFilters)) {
|
|
223
|
+
updateMany.arrayFilters = clone(updateMany.arrayFilters);
|
|
224
|
+
castArrayFiltersForUpdate(updateMany.arrayFilters, update, schema, model, options);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
updateMany['update'] = castUpdate(model.schema, update, {
|
|
228
|
+
strict: strict,
|
|
229
|
+
upsert: updateMany.upsert,
|
|
230
|
+
arrayFilters: updateMany.arrayFilters,
|
|
231
|
+
overwriteDiscriminatorKey: updateMany.overwriteDiscriminatorKey,
|
|
232
|
+
overwriteImmutable: updateMany.overwriteImmutable
|
|
233
|
+
}, model, updateMany['filter']);
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
module.exports.castReplaceOne = async function castReplaceOne(originalModel, replaceOne, options) {
|
|
237
|
+
const model = decideModelByObject(originalModel, replaceOne['filter']);
|
|
238
|
+
const schema = model.schema;
|
|
239
|
+
const strict = options.strict ?? model.schema.options.strict;
|
|
240
|
+
|
|
241
|
+
_addDiscriminatorToObject(schema, replaceOne['filter']);
|
|
242
|
+
replaceOne['filter'] = _castFilter(model, replaceOne['filter'], {
|
|
243
|
+
strict: strict,
|
|
244
|
+
upsert: replaceOne.upsert
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
// set `skipId`, otherwise we get "_id field cannot be changed"
|
|
248
|
+
const doc = new model(replaceOne['replacement'], strict, { skipId: true });
|
|
249
|
+
if (model.schema.options.timestamps && getTimestampsOpt(replaceOne, options)) {
|
|
250
|
+
doc.initializeTimestamps();
|
|
251
|
+
}
|
|
252
|
+
if (options.session != null) {
|
|
253
|
+
doc.$session(options.session);
|
|
254
|
+
}
|
|
255
|
+
const versionKey = model?.schema?.options?.versionKey;
|
|
256
|
+
if (versionKey && doc[versionKey] == null) {
|
|
257
|
+
doc[versionKey] = 0;
|
|
258
|
+
}
|
|
259
|
+
replaceOne['replacement'] = doc;
|
|
260
|
+
|
|
261
|
+
if (options.skipValidation || replaceOne.skipValidation) {
|
|
262
|
+
replaceOne['replacement'] = replaceOne['replacement'].toBSON();
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
await replaceOne['replacement'].$validate();
|
|
267
|
+
replaceOne['replacement'] = replaceOne['replacement'].toBSON();
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
module.exports.castDeleteOne = function castDeleteOne(originalModel, deleteOne) {
|
|
271
|
+
const model = decideModelByObject(originalModel, deleteOne['filter']);
|
|
272
|
+
const schema = model.schema;
|
|
273
|
+
|
|
274
|
+
_addDiscriminatorToObject(schema, deleteOne['filter']);
|
|
275
|
+
|
|
276
|
+
deleteOne['filter'] = _castFilter(model, deleteOne['filter']);
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
module.exports.castDeleteMany = function castDeleteMany(originalModel, deleteMany) {
|
|
280
|
+
const model = decideModelByObject(originalModel, deleteMany['filter']);
|
|
281
|
+
const schema = model.schema;
|
|
282
|
+
|
|
283
|
+
_addDiscriminatorToObject(schema, deleteMany['filter']);
|
|
284
|
+
|
|
285
|
+
deleteMany['filter'] = _castFilter(model, deleteMany['filter']);
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
module.exports.cast = {
|
|
289
|
+
insertOne: module.exports.castInsertOne,
|
|
290
|
+
updateOne: module.exports.castUpdateOne,
|
|
291
|
+
updateMany: module.exports.castUpdateMany,
|
|
292
|
+
replaceOne: module.exports.castReplaceOne,
|
|
293
|
+
deleteOne: module.exports.castDeleteOne,
|
|
294
|
+
deleteMany: module.exports.castDeleteMany
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
function _castFilter(model, filter, options) {
|
|
298
|
+
try {
|
|
299
|
+
return cast(model.schema, filter, options);
|
|
300
|
+
} catch (err) {
|
|
301
|
+
// Set the model on cast errors so `{MODEL}` gets replaced, like it is for
|
|
302
|
+
// query filter casting (gh-8300)
|
|
303
|
+
if (typeof err.setModel === 'function') {
|
|
304
|
+
err.setModel(model);
|
|
305
|
+
}
|
|
306
|
+
throw err;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
220
310
|
function _addDiscriminatorToObject(schema, obj) {
|
|
221
311
|
if (schema == null) {
|
|
222
312
|
return;
|
|
@@ -233,8 +323,24 @@ function _addDiscriminatorToObject(schema, obj) {
|
|
|
233
323
|
|
|
234
324
|
function decideModelByObject(model, object) {
|
|
235
325
|
const discriminatorKey = model.schema.options.discriminatorKey;
|
|
236
|
-
if (object != null &&
|
|
326
|
+
if (object != null && Object.hasOwn(object, discriminatorKey)) {
|
|
237
327
|
model = getDiscriminatorByValue(model.discriminators, object[discriminatorKey]) || model;
|
|
238
328
|
}
|
|
239
329
|
return model;
|
|
240
330
|
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* gets timestamps option for a given operation. If the option is set within an individual operation, use it. Otherwise, use the global timestamps option configured in the `bulkWrite` options. Overall default is `true`.
|
|
334
|
+
* @api private
|
|
335
|
+
*/
|
|
336
|
+
|
|
337
|
+
function getTimestampsOpt(opCommand, options) {
|
|
338
|
+
const opLevelOpt = opCommand.timestamps;
|
|
339
|
+
const bulkLevelOpt = options.timestamps;
|
|
340
|
+
if (opLevelOpt != null) {
|
|
341
|
+
return opLevelOpt;
|
|
342
|
+
} else if (bulkLevelOpt != null) {
|
|
343
|
+
return bulkLevelOpt;
|
|
344
|
+
}
|
|
345
|
+
return true;
|
|
346
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = function decorateBulkWriteResult(resultOrError, validationErrors, results) {
|
|
4
|
+
resultOrError.mongoose = resultOrError.mongoose || {};
|
|
5
|
+
resultOrError.mongoose.validationErrors = validationErrors;
|
|
6
|
+
resultOrError.mongoose.results = results;
|
|
7
|
+
return resultOrError;
|
|
8
|
+
};
|
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
const Mixed = require('../../schema/mixed');
|
|
4
4
|
const applyBuiltinPlugins = require('../schema/applyBuiltinPlugins');
|
|
5
|
+
const clone = require('../clone');
|
|
5
6
|
const defineKey = require('../document/compile').defineKey;
|
|
6
7
|
const get = require('../get');
|
|
8
|
+
const getOwnProperty = require('../getOwnProperty');
|
|
7
9
|
const utils = require('../../utils');
|
|
8
10
|
const mergeDiscriminatorSchema = require('../../helpers/discriminator/mergeDiscriminatorSchema');
|
|
9
11
|
|
|
@@ -13,17 +15,68 @@ const CUSTOMIZABLE_DISCRIMINATOR_OPTIONS = {
|
|
|
13
15
|
_id: true,
|
|
14
16
|
id: true,
|
|
15
17
|
virtuals: true,
|
|
16
|
-
methods: true
|
|
18
|
+
methods: true,
|
|
19
|
+
statics: true,
|
|
20
|
+
defaultSort: true
|
|
17
21
|
};
|
|
18
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Validate fields declared on the child schema when either schema is configured for encryption. Specifically, this function ensures that:
|
|
25
|
+
*
|
|
26
|
+
* - any encrypted fields are declared on exactly one of the schemas (not both)
|
|
27
|
+
* - encrypted fields cannot be declared on either the parent or child schema, where the other schema declares the same field without encryption.
|
|
28
|
+
*
|
|
29
|
+
* @param {Schema} parentSchema
|
|
30
|
+
* @param {Schema} childSchema
|
|
31
|
+
*/
|
|
32
|
+
function validateDiscriminatorSchemasForEncryption(parentSchema, childSchema) {
|
|
33
|
+
if (parentSchema.encryptionType() == null && childSchema.encryptionType() == null) return;
|
|
34
|
+
|
|
35
|
+
const allSharedNestedPaths = setIntersection(
|
|
36
|
+
allNestedPaths(parentSchema),
|
|
37
|
+
allNestedPaths(childSchema)
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
for (const path of allSharedNestedPaths) {
|
|
41
|
+
if (parentSchema._hasEncryptedField(path) && childSchema._hasEncryptedField(path)) {
|
|
42
|
+
throw new Error(`encrypted fields cannot be declared on both the base schema and the child schema in a discriminator. path=${path}`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (parentSchema._hasEncryptedField(path) || childSchema._hasEncryptedField(path)) {
|
|
46
|
+
throw new Error(`encrypted fields cannot have the same path as a non-encrypted field for discriminators. path=${path}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function allNestedPaths(schema) {
|
|
51
|
+
return [...Object.keys(schema.paths), ...Object.keys(schema.singleNestedPaths)];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @param {Iterable<string>} i1
|
|
56
|
+
* @param {Iterable<string>} i2
|
|
57
|
+
*/
|
|
58
|
+
function* setIntersection(i1, i2) {
|
|
59
|
+
const s1 = new Set(i1);
|
|
60
|
+
for (const item of i2) {
|
|
61
|
+
if (s1.has(item)) {
|
|
62
|
+
yield item;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
19
68
|
/*!
|
|
20
69
|
* ignore
|
|
21
70
|
*/
|
|
22
71
|
|
|
23
|
-
module.exports = function discriminator(model, name, schema, tiedValue, applyPlugins, mergeHooks) {
|
|
24
|
-
if (!
|
|
72
|
+
module.exports = function discriminator(model, name, schema, tiedValue, applyPlugins, mergeHooks, overwriteExisting) {
|
|
73
|
+
if (!schema?.instanceOfSchema) {
|
|
25
74
|
throw new Error('You must pass a valid discriminator Schema');
|
|
26
75
|
}
|
|
76
|
+
// Discriminators are stored in POJOs keyed by name, so `__proto__` would change their prototype
|
|
77
|
+
if (name === '__proto__') {
|
|
78
|
+
throw new Error('Discriminator name "__proto__" is not allowed');
|
|
79
|
+
}
|
|
27
80
|
|
|
28
81
|
mergeHooks = mergeHooks == null ? true : mergeHooks;
|
|
29
82
|
|
|
@@ -79,6 +132,8 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
|
|
|
79
132
|
value = tiedValue;
|
|
80
133
|
}
|
|
81
134
|
|
|
135
|
+
validateDiscriminatorSchemasForEncryption(model.schema, schema);
|
|
136
|
+
|
|
82
137
|
function merge(schema, baseSchema) {
|
|
83
138
|
// Retain original schema before merging base schema
|
|
84
139
|
schema._baseSchema = baseSchema;
|
|
@@ -114,18 +169,19 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
|
|
|
114
169
|
}
|
|
115
170
|
}
|
|
116
171
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}, {})
|
|
123
|
-
});
|
|
172
|
+
// Shallow clone `obj` so we can add additional properties without modifying original
|
|
173
|
+
// schema. `Schema.prototype.clone()` copies `obj` by reference, no cloning.
|
|
174
|
+
schema.obj = { ...schema.obj };
|
|
175
|
+
mergeDiscriminatorSchema(schema, baseSchema);
|
|
176
|
+
schema._gatherChildSchemas();
|
|
124
177
|
|
|
125
178
|
// Clean up conflicting paths _after_ merging re: gh-6076
|
|
126
179
|
for (const conflictingPath of conflictingPaths) {
|
|
127
180
|
delete schema.paths[conflictingPath];
|
|
128
181
|
}
|
|
182
|
+
schema._defaultsCache = null;
|
|
183
|
+
schema._pathsWithTransforms = undefined;
|
|
184
|
+
schema._hasSelectedPaths = undefined;
|
|
129
185
|
|
|
130
186
|
// Rebuild schema models because schemas may have been merged re: #7884
|
|
131
187
|
schema.childSchemas.forEach(obj => {
|
|
@@ -152,7 +208,6 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
|
|
|
152
208
|
if (baseSchema.options.collection) {
|
|
153
209
|
schema.options.collection = baseSchema.options.collection;
|
|
154
210
|
}
|
|
155
|
-
|
|
156
211
|
const toJSON = schema.options.toJSON;
|
|
157
212
|
const toObject = schema.options.toObject;
|
|
158
213
|
const _id = schema.options._id;
|
|
@@ -160,16 +215,13 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
|
|
|
160
215
|
|
|
161
216
|
const keys = Object.keys(schema.options);
|
|
162
217
|
schema.options.discriminatorKey = baseSchema.options.discriminatorKey;
|
|
163
|
-
|
|
218
|
+
const userProvidedOptions = schema._userProvidedOptions;
|
|
164
219
|
for (const _key of keys) {
|
|
165
220
|
if (!CUSTOMIZABLE_DISCRIMINATOR_OPTIONS[_key]) {
|
|
166
|
-
//
|
|
167
|
-
//
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
if (!utils.deepEqual(schema.options[_key], baseSchema.options[_key])) {
|
|
221
|
+
// Use `schema.options` in `deepEqual()` because of `discriminatorKey`
|
|
222
|
+
// set above. We don't allow customizing discriminator key, always
|
|
223
|
+
// overwrite. See gh-9238
|
|
224
|
+
if (_key in userProvidedOptions && !utils.deepEqual(schema.options[_key], baseSchema.options[_key])) {
|
|
173
225
|
throw new Error('Can\'t customize discriminator option ' + _key +
|
|
174
226
|
' (can only modify ' +
|
|
175
227
|
Object.keys(CUSTOMIZABLE_DISCRIMINATOR_OPTIONS).join(', ') +
|
|
@@ -177,7 +229,11 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
|
|
|
177
229
|
}
|
|
178
230
|
}
|
|
179
231
|
}
|
|
180
|
-
schema.options =
|
|
232
|
+
schema.options = clone(baseSchema.options);
|
|
233
|
+
|
|
234
|
+
for (const _key of Object.keys(userProvidedOptions)) {
|
|
235
|
+
schema.options[_key] = userProvidedOptions[_key];
|
|
236
|
+
}
|
|
181
237
|
if (toJSON) schema.options.toJSON = toJSON;
|
|
182
238
|
if (toObject) schema.options.toObject = toObject;
|
|
183
239
|
if (typeof _id !== 'undefined') {
|
|
@@ -210,7 +266,7 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
|
|
|
210
266
|
|
|
211
267
|
model.schema.discriminators[name] = schema;
|
|
212
268
|
|
|
213
|
-
if (model.discriminators
|
|
269
|
+
if (getOwnProperty(model.discriminators, name) && !schema.options.overwriteModels && !overwriteExisting) {
|
|
214
270
|
throw new Error('Discriminator with name "' + name + '" already exists');
|
|
215
271
|
}
|
|
216
272
|
|