@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
|
@@ -3,35 +3,54 @@ const schemaMerge = require('../schema/merge');
|
|
|
3
3
|
const specialProperties = require('../../helpers/specialProperties');
|
|
4
4
|
const isBsonType = require('../../helpers/isBsonType');
|
|
5
5
|
const ObjectId = require('../../types/objectid');
|
|
6
|
+
const SchemaType = require('../../schemaType');
|
|
6
7
|
const isObject = require('../../helpers/isObject');
|
|
7
8
|
/**
|
|
8
9
|
* Merges `from` into `to` without overwriting existing properties.
|
|
9
10
|
*
|
|
10
|
-
* @param {
|
|
11
|
-
* @param {
|
|
12
|
-
* @param {
|
|
11
|
+
* @param {object} to
|
|
12
|
+
* @param {object} from
|
|
13
|
+
* @param {string} [path]
|
|
13
14
|
* @api private
|
|
14
15
|
*/
|
|
15
16
|
|
|
16
|
-
module.exports = function mergeDiscriminatorSchema(to, from, path) {
|
|
17
|
+
module.exports = function mergeDiscriminatorSchema(to, from, path, seen) {
|
|
17
18
|
const keys = Object.keys(from);
|
|
18
19
|
let i = 0;
|
|
19
20
|
const len = keys.length;
|
|
20
21
|
let key;
|
|
21
22
|
|
|
22
23
|
path = path || '';
|
|
24
|
+
seen = seen || new WeakSet();
|
|
25
|
+
|
|
26
|
+
if (seen.has(from)) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
seen.add(from);
|
|
23
30
|
|
|
24
31
|
while (i < len) {
|
|
25
32
|
key = keys[i++];
|
|
26
|
-
if (
|
|
27
|
-
|
|
33
|
+
if (!path) {
|
|
34
|
+
if (key === 'discriminators' ||
|
|
35
|
+
key === 'base' ||
|
|
36
|
+
key === '_applyDiscriminators' ||
|
|
37
|
+
key === '_userProvidedOptions' ||
|
|
38
|
+
key === 'options' ||
|
|
39
|
+
key === 'tree') {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
28
42
|
}
|
|
29
|
-
if (path === 'tree' && from
|
|
43
|
+
if (path === 'tree' && from?.instanceOfSchema) {
|
|
30
44
|
continue;
|
|
31
45
|
}
|
|
32
46
|
if (specialProperties.has(key)) {
|
|
33
47
|
continue;
|
|
34
48
|
}
|
|
49
|
+
if (key === 'parentSchema') {
|
|
50
|
+
// Skip parentSchema to avoid merging parent schema multiple times
|
|
51
|
+
// through child paths' back-references (gh-15966)
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
35
54
|
if (to[key] == null) {
|
|
36
55
|
to[key] = from[key];
|
|
37
56
|
} else if (isObject(from[key])) {
|
|
@@ -53,12 +72,25 @@ module.exports = function mergeDiscriminatorSchema(to, from, path) {
|
|
|
53
72
|
to[key] = from[key].clone();
|
|
54
73
|
}
|
|
55
74
|
continue;
|
|
56
|
-
} else if (isBsonType(from[key], '
|
|
75
|
+
} else if (isBsonType(from[key], 'ObjectId')) {
|
|
57
76
|
to[key] = new ObjectId(from[key]);
|
|
58
77
|
continue;
|
|
78
|
+
} else if (from[key] instanceof SchemaType) {
|
|
79
|
+
if (to[key] == null) {
|
|
80
|
+
to[key] = from[key].clone();
|
|
81
|
+
}
|
|
82
|
+
// For container types with nested schemas, we need to continue to the
|
|
83
|
+
// recursive merge below to properly merge the nested schemas
|
|
84
|
+
if (!from[key].$isMongooseDocumentArray && !from[key].$isSingleNested) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
59
87
|
}
|
|
60
88
|
}
|
|
61
|
-
mergeDiscriminatorSchema(to[key], from[key], path ? path + '.' + key : key);
|
|
89
|
+
mergeDiscriminatorSchema(to[key], from[key], path ? path + '.' + key : key, seen);
|
|
62
90
|
}
|
|
63
91
|
}
|
|
92
|
+
|
|
93
|
+
if (from?.instanceOfSchema) {
|
|
94
|
+
to.tree = Object.assign({}, from.tree, to.tree);
|
|
95
|
+
}
|
|
64
96
|
};
|
|
@@ -2,31 +2,97 @@
|
|
|
2
2
|
|
|
3
3
|
const isNestedProjection = require('../projection/isNestedProjection');
|
|
4
4
|
|
|
5
|
-
module.exports = function applyDefaults(doc, fields, exclude, hasIncludedChildren, isBeforeSetters, pathsToSkip) {
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
module.exports = function applyDefaults(doc, fields, exclude, hasIncludedChildren, isBeforeSetters, pathsToSkip, options) {
|
|
6
|
+
// Only paths with a default can modify the doc, so skip iterating over every path in the schema.
|
|
7
|
+
const { paths: pathsWithDefaults, schemaTypes } = doc.$__schema._getDefaultsCache();
|
|
8
|
+
const numPaths = pathsWithDefaults.length;
|
|
9
|
+
const skipParentChangeTracking = options?.skipParentChangeTracking;
|
|
10
|
+
const skipId = doc.$__.skipId;
|
|
11
|
+
// Whether any path has a function default that must run **after** setters,
|
|
12
|
+
// so callers doing a before-setters pass can skip the after-setters pass
|
|
13
|
+
// entirely when this comes back `false`.
|
|
14
|
+
let hasPostSetterDefaults = false;
|
|
15
|
+
|
|
16
|
+
for (let i = 0; i < numPaths; ++i) {
|
|
17
|
+
const p = pathsWithDefaults[i];
|
|
10
18
|
let def;
|
|
11
19
|
let curPath = '';
|
|
12
|
-
const p = paths[i];
|
|
13
20
|
|
|
14
|
-
if (p === '_id' &&
|
|
21
|
+
if (p === '_id' && skipId) {
|
|
15
22
|
continue;
|
|
16
23
|
}
|
|
17
24
|
|
|
18
|
-
const type =
|
|
25
|
+
const type = schemaTypes[i];
|
|
26
|
+
if (typeof type.defaultValue === 'function' && !type.defaultValue.$runBeforeSetters) {
|
|
27
|
+
hasPostSetterDefaults = true;
|
|
28
|
+
}
|
|
19
29
|
const path = type.splitPath();
|
|
20
30
|
const len = path.length;
|
|
31
|
+
|
|
21
32
|
let included = false;
|
|
22
33
|
let doc_ = doc._doc;
|
|
34
|
+
|
|
35
|
+
if (len === 1) {
|
|
36
|
+
if (exclude === true) {
|
|
37
|
+
if (p in fields) {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
} else if (exclude === false && fields) {
|
|
41
|
+
const hasSubpaths = type.$isSingleNested || type.$isMongooseDocumentArray;
|
|
42
|
+
if ((p in fields && !isNestedProjection(fields[p])) || (hasSubpaths && hasIncludedChildren != null && hasIncludedChildren[p])) {
|
|
43
|
+
included = true;
|
|
44
|
+
} else if (hasIncludedChildren != null && !hasIncludedChildren[p]) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (doc_[p] !== void 0) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (isBeforeSetters != null) {
|
|
54
|
+
if (typeof type.defaultValue === 'function') {
|
|
55
|
+
if (!type.defaultValue.$runBeforeSetters && isBeforeSetters) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (type.defaultValue.$runBeforeSetters && !isBeforeSetters) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
} else if (!isBeforeSetters) {
|
|
62
|
+
// Non-function defaults should always run **before** setters
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (pathsToSkip && pathsToSkip[p]) {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (fields && exclude !== null && !(exclude === true || included)) {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
def = type.getDefault(doc, false);
|
|
77
|
+
} catch (err) {
|
|
78
|
+
doc.invalidate(p, err);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (typeof def !== 'undefined') {
|
|
83
|
+
doc_[p] = def;
|
|
84
|
+
applyChangeTracking(doc, p, skipParentChangeTracking);
|
|
85
|
+
}
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
|
|
23
89
|
for (let j = 0; j < len; ++j) {
|
|
24
90
|
if (doc_ == null) {
|
|
25
91
|
break;
|
|
26
92
|
}
|
|
27
93
|
|
|
28
94
|
const piece = path[j];
|
|
29
|
-
curPath
|
|
95
|
+
curPath = curPath.length ? curPath + '.' + piece : piece;
|
|
30
96
|
|
|
31
97
|
if (exclude === true) {
|
|
32
98
|
if (curPath in fields) {
|
|
@@ -80,7 +146,7 @@ module.exports = function applyDefaults(doc, fields, exclude, hasIncludedChildre
|
|
|
80
146
|
|
|
81
147
|
if (typeof def !== 'undefined') {
|
|
82
148
|
doc_[piece] = def;
|
|
83
|
-
applyChangeTracking(doc, p);
|
|
149
|
+
applyChangeTracking(doc, p, skipParentChangeTracking);
|
|
84
150
|
}
|
|
85
151
|
} else if (included) {
|
|
86
152
|
// selected field
|
|
@@ -93,7 +159,7 @@ module.exports = function applyDefaults(doc, fields, exclude, hasIncludedChildre
|
|
|
93
159
|
|
|
94
160
|
if (typeof def !== 'undefined') {
|
|
95
161
|
doc_[piece] = def;
|
|
96
|
-
applyChangeTracking(doc, p);
|
|
162
|
+
applyChangeTracking(doc, p, skipParentChangeTracking);
|
|
97
163
|
}
|
|
98
164
|
}
|
|
99
165
|
} else {
|
|
@@ -106,7 +172,7 @@ module.exports = function applyDefaults(doc, fields, exclude, hasIncludedChildre
|
|
|
106
172
|
|
|
107
173
|
if (typeof def !== 'undefined') {
|
|
108
174
|
doc_[piece] = def;
|
|
109
|
-
applyChangeTracking(doc, p);
|
|
175
|
+
applyChangeTracking(doc, p, skipParentChangeTracking);
|
|
110
176
|
}
|
|
111
177
|
}
|
|
112
178
|
} else {
|
|
@@ -114,15 +180,17 @@ module.exports = function applyDefaults(doc, fields, exclude, hasIncludedChildre
|
|
|
114
180
|
}
|
|
115
181
|
}
|
|
116
182
|
}
|
|
183
|
+
|
|
184
|
+
return hasPostSetterDefaults;
|
|
117
185
|
};
|
|
118
186
|
|
|
119
187
|
/*!
|
|
120
188
|
* ignore
|
|
121
189
|
*/
|
|
122
190
|
|
|
123
|
-
function applyChangeTracking(doc, fullPath) {
|
|
191
|
+
function applyChangeTracking(doc, fullPath, skipParentChangeTracking) {
|
|
124
192
|
doc.$__.activePaths.default(fullPath);
|
|
125
|
-
if (doc.$isSubdocument && doc.$isSingleNested && doc.$parent() != null) {
|
|
193
|
+
if (!skipParentChangeTracking && doc.$isSubdocument && doc.$isSingleNested && doc.$parent() != null) {
|
|
126
194
|
doc.$parent().$__.activePaths.default(doc.$__pathRelativeToParent(fullPath));
|
|
127
195
|
}
|
|
128
196
|
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const handleTimestampOption = require('../schema/handleTimestampOption');
|
|
4
|
+
const mpath = require('mpath');
|
|
5
|
+
const utils = require('../../utils');
|
|
6
|
+
|
|
7
|
+
module.exports = applyTimestamps;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Apply a given schema's timestamps to the given POJO
|
|
11
|
+
*
|
|
12
|
+
* @param {Schema} schema
|
|
13
|
+
* @param {object} obj
|
|
14
|
+
* @param {object} [options]
|
|
15
|
+
* @param {boolean} [options.isUpdate=false] if true, treat this as an update: just set updatedAt, skip setting createdAt. If false, set both createdAt and updatedAt
|
|
16
|
+
* @param {Function} [options.currentTime] if set, Mongoose will call this function to get the current time.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
function applyTimestamps(schema, obj, options) {
|
|
20
|
+
if (obj == null) {
|
|
21
|
+
return obj;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
applyTimestampsToChildren(schema, obj, options);
|
|
25
|
+
return applyTimestampsToDoc(schema, obj, options);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Apply timestamps to any subdocuments
|
|
30
|
+
*
|
|
31
|
+
* @param {Schema} schema subdocument schema
|
|
32
|
+
* @param {object} res subdocument
|
|
33
|
+
* @param {object} [options]
|
|
34
|
+
* @param {boolean} [options.isUpdate=false] if true, treat this as an update: just set updatedAt, skip setting createdAt. If false, set both createdAt and updatedAt
|
|
35
|
+
* @param {Function} [options.currentTime] if set, Mongoose will call this function to get the current time.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
function applyTimestampsToChildren(schema, res, options) {
|
|
39
|
+
for (const childSchema of schema.childSchemas) {
|
|
40
|
+
const _path = childSchema.model.path;
|
|
41
|
+
const _schema = childSchema.schema;
|
|
42
|
+
if (!_path) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const _obj = mpath.get(_path, res);
|
|
46
|
+
if (_obj == null || (Array.isArray(_obj) && _obj.flat(Infinity).length === 0)) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
applyTimestamps(_schema, _obj, options);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Apply timestamps to a given document. Does not apply timestamps to subdocuments: use `applyTimestampsToChildren` instead
|
|
56
|
+
*
|
|
57
|
+
* @param {Schema} schema
|
|
58
|
+
* @param {object} obj
|
|
59
|
+
* @param {object} [options]
|
|
60
|
+
* @param {boolean} [options.isUpdate=false] if true, treat this as an update: just set updatedAt, skip setting createdAt. If false, set both createdAt and updatedAt
|
|
61
|
+
* @param {Function} [options.currentTime] if set, Mongoose will call this function to get the current time.
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
function applyTimestampsToDoc(schema, obj, options) {
|
|
65
|
+
if (obj == null || typeof obj !== 'object') {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (Array.isArray(obj)) {
|
|
69
|
+
for (const el of obj) {
|
|
70
|
+
applyTimestampsToDoc(schema, el, options);
|
|
71
|
+
}
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (schema.discriminators && utils.hasOwnKeys(schema.discriminators)) {
|
|
76
|
+
for (const discriminatorKey of Object.keys(schema.discriminators)) {
|
|
77
|
+
const discriminator = schema.discriminators[discriminatorKey];
|
|
78
|
+
const key = discriminator.discriminatorMapping.key;
|
|
79
|
+
const value = discriminator.discriminatorMapping.value;
|
|
80
|
+
if (obj[key] == value) {
|
|
81
|
+
schema = discriminator;
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const createdAt = handleTimestampOption(schema.options.timestamps, 'createdAt');
|
|
88
|
+
const updatedAt = handleTimestampOption(schema.options.timestamps, 'updatedAt');
|
|
89
|
+
const currentTime = options?.currentTime;
|
|
90
|
+
|
|
91
|
+
let ts = null;
|
|
92
|
+
if (currentTime != null) {
|
|
93
|
+
ts = currentTime();
|
|
94
|
+
} else if (schema.base?.now) {
|
|
95
|
+
ts = schema.base.now();
|
|
96
|
+
} else {
|
|
97
|
+
ts = new Date();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (createdAt && obj[createdAt] == null && !options?.isUpdate) {
|
|
101
|
+
obj[createdAt] = ts;
|
|
102
|
+
}
|
|
103
|
+
if (updatedAt) {
|
|
104
|
+
obj[updatedAt] = ts;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const mpath = require('mpath');
|
|
4
|
+
const utils = require('../../utils');
|
|
5
|
+
|
|
6
|
+
module.exports = applyVirtuals;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Apply a given schema's virtuals to a given POJO
|
|
10
|
+
*
|
|
11
|
+
* @param {Schema} schema
|
|
12
|
+
* @param {object} obj
|
|
13
|
+
* @param {string[]} [virtuals] optional whitelist of virtuals to apply
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
function applyVirtuals(schema, obj, virtuals) {
|
|
18
|
+
if (obj == null) {
|
|
19
|
+
return obj;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let virtualsForChildren = virtuals;
|
|
23
|
+
let toApply = null;
|
|
24
|
+
|
|
25
|
+
if (Array.isArray(virtuals)) {
|
|
26
|
+
virtualsForChildren = [];
|
|
27
|
+
toApply = [];
|
|
28
|
+
for (const virtual of virtuals) {
|
|
29
|
+
if (virtual.length === 1) {
|
|
30
|
+
toApply.push(virtual[0]);
|
|
31
|
+
} else {
|
|
32
|
+
virtualsForChildren.push(virtual);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
applyVirtualsToChildren(schema, obj, virtualsForChildren);
|
|
38
|
+
return applyVirtualsToDoc(schema, obj, toApply);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Apply virtuals to any subdocuments
|
|
43
|
+
*
|
|
44
|
+
* @param {Schema} schema subdocument schema
|
|
45
|
+
* @param {object} res subdocument
|
|
46
|
+
* @param {string[]} [virtuals] optional whitelist of virtuals to apply
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
function applyVirtualsToChildren(schema, res, virtuals) {
|
|
50
|
+
let attachedVirtuals = false;
|
|
51
|
+
for (const childSchema of schema.childSchemas) {
|
|
52
|
+
const _path = childSchema.model.path;
|
|
53
|
+
const _schema = childSchema.schema;
|
|
54
|
+
if (!_path) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const _obj = mpath.get(_path, res);
|
|
58
|
+
if (_obj == null || (Array.isArray(_obj) && _obj.flat(Infinity).length === 0)) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let virtualsForChild = null;
|
|
63
|
+
if (Array.isArray(virtuals)) {
|
|
64
|
+
virtualsForChild = [];
|
|
65
|
+
for (const virtual of virtuals) {
|
|
66
|
+
if (virtual[0] == _path) {
|
|
67
|
+
virtualsForChild.push(virtual.slice(1));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (virtualsForChild.length === 0) {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
applyVirtuals(_schema, _obj, virtualsForChild);
|
|
77
|
+
attachedVirtuals = true;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (virtuals?.length && !attachedVirtuals) {
|
|
81
|
+
applyVirtualsToDoc(schema, res, virtuals);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Apply virtuals to a given document. Does not apply virtuals to subdocuments: use `applyVirtualsToChildren` instead
|
|
87
|
+
*
|
|
88
|
+
* @param {Schema} schema
|
|
89
|
+
* @param {object} doc
|
|
90
|
+
* @param {string[]} [virtuals] optional whitelist of virtuals to apply
|
|
91
|
+
* @returns
|
|
92
|
+
*/
|
|
93
|
+
|
|
94
|
+
function applyVirtualsToDoc(schema, obj, virtuals) {
|
|
95
|
+
if (obj == null || typeof obj !== 'object') {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (Array.isArray(obj)) {
|
|
99
|
+
for (const el of obj) {
|
|
100
|
+
applyVirtualsToDoc(schema, el, virtuals);
|
|
101
|
+
}
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (schema.discriminators && utils.hasOwnKeys(schema.discriminators)) {
|
|
106
|
+
for (const discriminatorKey of Object.keys(schema.discriminators)) {
|
|
107
|
+
const discriminator = schema.discriminators[discriminatorKey];
|
|
108
|
+
const key = discriminator.discriminatorMapping.key;
|
|
109
|
+
const value = discriminator.discriminatorMapping.value;
|
|
110
|
+
if (obj[key] == value) {
|
|
111
|
+
schema = discriminator;
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (virtuals == null) {
|
|
118
|
+
virtuals = Object.keys(schema.virtuals);
|
|
119
|
+
}
|
|
120
|
+
for (const virtual of virtuals) {
|
|
121
|
+
if (schema.virtuals[virtual] == null) {
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
const virtualType = schema.virtuals[virtual];
|
|
125
|
+
const sp = Array.isArray(virtual)
|
|
126
|
+
? virtual
|
|
127
|
+
: virtual.indexOf('.') === -1
|
|
128
|
+
? [virtual]
|
|
129
|
+
: virtual.split('.');
|
|
130
|
+
let cur = obj;
|
|
131
|
+
for (let i = 0; i < sp.length - 1; ++i) {
|
|
132
|
+
cur[sp[i]] = sp[i] in cur ? cur[sp[i]] : {};
|
|
133
|
+
cur = cur[sp[i]];
|
|
134
|
+
}
|
|
135
|
+
let val = virtualType.applyGetters(cur[sp[sp.length - 1]], obj);
|
|
136
|
+
const isPopulateVirtual =
|
|
137
|
+
virtualType.options?.ref || virtualType.options?.refPath;
|
|
138
|
+
if (isPopulateVirtual && val === undefined) {
|
|
139
|
+
if (virtualType.options.justOne) {
|
|
140
|
+
val = null;
|
|
141
|
+
} else {
|
|
142
|
+
val = [];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
cur[sp[sp.length - 1]] = val;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -16,7 +16,7 @@ module.exports = function cleanModifiedSubpaths(doc, path, options) {
|
|
|
16
16
|
for (const modifiedPath of Object.keys(doc.$__.activePaths.getStatePaths('modify'))) {
|
|
17
17
|
if (skipDocArrays) {
|
|
18
18
|
const schemaType = doc.$__schema.path(modifiedPath);
|
|
19
|
-
if (schemaType
|
|
19
|
+
if (schemaType?.$isMongooseDocumentArray) {
|
|
20
20
|
continue;
|
|
21
21
|
}
|
|
22
22
|
}
|
|
@@ -25,11 +25,21 @@ module.exports = function cleanModifiedSubpaths(doc, path, options) {
|
|
|
25
25
|
++deleted;
|
|
26
26
|
|
|
27
27
|
if (doc.$isSubdocument) {
|
|
28
|
-
|
|
29
|
-
const fullPath = doc.$__fullPath(modifiedPath);
|
|
30
|
-
owner.$__.activePaths.clearPath(fullPath);
|
|
28
|
+
cleanParent(doc, modifiedPath);
|
|
31
29
|
}
|
|
32
30
|
}
|
|
33
31
|
}
|
|
34
32
|
return deleted;
|
|
35
33
|
};
|
|
34
|
+
|
|
35
|
+
function cleanParent(doc, path, seen = new Set()) {
|
|
36
|
+
if (seen.has(doc)) {
|
|
37
|
+
throw new Error('Infinite subdocument loop: subdoc with _id ' + doc._id + ' is a parent of itself');
|
|
38
|
+
}
|
|
39
|
+
const parent = doc.$parent();
|
|
40
|
+
const newPath = doc.$__pathRelativeToParent(void 0, false) + '.' + path;
|
|
41
|
+
parent.$__.activePaths.clearPath(newPath);
|
|
42
|
+
if (parent.$isSubdocument) {
|
|
43
|
+
cleanParent(parent, newPath, seen);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const clone = require('../../helpers/clone');
|
|
3
4
|
const documentSchemaSymbol = require('../../helpers/symbols').documentSchemaSymbol;
|
|
4
5
|
const internalToObjectOptions = require('../../options').internalToObjectOptions;
|
|
6
|
+
const isMongooseDocument = require('../isMongooseDocument');
|
|
5
7
|
const utils = require('../../utils');
|
|
6
8
|
|
|
7
9
|
let Document;
|
|
@@ -30,10 +32,10 @@ const noDottedPathGetOptions = Object.freeze({
|
|
|
30
32
|
|
|
31
33
|
/**
|
|
32
34
|
* Compiles schemas.
|
|
33
|
-
* @param {
|
|
34
|
-
* @param {
|
|
35
|
-
* @param {
|
|
36
|
-
* @param {
|
|
35
|
+
* @param {object} tree
|
|
36
|
+
* @param {any} proto
|
|
37
|
+
* @param {string} prefix
|
|
38
|
+
* @param {object} options
|
|
37
39
|
* @api private
|
|
38
40
|
*/
|
|
39
41
|
|
|
@@ -45,7 +47,7 @@ function compile(tree, proto, prefix, options) {
|
|
|
45
47
|
const limb = tree[key];
|
|
46
48
|
|
|
47
49
|
const hasSubprops = isPOJO(limb) &&
|
|
48
|
-
|
|
50
|
+
utils.hasOwnKeys(limb) &&
|
|
49
51
|
(!limb[typeKey] || (typeKey === 'type' && isPOJO(limb.type) && limb.type.type));
|
|
50
52
|
const subprops = hasSubprops ? limb : null;
|
|
51
53
|
|
|
@@ -55,12 +57,12 @@ function compile(tree, proto, prefix, options) {
|
|
|
55
57
|
|
|
56
58
|
/**
|
|
57
59
|
* Defines the accessor named prop on the incoming prototype.
|
|
58
|
-
* @param {
|
|
59
|
-
* @param {
|
|
60
|
-
* @param {
|
|
61
|
-
* @param {
|
|
62
|
-
* @param {
|
|
63
|
-
* @param {
|
|
60
|
+
* @param {object} options
|
|
61
|
+
* @param {string} options.prop
|
|
62
|
+
* @param {boolean} options.subprops
|
|
63
|
+
* @param {any} options.prototype
|
|
64
|
+
* @param {string} [options.prefix]
|
|
65
|
+
* @param {object} options.options
|
|
64
66
|
* @api private
|
|
65
67
|
*/
|
|
66
68
|
|
|
@@ -75,6 +77,9 @@ function defineKey({ prop, subprops, prototype, prefix, options }) {
|
|
|
75
77
|
enumerable: true,
|
|
76
78
|
configurable: true,
|
|
77
79
|
get: function() {
|
|
80
|
+
if (!this.$__) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
78
83
|
const _this = this;
|
|
79
84
|
if (!this.$__.getters) {
|
|
80
85
|
this.$__.getters = {};
|
|
@@ -115,7 +120,7 @@ function defineKey({ prop, subprops, prototype, prefix, options }) {
|
|
|
115
120
|
configurable: true,
|
|
116
121
|
writable: false,
|
|
117
122
|
value: function() {
|
|
118
|
-
return
|
|
123
|
+
return clone(_this.get(path, null, {
|
|
119
124
|
virtuals: this &&
|
|
120
125
|
this.schema &&
|
|
121
126
|
this.schema.options &&
|
|
@@ -131,7 +136,7 @@ function defineKey({ prop, subprops, prototype, prefix, options }) {
|
|
|
131
136
|
writable: false,
|
|
132
137
|
value: function() {
|
|
133
138
|
return _this.get(path, null, {
|
|
134
|
-
virtuals: this
|
|
139
|
+
virtuals: this?.schema?.options?.toObject?.virtuals || null
|
|
135
140
|
});
|
|
136
141
|
}
|
|
137
142
|
});
|
|
@@ -142,7 +147,7 @@ function defineKey({ prop, subprops, prototype, prefix, options }) {
|
|
|
142
147
|
writable: false,
|
|
143
148
|
value: function() {
|
|
144
149
|
return _this.get(path, null, {
|
|
145
|
-
virtuals: this
|
|
150
|
+
virtuals: this?.schema?.options?.toJSON?.virtuals || null
|
|
146
151
|
});
|
|
147
152
|
}
|
|
148
153
|
});
|
|
@@ -177,7 +182,7 @@ function defineKey({ prop, subprops, prototype, prefix, options }) {
|
|
|
177
182
|
return this.$__.getters[path];
|
|
178
183
|
},
|
|
179
184
|
set: function(v) {
|
|
180
|
-
if (v
|
|
185
|
+
if (isMongooseDocument(v) && v.$__isNested) {
|
|
181
186
|
// Convert top-level to POJO, but leave subdocs hydrated so `$set`
|
|
182
187
|
// can handle them. See gh-9293.
|
|
183
188
|
v = v.$__get();
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Find the deepest subdocument along a given path to ensure setter functions run
|
|
5
|
+
* with the correct subdocument as `this`. If no subdocuments, returns the top-level
|
|
6
|
+
* document.
|
|
7
|
+
*
|
|
8
|
+
* @param {Document} doc
|
|
9
|
+
* @param {string[]} parts
|
|
10
|
+
* @param {Schema} schema
|
|
11
|
+
* @returns {Document}
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
module.exports = function getDeepestSubdocumentForPath(doc, parts, schema) {
|
|
15
|
+
let curPath = parts[0];
|
|
16
|
+
let curSchema = schema;
|
|
17
|
+
let subdoc = doc;
|
|
18
|
+
for (let i = 0; i < parts.length - 1; ++i) {
|
|
19
|
+
const curSchemaType = curSchema.path(curPath);
|
|
20
|
+
if (curSchemaType?.schema) {
|
|
21
|
+
let newSubdoc = subdoc.get(curPath);
|
|
22
|
+
curSchema = curSchemaType.schema;
|
|
23
|
+
curPath = parts[i + 1];
|
|
24
|
+
if (Array.isArray(newSubdoc) && !isNaN(curPath)) {
|
|
25
|
+
newSubdoc = newSubdoc[curPath];
|
|
26
|
+
curPath = '';
|
|
27
|
+
}
|
|
28
|
+
if (newSubdoc == null) {
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
subdoc = newSubdoc;
|
|
32
|
+
} else {
|
|
33
|
+
curPath += curPath.length ? '.' + parts[i + 1] : parts[i + 1];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return subdoc;
|
|
38
|
+
};
|
|
@@ -7,16 +7,19 @@ const getSchemaDiscriminatorByValue = require('../discriminator/getSchemaDiscrim
|
|
|
7
7
|
/**
|
|
8
8
|
* Like `schema.path()`, except with a document, because impossible to
|
|
9
9
|
* determine path type without knowing the embedded discriminator key.
|
|
10
|
+
*
|
|
10
11
|
* @param {Document} doc
|
|
11
|
-
* @param {
|
|
12
|
-
* @param {
|
|
12
|
+
* @param {string|string[]} path
|
|
13
|
+
* @param {object} [options]
|
|
13
14
|
* @api private
|
|
14
15
|
*/
|
|
15
16
|
|
|
16
17
|
module.exports = function getEmbeddedDiscriminatorPath(doc, path, options) {
|
|
17
18
|
options = options || {};
|
|
18
19
|
const typeOnly = options.typeOnly;
|
|
19
|
-
const parts =
|
|
20
|
+
const parts = Array.isArray(path) ?
|
|
21
|
+
path :
|
|
22
|
+
(path.indexOf('.') === -1 ? [path] : path.split('.'));
|
|
20
23
|
let schemaType = null;
|
|
21
24
|
let type = 'adhocOrUndefined';
|
|
22
25
|
|