@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
package/lib/types/index.js
CHANGED
|
@@ -9,12 +9,15 @@ exports.Array = require('./array');
|
|
|
9
9
|
exports.Buffer = require('./buffer');
|
|
10
10
|
|
|
11
11
|
exports.Document = // @deprecate
|
|
12
|
-
exports.Embedded = require('./
|
|
12
|
+
exports.Embedded = require('./arraySubdocument');
|
|
13
13
|
|
|
14
|
-
exports.DocumentArray = require('./
|
|
14
|
+
exports.DocumentArray = require('./documentArray');
|
|
15
|
+
exports.Double = require('./double');
|
|
15
16
|
exports.Decimal128 = require('./decimal128');
|
|
16
17
|
exports.ObjectId = require('./objectid');
|
|
17
18
|
|
|
18
19
|
exports.Map = require('./map');
|
|
19
20
|
|
|
20
21
|
exports.Subdocument = require('./subdocument');
|
|
22
|
+
|
|
23
|
+
exports.UUID = require('./uuid');
|
package/lib/types/map.js
CHANGED
|
@@ -4,11 +4,14 @@ const Mixed = require('../schema/mixed');
|
|
|
4
4
|
const MongooseError = require('../error/mongooseError');
|
|
5
5
|
const clone = require('../helpers/clone');
|
|
6
6
|
const deepEqual = require('../utils').deepEqual;
|
|
7
|
+
const isMongooseArray = require('../utils').isMongooseArray;
|
|
7
8
|
const getConstructorName = require('../helpers/getConstructorName');
|
|
8
9
|
const handleSpreadDoc = require('../helpers/document/handleSpreadDoc');
|
|
10
|
+
const isMongooseDocument = require('../helpers/isMongooseDocument');
|
|
9
11
|
const util = require('util');
|
|
10
12
|
const specialProperties = require('../helpers/specialProperties');
|
|
11
13
|
const isBsonType = require('../helpers/isBsonType');
|
|
14
|
+
const cleanModifiedSubpaths = require('../helpers/document/cleanModifiedSubpaths');
|
|
12
15
|
|
|
13
16
|
const populateModelSymbol = require('../helpers/symbols').populateModelSymbol;
|
|
14
17
|
|
|
@@ -19,10 +22,10 @@ const populateModelSymbol = require('../helpers/symbols').populateModelSymbol;
|
|
|
19
22
|
class MongooseMap extends Map {
|
|
20
23
|
constructor(v, path, doc, schemaType) {
|
|
21
24
|
if (getConstructorName(v) === 'Object') {
|
|
22
|
-
v = Object.keys(v).
|
|
25
|
+
v = Object.keys(v).map(key => [key, v[key]]);
|
|
23
26
|
}
|
|
24
27
|
super(v);
|
|
25
|
-
this.$__parent = doc
|
|
28
|
+
this.$__parent = doc?.$__ != null ? doc : null;
|
|
26
29
|
this.$__path = path;
|
|
27
30
|
this.$__schemaType = schemaType == null ? new Mixed(path) : schemaType;
|
|
28
31
|
|
|
@@ -34,7 +37,7 @@ class MongooseMap extends Map {
|
|
|
34
37
|
|
|
35
38
|
super.set(key, value);
|
|
36
39
|
|
|
37
|
-
if (value
|
|
40
|
+
if (value?.$isSingleNested) {
|
|
38
41
|
value.$basePath = this.$__path + '.' + key;
|
|
39
42
|
}
|
|
40
43
|
}
|
|
@@ -52,7 +55,7 @@ class MongooseMap extends Map {
|
|
|
52
55
|
*/
|
|
53
56
|
|
|
54
57
|
get(key, options) {
|
|
55
|
-
if (isBsonType(key, '
|
|
58
|
+
if (isBsonType(key, 'ObjectId')) {
|
|
56
59
|
key = key.toString();
|
|
57
60
|
}
|
|
58
61
|
|
|
@@ -63,15 +66,38 @@ class MongooseMap extends Map {
|
|
|
63
66
|
return this.$__schemaType.applyGetters(super.get(key), this.$__parent);
|
|
64
67
|
}
|
|
65
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Overwrites native Map's `has()` function to support ObjectIds as keys.
|
|
71
|
+
*
|
|
72
|
+
* @api public
|
|
73
|
+
* @method has
|
|
74
|
+
* @memberOf Map
|
|
75
|
+
*/
|
|
76
|
+
|
|
77
|
+
has(key) {
|
|
78
|
+
if (isBsonType(key, 'ObjectId')) {
|
|
79
|
+
key = key.toString();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return super.has(key);
|
|
83
|
+
}
|
|
84
|
+
|
|
66
85
|
/**
|
|
67
86
|
* Overwrites native Map's `set()` function to support setters, `populate()`,
|
|
68
87
|
* and change tracking. Note that Mongoose maps _only_ support strings and
|
|
69
88
|
* ObjectIds as keys.
|
|
70
89
|
*
|
|
90
|
+
* Keys also cannot:
|
|
91
|
+
* - be named after special properties `prototype`, `constructor`, and `__proto__`
|
|
92
|
+
* - start with a dollar sign (`$`)
|
|
93
|
+
* - contain any dots (`.`)
|
|
94
|
+
*
|
|
71
95
|
* #### Example:
|
|
72
96
|
*
|
|
73
97
|
* doc.myMap.set('test', 42); // works
|
|
74
98
|
* doc.myMap.set({ obj: 42 }, 42); // Throws "Mongoose maps only support string keys"
|
|
99
|
+
* doc.myMap.set(10, 42); // Throws "Mongoose maps only support string keys"
|
|
100
|
+
* doc.myMap.set("$test", 42); // Throws "Mongoose maps do not support keys that start with "$", got "$test""
|
|
75
101
|
*
|
|
76
102
|
* @api public
|
|
77
103
|
* @method set
|
|
@@ -79,7 +105,7 @@ class MongooseMap extends Map {
|
|
|
79
105
|
*/
|
|
80
106
|
|
|
81
107
|
set(key, value) {
|
|
82
|
-
if (isBsonType(key, '
|
|
108
|
+
if (isBsonType(key, 'ObjectId')) {
|
|
83
109
|
key = key.toString();
|
|
84
110
|
}
|
|
85
111
|
|
|
@@ -98,7 +124,7 @@ class MongooseMap extends Map {
|
|
|
98
124
|
|
|
99
125
|
let _fullPath;
|
|
100
126
|
const parent = this.$__parent;
|
|
101
|
-
const populated = parent
|
|
127
|
+
const populated = parent?.$__?.populated ?
|
|
102
128
|
parent.$populated(fullPath.call(this), true) || parent.$populated(this.$__path, true) :
|
|
103
129
|
null;
|
|
104
130
|
const priorVal = this.get(key);
|
|
@@ -112,23 +138,23 @@ class MongooseMap extends Map {
|
|
|
112
138
|
}
|
|
113
139
|
if (Array.isArray(value) && this.$__schemaType.$isMongooseArray) {
|
|
114
140
|
value = value.map(v => {
|
|
115
|
-
if (v
|
|
141
|
+
if (!isMongooseDocument(v)) {
|
|
116
142
|
v = new populated.options[populateModelSymbol](v);
|
|
117
143
|
}
|
|
118
144
|
// Doesn't support single nested "in-place" populate
|
|
119
|
-
v.$__.wasPopulated = { value: v._id };
|
|
145
|
+
v.$__.wasPopulated = { value: v._doc._id };
|
|
120
146
|
return v;
|
|
121
147
|
});
|
|
122
|
-
} else {
|
|
123
|
-
if (value
|
|
148
|
+
} else if (value != null) {
|
|
149
|
+
if (!isMongooseDocument(value)) {
|
|
124
150
|
value = new populated.options[populateModelSymbol](value);
|
|
125
151
|
}
|
|
126
152
|
// Doesn't support single nested "in-place" populate
|
|
127
|
-
value.$__.wasPopulated = { value: value._id };
|
|
153
|
+
value.$__.wasPopulated = { value: value._doc._id };
|
|
128
154
|
}
|
|
129
155
|
} else {
|
|
130
156
|
try {
|
|
131
|
-
const options = this.$__schemaType.$isMongooseDocumentArray || this.$__schemaType.$isSingleNested ?
|
|
157
|
+
const options = this.$__schemaType.$isMongooseDocumentArray || this.$__schemaType.$isSingleNested || this.$__schemaType.$isMongooseArray || this.$__schemaType.$isSchemaMap ?
|
|
132
158
|
{ path: fullPath.call(this) } :
|
|
133
159
|
null;
|
|
134
160
|
value = this.$__schemaType.applySetters(
|
|
@@ -139,7 +165,7 @@ class MongooseMap extends Map {
|
|
|
139
165
|
options
|
|
140
166
|
);
|
|
141
167
|
} catch (error) {
|
|
142
|
-
if (this.$__parent
|
|
168
|
+
if (this.$__parent?.$__ != null) {
|
|
143
169
|
this.$__parent.invalidate(fullPath.call(this), error);
|
|
144
170
|
return;
|
|
145
171
|
}
|
|
@@ -149,8 +175,19 @@ class MongooseMap extends Map {
|
|
|
149
175
|
|
|
150
176
|
super.set(key, value);
|
|
151
177
|
|
|
152
|
-
if (parent
|
|
153
|
-
|
|
178
|
+
if (parent?.$__ != null && !deepEqual(value, priorVal)) {
|
|
179
|
+
const path = fullPath.call(this);
|
|
180
|
+
parent.markModified(path);
|
|
181
|
+
// Replacing the whole array means later push(), pull() or addToSet() calls must not be sent as
|
|
182
|
+
// atomics against the array that was in the database, same as Document#$set re: gh-1126
|
|
183
|
+
if (isMongooseArray(value)) {
|
|
184
|
+
value._registerAtomic('$set', value);
|
|
185
|
+
}
|
|
186
|
+
// If overwriting the full document array or subdoc, make sure to clean up any paths that were modified
|
|
187
|
+
// before re: #15108
|
|
188
|
+
if (this.$__schemaType.$isMongooseDocumentArray || this.$__schemaType.$isSingleNested) {
|
|
189
|
+
cleanModifiedSubpaths(parent, path);
|
|
190
|
+
}
|
|
154
191
|
}
|
|
155
192
|
|
|
156
193
|
// Delay calculating full path unless absolutely necessary, because string
|
|
@@ -189,7 +226,7 @@ class MongooseMap extends Map {
|
|
|
189
226
|
*/
|
|
190
227
|
|
|
191
228
|
delete(key) {
|
|
192
|
-
if (isBsonType(key, '
|
|
229
|
+
if (isBsonType(key, 'ObjectId')) {
|
|
193
230
|
key = key.toString();
|
|
194
231
|
}
|
|
195
232
|
|
|
@@ -210,7 +247,7 @@ class MongooseMap extends Map {
|
|
|
210
247
|
}
|
|
211
248
|
|
|
212
249
|
toObject(options) {
|
|
213
|
-
if (options
|
|
250
|
+
if (options?.flattenMaps) {
|
|
214
251
|
const ret = {};
|
|
215
252
|
const keys = this.keys();
|
|
216
253
|
for (const key of keys) {
|
|
@@ -227,23 +264,23 @@ class MongooseMap extends Map {
|
|
|
227
264
|
}
|
|
228
265
|
|
|
229
266
|
/**
|
|
230
|
-
* Converts this map to a
|
|
231
|
-
*
|
|
267
|
+
* Converts this map to a POJO for `JSON.stringify()`. Set the `flattenMaps`
|
|
268
|
+
* option to `false` to convert this map to a native JavaScript Map instead.
|
|
232
269
|
*
|
|
233
270
|
* #### Example:
|
|
234
271
|
*
|
|
235
|
-
* doc.myMap.toJSON() instanceof Map; //
|
|
236
|
-
* doc.myMap.toJSON({ flattenMaps:
|
|
272
|
+
* doc.myMap.toJSON() instanceof Map; // false
|
|
273
|
+
* doc.myMap.toJSON({ flattenMaps: false }) instanceof Map; // true
|
|
237
274
|
*
|
|
238
275
|
* @api public
|
|
239
276
|
* @method toJSON
|
|
240
|
-
* @param {
|
|
241
|
-
* @param {
|
|
277
|
+
* @param {object} [options]
|
|
278
|
+
* @param {boolean} [options.flattenMaps=true] set to `false` to convert the map to a native JavaScript Map rather than a POJO
|
|
242
279
|
* @memberOf Map
|
|
243
280
|
*/
|
|
244
281
|
|
|
245
282
|
toJSON(options) {
|
|
246
|
-
if (typeof
|
|
283
|
+
if (typeof options?.flattenMaps === 'boolean' ? options.flattenMaps : true) {
|
|
247
284
|
const ret = {};
|
|
248
285
|
const keys = this.keys();
|
|
249
286
|
for (const key of keys) {
|
|
@@ -331,7 +368,7 @@ Object.defineProperty(MongooseMap.prototype, '$__deferredCalls', {
|
|
|
331
368
|
/**
|
|
332
369
|
* Since maps are stored as objects under the hood, keys must be strings
|
|
333
370
|
* and can't contain any invalid characters
|
|
334
|
-
* @param {
|
|
371
|
+
* @param {string} key
|
|
335
372
|
* @api private
|
|
336
373
|
*/
|
|
337
374
|
|
package/lib/types/objectid.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
12
|
|
|
13
|
-
const ObjectId = require('
|
|
13
|
+
const ObjectId = require('mongodb/lib/bson').ObjectId;
|
|
14
14
|
const objectIdSymbol = require('../helpers/symbols').objectIdSymbol;
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -30,7 +30,7 @@ Object.defineProperty(ObjectId.prototype, '_id', {
|
|
|
30
30
|
* Convenience `valueOf()` to allow comparing ObjectIds using double equals re: gh-7299
|
|
31
31
|
*/
|
|
32
32
|
|
|
33
|
-
if (!ObjectId.prototype
|
|
33
|
+
if (!Object.hasOwn(ObjectId.prototype, 'valueOf')) {
|
|
34
34
|
ObjectId.prototype.valueOf = function objectIdValueOf() {
|
|
35
35
|
return this.toString();
|
|
36
36
|
};
|
package/lib/types/subdocument.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const Document = require('../document');
|
|
4
|
-
const immediate = require('../helpers/immediate');
|
|
5
4
|
const internalToObjectOptions = require('../options').internalToObjectOptions;
|
|
6
|
-
const promiseOrCallback = require('../helpers/promiseOrCallback');
|
|
7
5
|
const util = require('util');
|
|
8
6
|
const utils = require('../utils');
|
|
9
7
|
|
|
@@ -16,7 +14,7 @@ module.exports = Subdocument;
|
|
|
16
14
|
* @api private
|
|
17
15
|
*/
|
|
18
16
|
|
|
19
|
-
function Subdocument(value, fields, parent,
|
|
17
|
+
function Subdocument(value, fields, parent, options) {
|
|
20
18
|
if (parent != null) {
|
|
21
19
|
// If setting a nested path, should copy isNew from parent re: gh-7048
|
|
22
20
|
const parentOptions = { isNew: parent.isNew };
|
|
@@ -25,12 +23,16 @@ function Subdocument(value, fields, parent, skipId, options) {
|
|
|
25
23
|
}
|
|
26
24
|
options = Object.assign(parentOptions, options);
|
|
27
25
|
}
|
|
28
|
-
if (options
|
|
26
|
+
if (options?.path != null) {
|
|
29
27
|
this.$basePath = options.path;
|
|
30
28
|
}
|
|
31
|
-
Document.call(this, value, fields,
|
|
29
|
+
Document.call(this, value, fields, options);
|
|
32
30
|
|
|
33
|
-
|
|
31
|
+
// `priorDoc` is only needed during construction, so release the reference.
|
|
32
|
+
// Set to undefined rather than `delete` to keep `$__` out of dictionary mode.
|
|
33
|
+
if (this.$__.priorDoc !== undefined) {
|
|
34
|
+
this.$__.priorDoc = undefined;
|
|
35
|
+
}
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
Subdocument.prototype = Object.create(Document.prototype);
|
|
@@ -62,16 +64,16 @@ Subdocument.prototype.toBSON = function() {
|
|
|
62
64
|
*
|
|
63
65
|
* _This is a no-op. Does not actually save the doc to the db._
|
|
64
66
|
*
|
|
65
|
-
* @param {
|
|
67
|
+
* @param {object} [options]
|
|
68
|
+
* @param {boolean} [options.suppressWarning=false] If `true`, suppress the warning about calling `save()` on a subdoc.
|
|
69
|
+
* @param {boolean|object} [options.middleware=true] set to `false` to skip all user-defined middleware
|
|
70
|
+
* @param {boolean} [options.middleware.pre=true] set to `false` to skip only pre hooks
|
|
71
|
+
* @param {boolean} [options.middleware.post=true] set to `false` to skip only post hooks
|
|
66
72
|
* @return {Promise} resolved Promise
|
|
67
73
|
* @api private
|
|
68
74
|
*/
|
|
69
75
|
|
|
70
|
-
Subdocument.prototype.save = function(options
|
|
71
|
-
if (typeof options === 'function') {
|
|
72
|
-
fn = options;
|
|
73
|
-
options = {};
|
|
74
|
-
}
|
|
76
|
+
Subdocument.prototype.save = async function save(options) {
|
|
75
77
|
options = options || {};
|
|
76
78
|
|
|
77
79
|
if (!options.suppressWarning) {
|
|
@@ -81,19 +83,17 @@ Subdocument.prototype.save = function(options, fn) {
|
|
|
81
83
|
'if you\'re sure this behavior is right for your app.');
|
|
82
84
|
}
|
|
83
85
|
|
|
84
|
-
return
|
|
85
|
-
this.$__save(cb);
|
|
86
|
-
});
|
|
86
|
+
return await this.$__save(options);
|
|
87
87
|
};
|
|
88
88
|
|
|
89
89
|
/**
|
|
90
90
|
* Given a path relative to this document, return the path relative
|
|
91
91
|
* to the top-level document.
|
|
92
|
-
* @param {
|
|
92
|
+
* @param {string} path
|
|
93
93
|
* @method $__fullPath
|
|
94
94
|
* @memberOf Subdocument
|
|
95
95
|
* @instance
|
|
96
|
-
* @returns {
|
|
96
|
+
* @returns {string}
|
|
97
97
|
* @api private
|
|
98
98
|
*/
|
|
99
99
|
|
|
@@ -109,9 +109,9 @@ Subdocument.prototype.$__fullPath = function(path) {
|
|
|
109
109
|
|
|
110
110
|
/**
|
|
111
111
|
* Given a path relative to this document, return the path relative
|
|
112
|
-
* to the
|
|
113
|
-
* @param {
|
|
114
|
-
* @returns {
|
|
112
|
+
* to the parent document.
|
|
113
|
+
* @param {string} p
|
|
114
|
+
* @returns {string}
|
|
115
115
|
* @method $__pathRelativeToParent
|
|
116
116
|
* @memberOf Subdocument
|
|
117
117
|
* @instance
|
|
@@ -122,6 +122,9 @@ Subdocument.prototype.$__pathRelativeToParent = function(p) {
|
|
|
122
122
|
if (p == null) {
|
|
123
123
|
return this.$basePath;
|
|
124
124
|
}
|
|
125
|
+
if (!this.$basePath) {
|
|
126
|
+
return p;
|
|
127
|
+
}
|
|
125
128
|
return [this.$basePath, p].join('.');
|
|
126
129
|
};
|
|
127
130
|
|
|
@@ -137,8 +140,15 @@ Subdocument.prototype.$__pathRelativeToParent = function(p) {
|
|
|
137
140
|
* @api private
|
|
138
141
|
*/
|
|
139
142
|
|
|
140
|
-
Subdocument.prototype.$__save = function(
|
|
141
|
-
|
|
143
|
+
Subdocument.prototype.$__save = async function $__save(options) {
|
|
144
|
+
try {
|
|
145
|
+
await this._execDocumentPreHooks('save', options, [options]);
|
|
146
|
+
} catch (error) {
|
|
147
|
+
await this._execDocumentPostHooks('save', options, error);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
await this._execDocumentPostHooks('save', options);
|
|
142
152
|
};
|
|
143
153
|
|
|
144
154
|
/*!
|
|
@@ -167,8 +177,12 @@ Subdocument.prototype.markModified = function(path) {
|
|
|
167
177
|
return;
|
|
168
178
|
}
|
|
169
179
|
|
|
180
|
+
if (this.isNew) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
|
|
170
184
|
const myPath = this.$__pathRelativeToParent().replace(/\.$/, '');
|
|
171
|
-
if (parent.isDirectModified(myPath)
|
|
185
|
+
if (parent.isDirectModified(myPath)) {
|
|
172
186
|
return;
|
|
173
187
|
}
|
|
174
188
|
this.$__parent.markModified(fullPath, this);
|
|
@@ -197,7 +211,7 @@ Subdocument.prototype.isModified = function(paths, options, modifiedPaths) {
|
|
|
197
211
|
/**
|
|
198
212
|
* Marks a path as valid, removing existing validation errors.
|
|
199
213
|
*
|
|
200
|
-
* @param {
|
|
214
|
+
* @param {string} path the field to mark as valid
|
|
201
215
|
* @api private
|
|
202
216
|
* @method $markValid
|
|
203
217
|
* @memberOf Subdocument
|
|
@@ -254,7 +268,7 @@ Subdocument.prototype.ownerDocument = function() {
|
|
|
254
268
|
return this.$__.ownerDocument;
|
|
255
269
|
}
|
|
256
270
|
|
|
257
|
-
let parent = this;
|
|
271
|
+
let parent = this;
|
|
258
272
|
const paths = [];
|
|
259
273
|
const seenDocs = new Set([parent]);
|
|
260
274
|
|
|
@@ -286,7 +300,7 @@ Subdocument.prototype.ownerDocument = function() {
|
|
|
286
300
|
*/
|
|
287
301
|
|
|
288
302
|
Subdocument.prototype.$__fullPathWithIndexes = function() {
|
|
289
|
-
let parent = this;
|
|
303
|
+
let parent = this;
|
|
290
304
|
const paths = [];
|
|
291
305
|
const seenDocs = new Set([parent]);
|
|
292
306
|
|
|
@@ -329,20 +343,14 @@ Subdocument.prototype.parent = function() {
|
|
|
329
343
|
|
|
330
344
|
Subdocument.prototype.$parent = Subdocument.prototype.parent;
|
|
331
345
|
|
|
332
|
-
|
|
333
|
-
*
|
|
334
|
-
*
|
|
335
|
-
* @method $__remove
|
|
336
|
-
* @memberOf Subdocument
|
|
337
|
-
* @instance
|
|
346
|
+
/*!
|
|
347
|
+
* Sets this sub-documents parent document.
|
|
348
|
+
*
|
|
338
349
|
* @api private
|
|
339
350
|
*/
|
|
340
351
|
|
|
341
|
-
Subdocument.prototype.$
|
|
342
|
-
|
|
343
|
-
return;
|
|
344
|
-
}
|
|
345
|
-
return cb(null, this);
|
|
352
|
+
Subdocument.prototype.$__setParent = function $__setParent(parent) {
|
|
353
|
+
this.$__parent = parent;
|
|
346
354
|
};
|
|
347
355
|
|
|
348
356
|
/**
|
|
@@ -360,23 +368,20 @@ Subdocument.prototype.$__removeFromParent = function() {
|
|
|
360
368
|
/**
|
|
361
369
|
* Null-out this subdoc
|
|
362
370
|
*
|
|
363
|
-
* @param {
|
|
364
|
-
* @param {Function} [callback] optional callback for compatibility with Document.prototype.remove
|
|
371
|
+
* @param {object} [options]
|
|
365
372
|
*/
|
|
366
373
|
|
|
367
|
-
Subdocument.prototype.
|
|
368
|
-
if (typeof options === 'function') {
|
|
369
|
-
callback = options;
|
|
370
|
-
options = null;
|
|
371
|
-
}
|
|
374
|
+
Subdocument.prototype.deleteOne = function deleteOne(options) {
|
|
372
375
|
registerRemoveListener(this);
|
|
373
376
|
|
|
374
377
|
// If removing entire doc, no need to remove subdoc
|
|
375
|
-
if (!options
|
|
378
|
+
if (!options?.noop) {
|
|
376
379
|
this.$__removeFromParent();
|
|
377
|
-
}
|
|
378
380
|
|
|
379
|
-
|
|
381
|
+
const owner = this.ownerDocument();
|
|
382
|
+
owner.$__.removedSubdocs = owner.$__.removedSubdocs || [];
|
|
383
|
+
owner.$__.removedSubdocs.push(this);
|
|
384
|
+
}
|
|
380
385
|
};
|
|
381
386
|
|
|
382
387
|
/*!
|
|
@@ -396,11 +401,7 @@ Subdocument.prototype.populate = function() {
|
|
|
396
401
|
*/
|
|
397
402
|
|
|
398
403
|
Subdocument.prototype.inspect = function() {
|
|
399
|
-
return this.toObject(
|
|
400
|
-
transform: false,
|
|
401
|
-
virtuals: false,
|
|
402
|
-
flattenDecimals: false
|
|
403
|
-
});
|
|
404
|
+
return this.toObject();
|
|
404
405
|
};
|
|
405
406
|
|
|
406
407
|
if (util.inspect.custom) {
|
|
@@ -408,6 +409,27 @@ if (util.inspect.custom) {
|
|
|
408
409
|
Subdocument.prototype[util.inspect.custom] = Subdocument.prototype.inspect;
|
|
409
410
|
}
|
|
410
411
|
|
|
412
|
+
/**
|
|
413
|
+
* Override `$toObject()` to handle minimizing the whole path. Should not minimize if schematype-level minimize
|
|
414
|
+
* is set to false re: gh-11247, gh-14058, gh-14151. Should not minimize document array elements: an array
|
|
415
|
+
* element cannot be removed without shifting the array, so minimizing it to `undefined` makes the BSON
|
|
416
|
+
* serializer store `null` re: gh-7322.
|
|
417
|
+
*/
|
|
418
|
+
|
|
419
|
+
Subdocument.prototype.$toObject = function $toObject(options, json) {
|
|
420
|
+
const ret = Document.prototype.$toObject.call(this, options, json);
|
|
421
|
+
|
|
422
|
+
// If `$toObject()` was called recursively, respect the minimize option, including schematype level minimize.
|
|
423
|
+
// If minimize is set, then we can minimize out the whole object.
|
|
424
|
+
if (utils.hasOwnKeys(ret) === false && options?._calledWithOptions != null) {
|
|
425
|
+
const minimize = options._calledWithOptions?.minimize ?? this?.$__schemaTypeOptions?.minimize ?? options.minimize;
|
|
426
|
+
if (minimize && !this.constructor.$__required && !this.$isDocumentArrayElement) {
|
|
427
|
+
return undefined;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return ret;
|
|
431
|
+
};
|
|
432
|
+
|
|
411
433
|
/**
|
|
412
434
|
* Registers remove event listeners for triggering
|
|
413
435
|
* on subdocuments.
|
|
@@ -417,16 +439,15 @@ if (util.inspect.custom) {
|
|
|
417
439
|
*/
|
|
418
440
|
|
|
419
441
|
function registerRemoveListener(sub) {
|
|
420
|
-
|
|
442
|
+
const owner = sub.ownerDocument();
|
|
421
443
|
|
|
422
444
|
function emitRemove() {
|
|
423
445
|
owner.$removeListener('save', emitRemove);
|
|
424
|
-
owner.$removeListener('
|
|
425
|
-
sub.emit('
|
|
426
|
-
sub.constructor.emit('
|
|
427
|
-
owner = sub = null;
|
|
446
|
+
owner.$removeListener('deleteOne', emitRemove);
|
|
447
|
+
sub.emit('deleteOne', sub);
|
|
448
|
+
sub.constructor.emit('deleteOne', sub);
|
|
428
449
|
}
|
|
429
450
|
|
|
430
451
|
owner.$on('save', emitRemove);
|
|
431
|
-
owner.$on('
|
|
452
|
+
owner.$on('deleteOne', emitRemove);
|
|
432
453
|
}
|