@depup/mongoose 9.1.3-depup.0
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/LICENSE.md +22 -0
- package/README.md +397 -0
- package/SECURITY.md +1 -0
- package/eslint.config.mjs +198 -0
- package/index.js +64 -0
- package/lib/aggregate.js +1189 -0
- package/lib/cast/bigint.js +46 -0
- package/lib/cast/boolean.js +32 -0
- package/lib/cast/date.js +41 -0
- package/lib/cast/decimal128.js +39 -0
- package/lib/cast/double.js +50 -0
- package/lib/cast/int32.js +36 -0
- package/lib/cast/number.js +42 -0
- package/lib/cast/objectid.js +29 -0
- package/lib/cast/string.js +37 -0
- package/lib/cast/uuid.js +35 -0
- package/lib/cast.js +436 -0
- package/lib/collection.js +321 -0
- package/lib/connection.js +1855 -0
- package/lib/connectionState.js +26 -0
- package/lib/constants.js +73 -0
- package/lib/cursor/aggregationCursor.js +466 -0
- package/lib/cursor/changeStream.js +198 -0
- package/lib/cursor/queryCursor.js +622 -0
- package/lib/document.js +5521 -0
- package/lib/driver.js +15 -0
- package/lib/drivers/SPEC.md +4 -0
- package/lib/drivers/node-mongodb-native/bulkWriteResult.js +5 -0
- package/lib/drivers/node-mongodb-native/collection.js +393 -0
- package/lib/drivers/node-mongodb-native/connection.js +506 -0
- package/lib/drivers/node-mongodb-native/index.js +10 -0
- package/lib/error/browserMissingSchema.js +29 -0
- package/lib/error/bulkSaveIncompleteError.js +44 -0
- package/lib/error/bulkWriteError.js +41 -0
- package/lib/error/cast.js +158 -0
- package/lib/error/createCollectionsError.js +26 -0
- package/lib/error/divergentArray.js +40 -0
- package/lib/error/eachAsyncMultiError.js +41 -0
- package/lib/error/index.js +237 -0
- package/lib/error/invalidSchemaOption.js +32 -0
- package/lib/error/messages.js +47 -0
- package/lib/error/missingSchema.js +33 -0
- package/lib/error/mongooseError.js +13 -0
- package/lib/error/notFound.js +47 -0
- package/lib/error/objectExpected.js +31 -0
- package/lib/error/objectParameter.js +31 -0
- package/lib/error/overwriteModel.js +31 -0
- package/lib/error/parallelSave.js +33 -0
- package/lib/error/parallelValidate.js +33 -0
- package/lib/error/serverSelection.js +62 -0
- package/lib/error/setOptionError.js +103 -0
- package/lib/error/strict.js +35 -0
- package/lib/error/strictPopulate.js +31 -0
- package/lib/error/syncIndexes.js +30 -0
- package/lib/error/validation.js +97 -0
- package/lib/error/validator.js +100 -0
- package/lib/error/version.js +38 -0
- package/lib/helpers/aggregate/prepareDiscriminatorPipeline.js +39 -0
- package/lib/helpers/aggregate/stringifyFunctionOperators.js +50 -0
- package/lib/helpers/arrayDepth.js +33 -0
- package/lib/helpers/clone.js +204 -0
- package/lib/helpers/common.js +127 -0
- package/lib/helpers/createJSONSchemaTypeDefinition.js +24 -0
- package/lib/helpers/cursor/eachAsync.js +225 -0
- package/lib/helpers/discriminator/applyEmbeddedDiscriminators.js +36 -0
- package/lib/helpers/discriminator/areDiscriminatorValuesEqual.js +16 -0
- package/lib/helpers/discriminator/checkEmbeddedDiscriminatorKeyProjection.js +12 -0
- package/lib/helpers/discriminator/getConstructor.js +29 -0
- package/lib/helpers/discriminator/getDiscriminatorByValue.js +28 -0
- package/lib/helpers/discriminator/getSchemaDiscriminatorByValue.js +27 -0
- package/lib/helpers/discriminator/mergeDiscriminatorSchema.js +91 -0
- package/lib/helpers/document/applyDefaults.js +132 -0
- package/lib/helpers/document/applyTimestamps.js +106 -0
- package/lib/helpers/document/applyVirtuals.js +147 -0
- package/lib/helpers/document/cleanModifiedSubpaths.js +45 -0
- package/lib/helpers/document/compile.js +238 -0
- package/lib/helpers/document/getDeepestSubdocumentForPath.js +38 -0
- package/lib/helpers/document/getEmbeddedDiscriminatorPath.js +53 -0
- package/lib/helpers/document/handleSpreadDoc.js +35 -0
- package/lib/helpers/each.js +25 -0
- package/lib/helpers/error/combinePathErrors.js +22 -0
- package/lib/helpers/firstKey.js +8 -0
- package/lib/helpers/get.js +65 -0
- package/lib/helpers/getConstructorName.js +16 -0
- package/lib/helpers/getDefaultBulkwriteResult.js +18 -0
- package/lib/helpers/getFunctionName.js +10 -0
- package/lib/helpers/immediate.js +16 -0
- package/lib/helpers/indexes/applySchemaCollation.js +13 -0
- package/lib/helpers/indexes/decorateDiscriminatorIndexOptions.js +14 -0
- package/lib/helpers/indexes/getRelatedIndexes.js +63 -0
- package/lib/helpers/indexes/isDefaultIdIndex.js +18 -0
- package/lib/helpers/indexes/isIndexEqual.js +95 -0
- package/lib/helpers/indexes/isIndexSpecEqual.js +32 -0
- package/lib/helpers/indexes/isTextIndex.js +16 -0
- package/lib/helpers/indexes/isTimeseriesIndex.js +16 -0
- package/lib/helpers/isAsyncFunction.js +9 -0
- package/lib/helpers/isBsonType.js +15 -0
- package/lib/helpers/isMongooseObject.js +22 -0
- package/lib/helpers/isObject.js +16 -0
- package/lib/helpers/isPOJO.js +12 -0
- package/lib/helpers/isPromise.js +6 -0
- package/lib/helpers/isSimpleValidator.js +22 -0
- package/lib/helpers/minimize.js +41 -0
- package/lib/helpers/model/applyDefaultsToPOJO.js +52 -0
- package/lib/helpers/model/applyHooks.js +140 -0
- package/lib/helpers/model/applyMethods.js +70 -0
- package/lib/helpers/model/applyStaticHooks.js +33 -0
- package/lib/helpers/model/applyStatics.js +13 -0
- package/lib/helpers/model/castBulkWrite.js +316 -0
- package/lib/helpers/model/decorateBulkWriteResult.js +8 -0
- package/lib/helpers/model/discriminator.js +265 -0
- package/lib/helpers/model/pushNestedArrayPaths.js +15 -0
- package/lib/helpers/omitUndefined.js +20 -0
- package/lib/helpers/once.js +12 -0
- package/lib/helpers/parallelLimit.js +37 -0
- package/lib/helpers/path/parentPaths.js +18 -0
- package/lib/helpers/path/setDottedPath.js +33 -0
- package/lib/helpers/pluralize.js +95 -0
- package/lib/helpers/populate/assignRawDocsToIdStructure.js +129 -0
- package/lib/helpers/populate/assignVals.js +360 -0
- package/lib/helpers/populate/createPopulateQueryFilter.js +97 -0
- package/lib/helpers/populate/getModelsMapForPopulate.js +776 -0
- package/lib/helpers/populate/getSchemaTypes.js +228 -0
- package/lib/helpers/populate/getVirtual.js +103 -0
- package/lib/helpers/populate/leanPopulateMap.js +7 -0
- package/lib/helpers/populate/lookupLocalFields.js +40 -0
- package/lib/helpers/populate/markArraySubdocsPopulated.js +49 -0
- package/lib/helpers/populate/modelNamesFromRefPath.js +66 -0
- package/lib/helpers/populate/removeDeselectedForeignField.js +31 -0
- package/lib/helpers/populate/setPopulatedVirtualValue.js +33 -0
- package/lib/helpers/populate/skipPopulateValue.js +10 -0
- package/lib/helpers/populate/validateRef.js +19 -0
- package/lib/helpers/printJestWarning.js +21 -0
- package/lib/helpers/processConnectionOptions.js +65 -0
- package/lib/helpers/projection/applyProjection.js +83 -0
- package/lib/helpers/projection/hasIncludedChildren.js +41 -0
- package/lib/helpers/projection/isDefiningProjection.js +18 -0
- package/lib/helpers/projection/isExclusive.js +37 -0
- package/lib/helpers/projection/isInclusive.js +39 -0
- package/lib/helpers/projection/isNestedProjection.js +8 -0
- package/lib/helpers/projection/isPathExcluded.js +40 -0
- package/lib/helpers/projection/isPathSelectedInclusive.js +28 -0
- package/lib/helpers/projection/isSubpath.js +14 -0
- package/lib/helpers/projection/parseProjection.js +33 -0
- package/lib/helpers/query/applyGlobalOption.js +29 -0
- package/lib/helpers/query/cast$expr.js +287 -0
- package/lib/helpers/query/castFilterPath.js +54 -0
- package/lib/helpers/query/castUpdate.js +643 -0
- package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +103 -0
- package/lib/helpers/query/handleImmutable.js +44 -0
- package/lib/helpers/query/handleReadPreferenceAliases.js +23 -0
- package/lib/helpers/query/hasDollarKeys.js +23 -0
- package/lib/helpers/query/isOperator.js +14 -0
- package/lib/helpers/query/sanitizeFilter.js +38 -0
- package/lib/helpers/query/sanitizeProjection.js +14 -0
- package/lib/helpers/query/selectPopulatedFields.js +62 -0
- package/lib/helpers/query/trusted.js +13 -0
- package/lib/helpers/query/validOps.js +3 -0
- package/lib/helpers/schema/addAutoId.js +7 -0
- package/lib/helpers/schema/applyBuiltinPlugins.js +12 -0
- package/lib/helpers/schema/applyPlugins.js +55 -0
- package/lib/helpers/schema/applyReadConcern.js +20 -0
- package/lib/helpers/schema/applyWriteConcern.js +39 -0
- package/lib/helpers/schema/cleanPositionalOperators.js +12 -0
- package/lib/helpers/schema/getIndexes.js +171 -0
- package/lib/helpers/schema/getKeysInSchemaOrder.js +28 -0
- package/lib/helpers/schema/getPath.js +43 -0
- package/lib/helpers/schema/getSubdocumentStrictValue.js +32 -0
- package/lib/helpers/schema/handleIdOption.js +20 -0
- package/lib/helpers/schema/handleTimestampOption.js +24 -0
- package/lib/helpers/schema/idGetter.js +34 -0
- package/lib/helpers/schema/merge.js +36 -0
- package/lib/helpers/schematype/handleImmutable.js +50 -0
- package/lib/helpers/setDefaultsOnInsert.js +158 -0
- package/lib/helpers/specialProperties.js +3 -0
- package/lib/helpers/symbols.js +20 -0
- package/lib/helpers/timers.js +3 -0
- package/lib/helpers/timestamps/setDocumentTimestamps.js +26 -0
- package/lib/helpers/timestamps/setupTimestamps.js +116 -0
- package/lib/helpers/topology/allServersUnknown.js +12 -0
- package/lib/helpers/topology/isAtlas.js +31 -0
- package/lib/helpers/topology/isSSLError.js +16 -0
- package/lib/helpers/update/applyTimestampsToChildren.js +193 -0
- package/lib/helpers/update/applyTimestampsToUpdate.js +131 -0
- package/lib/helpers/update/castArrayFilters.js +113 -0
- package/lib/helpers/update/decorateUpdateWithVersionKey.js +35 -0
- package/lib/helpers/update/modifiedPaths.js +33 -0
- package/lib/helpers/update/moveImmutableProperties.js +53 -0
- package/lib/helpers/update/removeUnusedArrayFilters.js +32 -0
- package/lib/helpers/update/updatedPathsByArrayFilter.js +27 -0
- package/lib/helpers/updateValidators.js +193 -0
- package/lib/index.js +17 -0
- package/lib/internal.js +46 -0
- package/lib/model.js +5010 -0
- package/lib/modifiedPathsSnapshot.js +9 -0
- package/lib/mongoose.js +1411 -0
- package/lib/options/populateOptions.js +36 -0
- package/lib/options/propertyOptions.js +8 -0
- package/lib/options/saveOptions.js +16 -0
- package/lib/options/schemaArrayOptions.js +78 -0
- package/lib/options/schemaBufferOptions.js +38 -0
- package/lib/options/schemaDateOptions.js +71 -0
- package/lib/options/schemaDocumentArrayOptions.js +68 -0
- package/lib/options/schemaMapOptions.js +43 -0
- package/lib/options/schemaNumberOptions.js +101 -0
- package/lib/options/schemaObjectIdOptions.js +64 -0
- package/lib/options/schemaStringOptions.js +138 -0
- package/lib/options/schemaSubdocumentOptions.js +66 -0
- package/lib/options/schemaTypeOptions.js +244 -0
- package/lib/options/schemaUnionOptions.js +32 -0
- package/lib/options/virtualOptions.js +164 -0
- package/lib/options.js +17 -0
- package/lib/plugins/index.js +6 -0
- package/lib/plugins/saveSubdocs.js +76 -0
- package/lib/plugins/sharding.js +84 -0
- package/lib/plugins/trackTransaction.js +84 -0
- package/lib/plugins/validateBeforeSave.js +41 -0
- package/lib/query.js +5673 -0
- package/lib/queryHelpers.js +387 -0
- package/lib/schema/array.js +699 -0
- package/lib/schema/bigint.js +282 -0
- package/lib/schema/boolean.js +332 -0
- package/lib/schema/buffer.js +343 -0
- package/lib/schema/date.js +467 -0
- package/lib/schema/decimal128.js +263 -0
- package/lib/schema/documentArray.js +656 -0
- package/lib/schema/documentArrayElement.js +137 -0
- package/lib/schema/double.js +246 -0
- package/lib/schema/index.js +32 -0
- package/lib/schema/int32.js +289 -0
- package/lib/schema/map.js +201 -0
- package/lib/schema/mixed.js +146 -0
- package/lib/schema/number.js +510 -0
- package/lib/schema/objectId.js +333 -0
- package/lib/schema/operators/bitwise.js +38 -0
- package/lib/schema/operators/exists.js +12 -0
- package/lib/schema/operators/geospatial.js +107 -0
- package/lib/schema/operators/helpers.js +32 -0
- package/lib/schema/operators/text.js +39 -0
- package/lib/schema/operators/type.js +20 -0
- package/lib/schema/string.js +733 -0
- package/lib/schema/subdocument.js +436 -0
- package/lib/schema/symbols.js +5 -0
- package/lib/schema/union.js +113 -0
- package/lib/schema/uuid.js +305 -0
- package/lib/schema.js +3226 -0
- package/lib/schemaType.js +1835 -0
- package/lib/stateMachine.js +232 -0
- package/lib/types/array/index.js +119 -0
- package/lib/types/array/isMongooseArray.js +5 -0
- package/lib/types/array/methods/index.js +1095 -0
- package/lib/types/arraySubdocument.js +207 -0
- package/lib/types/buffer.js +294 -0
- package/lib/types/decimal128.js +13 -0
- package/lib/types/documentArray/index.js +113 -0
- package/lib/types/documentArray/isMongooseDocumentArray.js +5 -0
- package/lib/types/documentArray/methods/index.js +415 -0
- package/lib/types/double.js +13 -0
- package/lib/types/index.js +23 -0
- package/lib/types/map.js +419 -0
- package/lib/types/objectid.js +41 -0
- package/lib/types/subdocument.js +464 -0
- package/lib/types/uuid.js +13 -0
- package/lib/utils.js +1054 -0
- package/lib/validOptions.js +42 -0
- package/lib/virtualType.js +204 -0
- package/package.json +148 -0
- package/types/aggregate.d.ts +180 -0
- package/types/augmentations.d.ts +9 -0
- package/types/callback.d.ts +8 -0
- package/types/collection.d.ts +49 -0
- package/types/connection.d.ts +297 -0
- package/types/cursor.d.ts +67 -0
- package/types/document.d.ts +374 -0
- package/types/error.d.ts +143 -0
- package/types/expressions.d.ts +3053 -0
- package/types/helpers.d.ts +32 -0
- package/types/index.d.ts +1056 -0
- package/types/indexes.d.ts +97 -0
- package/types/inferhydrateddoctype.d.ts +115 -0
- package/types/inferrawdoctype.d.ts +135 -0
- package/types/inferschematype.d.ts +337 -0
- package/types/middlewares.d.ts +59 -0
- package/types/models.d.ts +1306 -0
- package/types/mongooseoptions.d.ts +228 -0
- package/types/pipelinestage.d.ts +333 -0
- package/types/populate.d.ts +53 -0
- package/types/query.d.ts +934 -0
- package/types/schemaoptions.d.ts +282 -0
- package/types/schematypes.d.ts +654 -0
- package/types/session.d.ts +32 -0
- package/types/types.d.ts +109 -0
- package/types/utility.d.ts +175 -0
- package/types/validation.d.ts +39 -0
- package/types/virtuals.d.ts +14 -0
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const clone = require('../../helpers/clone');
|
|
4
|
+
const documentSchemaSymbol = require('../../helpers/symbols').documentSchemaSymbol;
|
|
5
|
+
const internalToObjectOptions = require('../../options').internalToObjectOptions;
|
|
6
|
+
const utils = require('../../utils');
|
|
7
|
+
|
|
8
|
+
let Document;
|
|
9
|
+
const getSymbol = require('../../helpers/symbols').getSymbol;
|
|
10
|
+
const scopeSymbol = require('../../helpers/symbols').scopeSymbol;
|
|
11
|
+
|
|
12
|
+
const isPOJO = utils.isPOJO;
|
|
13
|
+
|
|
14
|
+
/*!
|
|
15
|
+
* exports
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
exports.compile = compile;
|
|
19
|
+
exports.defineKey = defineKey;
|
|
20
|
+
|
|
21
|
+
const _isEmptyOptions = Object.freeze({
|
|
22
|
+
minimize: true,
|
|
23
|
+
virtuals: false,
|
|
24
|
+
getters: false,
|
|
25
|
+
transform: false
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const noDottedPathGetOptions = Object.freeze({
|
|
29
|
+
noDottedPath: true
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Compiles schemas.
|
|
34
|
+
* @param {Object} tree
|
|
35
|
+
* @param {Any} proto
|
|
36
|
+
* @param {String} prefix
|
|
37
|
+
* @param {Object} options
|
|
38
|
+
* @api private
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
function compile(tree, proto, prefix, options) {
|
|
42
|
+
Document = Document || require('../../document');
|
|
43
|
+
const typeKey = options.typeKey;
|
|
44
|
+
|
|
45
|
+
for (const key of Object.keys(tree)) {
|
|
46
|
+
const limb = tree[key];
|
|
47
|
+
|
|
48
|
+
const hasSubprops = isPOJO(limb) &&
|
|
49
|
+
utils.hasOwnKeys(limb) &&
|
|
50
|
+
(!limb[typeKey] || (typeKey === 'type' && isPOJO(limb.type) && limb.type.type));
|
|
51
|
+
const subprops = hasSubprops ? limb : null;
|
|
52
|
+
|
|
53
|
+
defineKey({ prop: key, subprops: subprops, prototype: proto, prefix: prefix, options: options });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Defines the accessor named prop on the incoming prototype.
|
|
59
|
+
* @param {Object} options
|
|
60
|
+
* @param {String} options.prop
|
|
61
|
+
* @param {Boolean} options.subprops
|
|
62
|
+
* @param {Any} options.prototype
|
|
63
|
+
* @param {String} [options.prefix]
|
|
64
|
+
* @param {Object} options.options
|
|
65
|
+
* @api private
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
function defineKey({ prop, subprops, prototype, prefix, options }) {
|
|
69
|
+
Document = Document || require('../../document');
|
|
70
|
+
const path = (prefix ? prefix + '.' : '') + prop;
|
|
71
|
+
prefix = prefix || '';
|
|
72
|
+
const useGetOptions = prefix ? Object.freeze({}) : noDottedPathGetOptions;
|
|
73
|
+
|
|
74
|
+
if (subprops) {
|
|
75
|
+
Object.defineProperty(prototype, prop, {
|
|
76
|
+
enumerable: true,
|
|
77
|
+
configurable: true,
|
|
78
|
+
get: function() {
|
|
79
|
+
const _this = this;
|
|
80
|
+
if (!this.$__.getters) {
|
|
81
|
+
this.$__.getters = {};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (!this.$__.getters[path]) {
|
|
85
|
+
const nested = Object.create(Document.prototype, getOwnPropertyDescriptors(this));
|
|
86
|
+
|
|
87
|
+
// save scope for nested getters/setters
|
|
88
|
+
if (!prefix) {
|
|
89
|
+
nested.$__[scopeSymbol] = this;
|
|
90
|
+
}
|
|
91
|
+
nested.$__.nestedPath = path;
|
|
92
|
+
|
|
93
|
+
Object.defineProperty(nested, 'schema', {
|
|
94
|
+
enumerable: false,
|
|
95
|
+
configurable: true,
|
|
96
|
+
writable: false,
|
|
97
|
+
value: prototype.schema
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
Object.defineProperty(nested, '$__schema', {
|
|
101
|
+
enumerable: false,
|
|
102
|
+
configurable: true,
|
|
103
|
+
writable: false,
|
|
104
|
+
value: prototype.schema
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
Object.defineProperty(nested, documentSchemaSymbol, {
|
|
108
|
+
enumerable: false,
|
|
109
|
+
configurable: true,
|
|
110
|
+
writable: false,
|
|
111
|
+
value: prototype.schema
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
Object.defineProperty(nested, 'toObject', {
|
|
115
|
+
enumerable: false,
|
|
116
|
+
configurable: true,
|
|
117
|
+
writable: false,
|
|
118
|
+
value: function() {
|
|
119
|
+
return clone(_this.get(path, null, {
|
|
120
|
+
virtuals: this &&
|
|
121
|
+
this.schema &&
|
|
122
|
+
this.schema.options &&
|
|
123
|
+
this.schema.options.toObject &&
|
|
124
|
+
this.schema.options.toObject.virtuals || null
|
|
125
|
+
}));
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
Object.defineProperty(nested, '$__get', {
|
|
130
|
+
enumerable: false,
|
|
131
|
+
configurable: true,
|
|
132
|
+
writable: false,
|
|
133
|
+
value: function() {
|
|
134
|
+
return _this.get(path, null, {
|
|
135
|
+
virtuals: this?.schema?.options?.toObject?.virtuals || null
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
Object.defineProperty(nested, 'toJSON', {
|
|
141
|
+
enumerable: false,
|
|
142
|
+
configurable: true,
|
|
143
|
+
writable: false,
|
|
144
|
+
value: function() {
|
|
145
|
+
return _this.get(path, null, {
|
|
146
|
+
virtuals: this?.schema?.options?.toJSON?.virtuals || null
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
Object.defineProperty(nested, '$__isNested', {
|
|
152
|
+
enumerable: false,
|
|
153
|
+
configurable: true,
|
|
154
|
+
writable: false,
|
|
155
|
+
value: true
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
Object.defineProperty(nested, '$isEmpty', {
|
|
159
|
+
enumerable: false,
|
|
160
|
+
configurable: true,
|
|
161
|
+
writable: false,
|
|
162
|
+
value: function() {
|
|
163
|
+
return Object.keys(this.get(path, null, _isEmptyOptions) || {}).length === 0;
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
Object.defineProperty(nested, '$__parent', {
|
|
168
|
+
enumerable: false,
|
|
169
|
+
configurable: true,
|
|
170
|
+
writable: false,
|
|
171
|
+
value: this
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
compile(subprops, nested, path, options);
|
|
175
|
+
this.$__.getters[path] = nested;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return this.$__.getters[path];
|
|
179
|
+
},
|
|
180
|
+
set: function(v) {
|
|
181
|
+
if (v?.$__isNested) {
|
|
182
|
+
// Convert top-level to POJO, but leave subdocs hydrated so `$set`
|
|
183
|
+
// can handle them. See gh-9293.
|
|
184
|
+
v = v.$__get();
|
|
185
|
+
} else if (v instanceof Document && !v.$__isNested) {
|
|
186
|
+
v = v.$toObject(internalToObjectOptions);
|
|
187
|
+
}
|
|
188
|
+
const doc = this.$__[scopeSymbol] || this;
|
|
189
|
+
doc.$set(path, v);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
} else {
|
|
193
|
+
Object.defineProperty(prototype, prop, {
|
|
194
|
+
enumerable: true,
|
|
195
|
+
configurable: true,
|
|
196
|
+
get: function() {
|
|
197
|
+
return this[getSymbol].call(
|
|
198
|
+
this.$__[scopeSymbol] || this,
|
|
199
|
+
path,
|
|
200
|
+
null,
|
|
201
|
+
useGetOptions
|
|
202
|
+
);
|
|
203
|
+
},
|
|
204
|
+
set: function(v) {
|
|
205
|
+
this.$set.call(this.$__[scopeSymbol] || this, path, v);
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// gets descriptors for all properties of `object`
|
|
212
|
+
// makes all properties non-enumerable to match previous behavior to #2211
|
|
213
|
+
function getOwnPropertyDescriptors(object) {
|
|
214
|
+
const result = {};
|
|
215
|
+
|
|
216
|
+
Object.getOwnPropertyNames(object).forEach(function(key) {
|
|
217
|
+
const skip = [
|
|
218
|
+
'isNew',
|
|
219
|
+
'$__',
|
|
220
|
+
'$errors',
|
|
221
|
+
'errors',
|
|
222
|
+
'_doc',
|
|
223
|
+
'$locals',
|
|
224
|
+
'$op',
|
|
225
|
+
'__parentArray',
|
|
226
|
+
'__index',
|
|
227
|
+
'$isDocumentArrayElement'
|
|
228
|
+
].indexOf(key) === -1;
|
|
229
|
+
if (skip) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
result[key] = Object.getOwnPropertyDescriptor(object, key);
|
|
234
|
+
result[key].enumerable = false;
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
return result;
|
|
238
|
+
}
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const get = require('../get');
|
|
4
|
+
const getSchemaDiscriminatorByValue = require('../discriminator/getSchemaDiscriminatorByValue');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Like `schema.path()`, except with a document, because impossible to
|
|
8
|
+
* determine path type without knowing the embedded discriminator key.
|
|
9
|
+
*
|
|
10
|
+
* @param {Document} doc
|
|
11
|
+
* @param {String|String[]} path
|
|
12
|
+
* @param {Object} [options]
|
|
13
|
+
* @api private
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
module.exports = function getEmbeddedDiscriminatorPath(doc, path, options) {
|
|
17
|
+
options = options || {};
|
|
18
|
+
const typeOnly = options.typeOnly;
|
|
19
|
+
const parts = Array.isArray(path) ?
|
|
20
|
+
path :
|
|
21
|
+
(path.indexOf('.') === -1 ? [path] : path.split('.'));
|
|
22
|
+
let schemaType = null;
|
|
23
|
+
let type = 'adhocOrUndefined';
|
|
24
|
+
|
|
25
|
+
const schema = getSchemaDiscriminatorByValue(doc.schema, doc.get(doc.schema.options.discriminatorKey)) || doc.schema;
|
|
26
|
+
|
|
27
|
+
for (let i = 0; i < parts.length; ++i) {
|
|
28
|
+
const subpath = parts.slice(0, i + 1).join('.');
|
|
29
|
+
schemaType = schema.path(subpath);
|
|
30
|
+
if (schemaType == null) {
|
|
31
|
+
type = 'adhocOrUndefined';
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (schemaType.instance === 'Mixed') {
|
|
35
|
+
return typeOnly ? 'real' : schemaType;
|
|
36
|
+
}
|
|
37
|
+
type = schema.pathType(subpath);
|
|
38
|
+
if ((schemaType.$isSingleNested || schemaType.$isMongooseDocumentArrayElement) &&
|
|
39
|
+
schemaType.schema.discriminators != null) {
|
|
40
|
+
const discriminators = schemaType.schema.discriminators;
|
|
41
|
+
const discriminatorKey = doc.get(subpath + '.' +
|
|
42
|
+
get(schemaType, 'schema.options.discriminatorKey'));
|
|
43
|
+
if (discriminatorKey == null || discriminators[discriminatorKey] == null) {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const rest = parts.slice(i + 1).join('.');
|
|
47
|
+
return getEmbeddedDiscriminatorPath(doc.get(subpath), rest, options);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Are we getting the whole schema or just the type, 'real', 'nested', etc.
|
|
52
|
+
return typeOnly ? type : schemaType;
|
|
53
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const utils = require('../../utils');
|
|
4
|
+
|
|
5
|
+
const keysToSkip = new Set(['__index', '__parentArray', '_doc']);
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Using spread operator on a Mongoose document gives you a
|
|
9
|
+
* POJO that has a tendency to cause infinite recursion. So
|
|
10
|
+
* we use this function on `set()` to prevent that.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
module.exports = function handleSpreadDoc(v, includeExtraKeys) {
|
|
14
|
+
if (utils.isPOJO(v) && v.$__ != null && v._doc != null) {
|
|
15
|
+
if (includeExtraKeys) {
|
|
16
|
+
const extraKeys = {};
|
|
17
|
+
for (const key of Object.keys(v)) {
|
|
18
|
+
if (typeof key === 'symbol') {
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
if (key[0] === '$') {
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
if (keysToSkip.has(key)) {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
extraKeys[key] = v[key];
|
|
28
|
+
}
|
|
29
|
+
return { ...v._doc, ...extraKeys };
|
|
30
|
+
}
|
|
31
|
+
return v._doc;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return v;
|
|
35
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = function each(arr, cb, done) {
|
|
4
|
+
if (arr.length === 0) {
|
|
5
|
+
return done();
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
let remaining = arr.length;
|
|
9
|
+
let err = null;
|
|
10
|
+
for (const v of arr) {
|
|
11
|
+
cb(v, function(_err) {
|
|
12
|
+
if (err != null) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (_err != null) {
|
|
16
|
+
err = _err;
|
|
17
|
+
return done(err);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (--remaining <= 0) {
|
|
21
|
+
return done();
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*!
|
|
4
|
+
* ignore
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
module.exports = function combinePathErrors(err) {
|
|
8
|
+
const keys = Object.keys(err.errors || {});
|
|
9
|
+
const len = keys.length;
|
|
10
|
+
const msgs = [];
|
|
11
|
+
let key;
|
|
12
|
+
|
|
13
|
+
for (let i = 0; i < len; ++i) {
|
|
14
|
+
key = keys[i];
|
|
15
|
+
if (err === err.errors[key]) {
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
msgs.push(key + ': ' + err.errors[key].message);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return msgs.join(', ');
|
|
22
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Simplified lodash.get to work around the annoying null quirk. See:
|
|
5
|
+
* https://github.com/lodash/lodash/issues/3659
|
|
6
|
+
* @api private
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
module.exports = function get(obj, path, def) {
|
|
10
|
+
let parts;
|
|
11
|
+
let isPathArray = false;
|
|
12
|
+
if (typeof path === 'string') {
|
|
13
|
+
if (path.indexOf('.') === -1) {
|
|
14
|
+
const _v = getProperty(obj, path);
|
|
15
|
+
if (_v == null) {
|
|
16
|
+
return def;
|
|
17
|
+
}
|
|
18
|
+
return _v;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
parts = path.split('.');
|
|
22
|
+
} else {
|
|
23
|
+
isPathArray = true;
|
|
24
|
+
parts = path;
|
|
25
|
+
|
|
26
|
+
if (parts.length === 1) {
|
|
27
|
+
const _v = getProperty(obj, parts[0]);
|
|
28
|
+
if (_v == null) {
|
|
29
|
+
return def;
|
|
30
|
+
}
|
|
31
|
+
return _v;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
let rest = path;
|
|
35
|
+
let cur = obj;
|
|
36
|
+
for (const part of parts) {
|
|
37
|
+
if (cur == null) {
|
|
38
|
+
return def;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// `lib/cast.js` depends on being able to get dotted paths in updates,
|
|
42
|
+
// like `{ $set: { 'a.b': 42 } }`
|
|
43
|
+
if (!isPathArray && cur[rest] != null) {
|
|
44
|
+
return cur[rest];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
cur = getProperty(cur, part);
|
|
48
|
+
|
|
49
|
+
if (!isPathArray) {
|
|
50
|
+
rest = rest.substr(part.length + 1);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return cur == null ? def : cur;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
function getProperty(obj, prop) {
|
|
58
|
+
if (obj == null) {
|
|
59
|
+
return obj;
|
|
60
|
+
}
|
|
61
|
+
if (obj instanceof Map) {
|
|
62
|
+
return obj.get(prop);
|
|
63
|
+
}
|
|
64
|
+
return obj[prop];
|
|
65
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* If `val` is an object, returns constructor name, if possible. Otherwise returns undefined.
|
|
5
|
+
* @api private
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
module.exports = function getConstructorName(val) {
|
|
9
|
+
if (val == null) {
|
|
10
|
+
return void 0;
|
|
11
|
+
}
|
|
12
|
+
if (typeof val.constructor !== 'function') {
|
|
13
|
+
return void 0;
|
|
14
|
+
}
|
|
15
|
+
return val.constructor.name;
|
|
16
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function getDefaultBulkwriteResult() {
|
|
4
|
+
return {
|
|
5
|
+
ok: 1,
|
|
6
|
+
nInserted: 0,
|
|
7
|
+
nUpserted: 0,
|
|
8
|
+
nMatched: 0,
|
|
9
|
+
nModified: 0,
|
|
10
|
+
nRemoved: 0,
|
|
11
|
+
upserted: [],
|
|
12
|
+
writeErrors: [],
|
|
13
|
+
insertedIds: [],
|
|
14
|
+
writeConcernErrors: []
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = getDefaultBulkwriteResult;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Centralize this so we can more easily work around issues with people
|
|
3
|
+
* stubbing out `process.nextTick()` in tests using sinon:
|
|
4
|
+
* https://github.com/sinonjs/lolex#automatically-incrementing-mocked-time
|
|
5
|
+
* See gh-6074
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
'use strict';
|
|
9
|
+
|
|
10
|
+
const nextTick = typeof process !== 'undefined' && typeof process.nextTick === 'function' ?
|
|
11
|
+
process.nextTick.bind(process) :
|
|
12
|
+
cb => setTimeout(cb, 0); // Fallback for browser build
|
|
13
|
+
|
|
14
|
+
module.exports = function immediate(cb) {
|
|
15
|
+
return nextTick(cb);
|
|
16
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const isTextIndex = require('./isTextIndex');
|
|
4
|
+
|
|
5
|
+
module.exports = function applySchemaCollation(indexKeys, indexOptions, schemaOptions) {
|
|
6
|
+
if (isTextIndex(indexKeys)) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
if (Object.hasOwn(schemaOptions, 'collation') && !Object.hasOwn(indexOptions, 'collation')) {
|
|
11
|
+
indexOptions.collation = schemaOptions.collation;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = function decorateDiscriminatorIndexOptions(schema, indexOptions) {
|
|
4
|
+
// If the model is a discriminator and has an index, add a
|
|
5
|
+
// partialFilterExpression by default so the index will only apply
|
|
6
|
+
// to that discriminator.
|
|
7
|
+
const discriminatorName = schema.discriminatorMapping?.value;
|
|
8
|
+
if (discriminatorName && !('sparse' in indexOptions)) {
|
|
9
|
+
const discriminatorKey = schema.options.discriminatorKey;
|
|
10
|
+
indexOptions.partialFilterExpression = indexOptions.partialFilterExpression || {};
|
|
11
|
+
indexOptions.partialFilterExpression[discriminatorKey] = discriminatorName;
|
|
12
|
+
}
|
|
13
|
+
return indexOptions;
|
|
14
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const hasDollarKeys = require('../query/hasDollarKeys');
|
|
4
|
+
|
|
5
|
+
function getRelatedSchemaIndexes(model, schemaIndexes) {
|
|
6
|
+
return getRelatedIndexes({
|
|
7
|
+
baseModelName: model.baseModelName,
|
|
8
|
+
discriminatorMapping: model.schema.discriminatorMapping,
|
|
9
|
+
indexes: schemaIndexes,
|
|
10
|
+
indexesType: 'schema'
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function getRelatedDBIndexes(model, dbIndexes) {
|
|
15
|
+
return getRelatedIndexes({
|
|
16
|
+
baseModelName: model.baseModelName,
|
|
17
|
+
discriminatorMapping: model.schema.discriminatorMapping,
|
|
18
|
+
indexes: dbIndexes,
|
|
19
|
+
indexesType: 'db'
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
module.exports = {
|
|
24
|
+
getRelatedSchemaIndexes,
|
|
25
|
+
getRelatedDBIndexes
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
function getRelatedIndexes({
|
|
29
|
+
baseModelName,
|
|
30
|
+
discriminatorMapping,
|
|
31
|
+
indexes,
|
|
32
|
+
indexesType
|
|
33
|
+
}) {
|
|
34
|
+
const discriminatorKey = discriminatorMapping?.key;
|
|
35
|
+
const discriminatorValue = discriminatorMapping?.value;
|
|
36
|
+
|
|
37
|
+
if (!discriminatorKey) {
|
|
38
|
+
return indexes;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const isChildDiscriminatorModel = Boolean(baseModelName);
|
|
42
|
+
if (isChildDiscriminatorModel) {
|
|
43
|
+
return indexes.filter(index => {
|
|
44
|
+
const partialFilterExpression = getPartialFilterExpression(index, indexesType);
|
|
45
|
+
return partialFilterExpression && partialFilterExpression[discriminatorKey] === discriminatorValue;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return indexes.filter(index => {
|
|
50
|
+
const partialFilterExpression = getPartialFilterExpression(index, indexesType);
|
|
51
|
+
return !partialFilterExpression
|
|
52
|
+
|| !partialFilterExpression[discriminatorKey]
|
|
53
|
+
|| (hasDollarKeys(partialFilterExpression[discriminatorKey]) && !('$eq' in partialFilterExpression[discriminatorKey]));
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function getPartialFilterExpression(index, indexesType) {
|
|
58
|
+
if (indexesType === 'schema') {
|
|
59
|
+
const options = index[1];
|
|
60
|
+
return options?.partialFilterExpression;
|
|
61
|
+
}
|
|
62
|
+
return index.partialFilterExpression;
|
|
63
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const get = require('../get');
|
|
4
|
+
|
|
5
|
+
module.exports = function isDefaultIdIndex(index) {
|
|
6
|
+
if (Array.isArray(index)) {
|
|
7
|
+
// Mongoose syntax
|
|
8
|
+
const keys = Object.keys(index[0]);
|
|
9
|
+
return keys.length === 1 && keys[0] === '_id' && index[0]._id !== 'hashed';
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (typeof index !== 'object') {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const key = get(index, 'key', {});
|
|
17
|
+
return Object.keys(key).length === 1 && Object.hasOwn(key, '_id');
|
|
18
|
+
};
|