@datanimbus/mongoose 6.13.10
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/.eslintrc.js +232 -0
- package/.mocharc.yml +4 -0
- package/LICENSE.md +22 -0
- package/README.md +387 -0
- package/SECURITY.md +1 -0
- package/browser.js +8 -0
- package/dist/browser.umd.js +2 -0
- package/index.js +63 -0
- package/lgtm.yml +12 -0
- package/lib/aggregate.js +1164 -0
- package/lib/browser.js +158 -0
- package/lib/browserDocument.js +101 -0
- package/lib/cast/boolean.js +32 -0
- package/lib/cast/date.js +41 -0
- package/lib/cast/decimal128.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.js +418 -0
- package/lib/collection.js +311 -0
- package/lib/connection.js +1574 -0
- package/lib/connectionstate.js +26 -0
- package/lib/cursor/AggregationCursor.js +380 -0
- package/lib/cursor/ChangeStream.js +154 -0
- package/lib/cursor/QueryCursor.js +538 -0
- package/lib/document.js +4771 -0
- package/lib/document_provider.js +30 -0
- package/lib/driver.js +15 -0
- package/lib/drivers/SPEC.md +4 -0
- package/lib/drivers/browser/ReadPreference.js +7 -0
- package/lib/drivers/browser/binary.js +14 -0
- package/lib/drivers/browser/decimal128.js +7 -0
- package/lib/drivers/browser/index.js +16 -0
- package/lib/drivers/browser/objectid.js +29 -0
- package/lib/drivers/node-mongodb-native/ReadPreference.js +47 -0
- package/lib/drivers/node-mongodb-native/binary.js +10 -0
- package/lib/drivers/node-mongodb-native/collection.js +460 -0
- package/lib/drivers/node-mongodb-native/connection.js +166 -0
- package/lib/drivers/node-mongodb-native/decimal128.js +7 -0
- package/lib/drivers/node-mongodb-native/index.js +12 -0
- package/lib/drivers/node-mongodb-native/objectid.js +16 -0
- package/lib/error/browserMissingSchema.js +28 -0
- package/lib/error/bulkWriteError.js +41 -0
- package/lib/error/cast.js +158 -0
- package/lib/error/disconnected.js +33 -0
- package/lib/error/divergentArray.js +38 -0
- package/lib/error/eachAsyncMultiError.js +41 -0
- package/lib/error/index.js +228 -0
- package/lib/error/messages.js +47 -0
- package/lib/error/missingSchema.js +31 -0
- package/lib/error/mongooseError.js +13 -0
- package/lib/error/notFound.js +45 -0
- package/lib/error/objectExpected.js +30 -0
- package/lib/error/objectParameter.js +30 -0
- package/lib/error/overwriteModel.js +30 -0
- package/lib/error/parallelSave.js +30 -0
- package/lib/error/parallelValidate.js +31 -0
- package/lib/error/serverSelection.js +61 -0
- package/lib/error/setOptionError.js +101 -0
- package/lib/error/strict.js +33 -0
- package/lib/error/strictPopulate.js +29 -0
- package/lib/error/syncIndexes.js +30 -0
- package/lib/error/validation.js +103 -0
- package/lib/error/validator.js +99 -0
- package/lib/error/version.js +36 -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 +177 -0
- package/lib/helpers/common.js +127 -0
- package/lib/helpers/cursor/eachAsync.js +222 -0
- package/lib/helpers/discriminator/areDiscriminatorValuesEqual.js +16 -0
- package/lib/helpers/discriminator/checkEmbeddedDiscriminatorKeyProjection.js +12 -0
- package/lib/helpers/discriminator/getConstructor.js +26 -0
- package/lib/helpers/discriminator/getDiscriminatorByValue.js +28 -0
- package/lib/helpers/discriminator/getSchemaDiscriminatorByValue.js +27 -0
- package/lib/helpers/discriminator/mergeDiscriminatorSchema.js +64 -0
- package/lib/helpers/document/applyDefaults.js +128 -0
- package/lib/helpers/document/cleanModifiedSubpaths.js +35 -0
- package/lib/helpers/document/compile.js +237 -0
- package/lib/helpers/document/getEmbeddedDiscriminatorPath.js +50 -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 +27 -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 +59 -0
- package/lib/helpers/indexes/isDefaultIdIndex.js +18 -0
- package/lib/helpers/indexes/isIndexEqual.js +96 -0
- package/lib/helpers/indexes/isTextIndex.js +16 -0
- package/lib/helpers/isAsyncFunction.js +9 -0
- package/lib/helpers/isBsonType.js +16 -0
- package/lib/helpers/isMongooseObject.js +22 -0
- package/lib/helpers/isObject.js +16 -0
- package/lib/helpers/isPromise.js +6 -0
- package/lib/helpers/isSimpleValidator.js +22 -0
- package/lib/helpers/model/applyDefaultsToPOJO.js +52 -0
- package/lib/helpers/model/applyHooks.js +149 -0
- package/lib/helpers/model/applyMethods.js +70 -0
- package/lib/helpers/model/applyStaticHooks.js +71 -0
- package/lib/helpers/model/applyStatics.js +13 -0
- package/lib/helpers/model/castBulkWrite.js +240 -0
- package/lib/helpers/model/discriminator.js +218 -0
- package/lib/helpers/model/pushNestedArrayPaths.js +15 -0
- package/lib/helpers/once.js +12 -0
- package/lib/helpers/parallelLimit.js +55 -0
- package/lib/helpers/path/parentPaths.js +18 -0
- package/lib/helpers/path/setDottedPath.js +33 -0
- package/lib/helpers/pluralize.js +94 -0
- package/lib/helpers/populate/SkipPopulateValue.js +10 -0
- package/lib/helpers/populate/assignRawDocsToIdStructure.js +124 -0
- package/lib/helpers/populate/assignVals.js +337 -0
- package/lib/helpers/populate/createPopulateQueryFilter.js +97 -0
- package/lib/helpers/populate/getModelsMapForPopulate.js +756 -0
- package/lib/helpers/populate/getSchemaTypes.js +233 -0
- package/lib/helpers/populate/getVirtual.js +72 -0
- package/lib/helpers/populate/leanPopulateMap.js +7 -0
- package/lib/helpers/populate/lookupLocalFields.js +40 -0
- package/lib/helpers/populate/markArraySubdocsPopulated.js +47 -0
- package/lib/helpers/populate/modelNamesFromRefPath.js +68 -0
- package/lib/helpers/populate/removeDeselectedForeignField.js +31 -0
- package/lib/helpers/populate/validateRef.js +19 -0
- package/lib/helpers/printJestWarning.js +21 -0
- package/lib/helpers/printStrictQueryWarning.js +11 -0
- package/lib/helpers/processConnectionOptions.js +65 -0
- package/lib/helpers/projection/applyProjection.js +77 -0
- package/lib/helpers/projection/hasIncludedChildren.js +41 -0
- package/lib/helpers/projection/isDefiningProjection.js +18 -0
- package/lib/helpers/projection/isExclusive.js +35 -0
- package/lib/helpers/projection/isInclusive.js +38 -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/promiseOrCallback.js +55 -0
- package/lib/helpers/query/applyGlobalOption.js +29 -0
- package/lib/helpers/query/applyQueryMiddleware.js +79 -0
- package/lib/helpers/query/cast$expr.js +282 -0
- package/lib/helpers/query/castFilterPath.js +54 -0
- package/lib/helpers/query/castUpdate.js +560 -0
- package/lib/helpers/query/completeMany.js +52 -0
- package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +90 -0
- package/lib/helpers/query/handleImmutable.js +28 -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 +49 -0
- package/lib/helpers/query/trusted.js +13 -0
- package/lib/helpers/query/validOps.js +24 -0
- package/lib/helpers/query/wrapThunk.js +31 -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/applyWriteConcern.js +30 -0
- package/lib/helpers/schema/cleanPositionalOperators.js +12 -0
- package/lib/helpers/schema/getIndexes.js +164 -0
- package/lib/helpers/schema/getKeysInSchemaOrder.js +28 -0
- package/lib/helpers/schema/getPath.js +37 -0
- package/lib/helpers/schema/handleIdOption.js +20 -0
- package/lib/helpers/schema/handleTimestampOption.js +24 -0
- package/lib/helpers/schema/idGetter.js +32 -0
- package/lib/helpers/schema/merge.js +28 -0
- package/lib/helpers/schematype/handleImmutable.js +50 -0
- package/lib/helpers/setDefaultsOnInsert.js +132 -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 +101 -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 +117 -0
- package/lib/helpers/update/castArrayFilters.js +109 -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 +249 -0
- package/lib/index.js +1338 -0
- package/lib/internal.js +46 -0
- package/lib/model.js +5474 -0
- package/lib/options/PopulateOptions.js +36 -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 +42 -0
- package/lib/options/SchemaTypeOptions.js +244 -0
- package/lib/options/VirtualOptions.js +164 -0
- package/lib/options/propertyOptions.js +8 -0
- package/lib/options/removeOptions.js +14 -0
- package/lib/options/saveOptions.js +14 -0
- package/lib/options.js +15 -0
- package/lib/plugins/index.js +7 -0
- package/lib/plugins/removeSubdocs.js +31 -0
- package/lib/plugins/saveSubdocs.js +66 -0
- package/lib/plugins/sharding.js +83 -0
- package/lib/plugins/trackTransaction.js +92 -0
- package/lib/plugins/validateBeforeSave.js +45 -0
- package/lib/promise_provider.js +49 -0
- package/lib/query.js +5982 -0
- package/lib/queryhelpers.js +353 -0
- package/lib/schema/DocumentArrayElement.js +100 -0
- package/lib/schema/SubdocumentPath.js +369 -0
- package/lib/schema/array.js +663 -0
- package/lib/schema/boolean.js +267 -0
- package/lib/schema/buffer.js +269 -0
- package/lib/schema/date.js +404 -0
- package/lib/schema/decimal128.js +210 -0
- package/lib/schema/documentarray.js +598 -0
- package/lib/schema/index.js +39 -0
- package/lib/schema/map.js +84 -0
- package/lib/schema/mixed.js +132 -0
- package/lib/schema/number.js +438 -0
- package/lib/schema/objectid.js +295 -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 +694 -0
- package/lib/schema/symbols.js +5 -0
- package/lib/schema/uuid.js +334 -0
- package/lib/schema.js +2665 -0
- package/lib/schematype.js +1727 -0
- package/lib/statemachine.js +207 -0
- package/lib/types/ArraySubdocument.js +189 -0
- package/lib/types/DocumentArray/index.js +113 -0
- package/lib/types/DocumentArray/isMongooseDocumentArray.js +5 -0
- package/lib/types/DocumentArray/methods/index.js +383 -0
- package/lib/types/array/index.js +116 -0
- package/lib/types/array/isMongooseArray.js +5 -0
- package/lib/types/array/methods/index.js +1014 -0
- package/lib/types/buffer.js +277 -0
- package/lib/types/decimal128.js +13 -0
- package/lib/types/index.js +20 -0
- package/lib/types/map.js +354 -0
- package/lib/types/objectid.js +41 -0
- package/lib/types/subdocument.js +432 -0
- package/lib/utils.js +1011 -0
- package/lib/validoptions.js +36 -0
- package/lib/virtualtype.js +175 -0
- package/package.json +164 -0
- package/scripts/build-browser.js +18 -0
- package/scripts/create-tarball.js +7 -0
- package/scripts/generateSearch.js +154 -0
- package/scripts/tsc-diagnostics-check.js +15 -0
- package/tools/auth.js +31 -0
- package/tools/repl.js +35 -0
- package/tools/sharded.js +46 -0
- package/tsconfig.json +9 -0
- package/types/aggregate.d.ts +174 -0
- package/types/augmentations.d.ts +9 -0
- package/types/callback.d.ts +8 -0
- package/types/collection.d.ts +44 -0
- package/types/connection.d.ts +242 -0
- package/types/cursor.d.ts +62 -0
- package/types/document.d.ts +266 -0
- package/types/error.d.ts +138 -0
- package/types/expressions.d.ts +2936 -0
- package/types/helpers.d.ts +32 -0
- package/types/index.d.ts +627 -0
- package/types/indexes.d.ts +98 -0
- package/types/inferschematype.d.ts +228 -0
- package/types/middlewares.d.ts +32 -0
- package/types/models.d.ts +488 -0
- package/types/mongooseoptions.d.ts +206 -0
- package/types/pipelinestage.d.ts +297 -0
- package/types/populate.d.ts +45 -0
- package/types/query.d.ts +659 -0
- package/types/schemaoptions.d.ts +231 -0
- package/types/schematypes.d.ts +478 -0
- package/types/session.d.ts +36 -0
- package/types/types.d.ts +104 -0
- package/types/utility.d.ts +43 -0
- package/types/validation.d.ts +34 -0
- package/types/virtuals.d.ts +14 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*!
|
|
4
|
+
* ignore
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const get = require('../get');
|
|
8
|
+
|
|
9
|
+
module.exports = applyTimestampsToUpdate;
|
|
10
|
+
|
|
11
|
+
/*!
|
|
12
|
+
* ignore
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
function applyTimestampsToUpdate(now, createdAt, updatedAt, currentUpdate, options) {
|
|
16
|
+
const updates = currentUpdate;
|
|
17
|
+
let _updates = updates;
|
|
18
|
+
const overwrite = get(options, 'overwrite', false);
|
|
19
|
+
const timestamps = get(options, 'timestamps', true);
|
|
20
|
+
|
|
21
|
+
// Support skipping timestamps at the query level, see gh-6980
|
|
22
|
+
if (!timestamps || updates == null) {
|
|
23
|
+
return currentUpdate;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const skipCreatedAt = timestamps != null && timestamps.createdAt === false;
|
|
27
|
+
const skipUpdatedAt = timestamps != null && timestamps.updatedAt === false;
|
|
28
|
+
|
|
29
|
+
if (overwrite) {
|
|
30
|
+
if (currentUpdate && currentUpdate.$set) {
|
|
31
|
+
currentUpdate = currentUpdate.$set;
|
|
32
|
+
updates.$set = {};
|
|
33
|
+
_updates = updates.$set;
|
|
34
|
+
}
|
|
35
|
+
if (!skipUpdatedAt && updatedAt && !currentUpdate[updatedAt]) {
|
|
36
|
+
_updates[updatedAt] = now;
|
|
37
|
+
}
|
|
38
|
+
if (!skipCreatedAt && createdAt && !currentUpdate[createdAt]) {
|
|
39
|
+
_updates[createdAt] = now;
|
|
40
|
+
}
|
|
41
|
+
return updates;
|
|
42
|
+
}
|
|
43
|
+
currentUpdate = currentUpdate || {};
|
|
44
|
+
|
|
45
|
+
if (Array.isArray(updates)) {
|
|
46
|
+
// Update with aggregation pipeline
|
|
47
|
+
updates.push({ $set: { [updatedAt]: now } });
|
|
48
|
+
|
|
49
|
+
return updates;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
updates.$set = updates.$set || {};
|
|
53
|
+
if (!skipUpdatedAt && updatedAt &&
|
|
54
|
+
(!currentUpdate.$currentDate || !currentUpdate.$currentDate[updatedAt])) {
|
|
55
|
+
let timestampSet = false;
|
|
56
|
+
if (updatedAt.indexOf('.') !== -1) {
|
|
57
|
+
const pieces = updatedAt.split('.');
|
|
58
|
+
for (let i = 1; i < pieces.length; ++i) {
|
|
59
|
+
const remnant = pieces.slice(-i).join('.');
|
|
60
|
+
const start = pieces.slice(0, -i).join('.');
|
|
61
|
+
if (currentUpdate[start] != null) {
|
|
62
|
+
currentUpdate[start][remnant] = now;
|
|
63
|
+
timestampSet = true;
|
|
64
|
+
break;
|
|
65
|
+
} else if (currentUpdate.$set && currentUpdate.$set[start]) {
|
|
66
|
+
currentUpdate.$set[start][remnant] = now;
|
|
67
|
+
timestampSet = true;
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (!timestampSet) {
|
|
74
|
+
updates.$set[updatedAt] = now;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (updates.hasOwnProperty(updatedAt)) {
|
|
78
|
+
delete updates[updatedAt];
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!skipCreatedAt && createdAt) {
|
|
83
|
+
if (currentUpdate[createdAt]) {
|
|
84
|
+
delete currentUpdate[createdAt];
|
|
85
|
+
}
|
|
86
|
+
if (currentUpdate.$set && currentUpdate.$set[createdAt]) {
|
|
87
|
+
delete currentUpdate.$set[createdAt];
|
|
88
|
+
}
|
|
89
|
+
let timestampSet = false;
|
|
90
|
+
if (createdAt.indexOf('.') !== -1) {
|
|
91
|
+
const pieces = createdAt.split('.');
|
|
92
|
+
for (let i = 1; i < pieces.length; ++i) {
|
|
93
|
+
const remnant = pieces.slice(-i).join('.');
|
|
94
|
+
const start = pieces.slice(0, -i).join('.');
|
|
95
|
+
if (currentUpdate[start] != null) {
|
|
96
|
+
currentUpdate[start][remnant] = now;
|
|
97
|
+
timestampSet = true;
|
|
98
|
+
break;
|
|
99
|
+
} else if (currentUpdate.$set && currentUpdate.$set[start]) {
|
|
100
|
+
currentUpdate.$set[start][remnant] = now;
|
|
101
|
+
timestampSet = true;
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (!timestampSet) {
|
|
108
|
+
updates.$setOnInsert = updates.$setOnInsert || {};
|
|
109
|
+
updates.$setOnInsert[createdAt] = now;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (Object.keys(updates.$set).length === 0) {
|
|
114
|
+
delete updates.$set;
|
|
115
|
+
}
|
|
116
|
+
return updates;
|
|
117
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const castFilterPath = require('../query/castFilterPath');
|
|
4
|
+
const cleanPositionalOperators = require('../schema/cleanPositionalOperators');
|
|
5
|
+
const getPath = require('../schema/getPath');
|
|
6
|
+
const updatedPathsByArrayFilter = require('./updatedPathsByArrayFilter');
|
|
7
|
+
|
|
8
|
+
module.exports = function castArrayFilters(query) {
|
|
9
|
+
const arrayFilters = query.options.arrayFilters;
|
|
10
|
+
const update = query.getUpdate();
|
|
11
|
+
const schema = query.schema;
|
|
12
|
+
const updatedPathsByFilter = updatedPathsByArrayFilter(update);
|
|
13
|
+
|
|
14
|
+
let strictQuery = schema.options.strict;
|
|
15
|
+
if (query._mongooseOptions.strict != null) {
|
|
16
|
+
strictQuery = query._mongooseOptions.strict;
|
|
17
|
+
}
|
|
18
|
+
if (query.model && query.model.base.options.strictQuery != null) {
|
|
19
|
+
strictQuery = query.model.base.options.strictQuery;
|
|
20
|
+
}
|
|
21
|
+
if (schema._userProvidedOptions.strictQuery != null) {
|
|
22
|
+
strictQuery = schema._userProvidedOptions.strictQuery;
|
|
23
|
+
}
|
|
24
|
+
if (query._mongooseOptions.strictQuery != null) {
|
|
25
|
+
strictQuery = query._mongooseOptions.strictQuery;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
_castArrayFilters(arrayFilters, schema, strictQuery, updatedPathsByFilter, query);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function _castArrayFilters(arrayFilters, schema, strictQuery, updatedPathsByFilter, query) {
|
|
32
|
+
if (!Array.isArray(arrayFilters)) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
for (const filter of arrayFilters) {
|
|
37
|
+
if (filter == null) {
|
|
38
|
+
throw new Error(`Got null array filter in ${arrayFilters}`);
|
|
39
|
+
}
|
|
40
|
+
const keys = Object.keys(filter).filter(key => filter[key] != null);
|
|
41
|
+
if (keys.length === 0) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const firstKey = keys[0];
|
|
46
|
+
if (firstKey === '$and' || firstKey === '$or') {
|
|
47
|
+
for (const key of keys) {
|
|
48
|
+
_castArrayFilters(filter[key], schema, strictQuery, updatedPathsByFilter, query);
|
|
49
|
+
}
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const dot = firstKey.indexOf('.');
|
|
53
|
+
const filterWildcardPath = dot === -1 ? firstKey : firstKey.substring(0, dot);
|
|
54
|
+
if (updatedPathsByFilter[filterWildcardPath] == null) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const baseFilterPath = cleanPositionalOperators(
|
|
58
|
+
updatedPathsByFilter[filterWildcardPath]
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
const baseSchematype = getPath(schema, baseFilterPath);
|
|
62
|
+
let filterBaseSchema = baseSchematype != null ? baseSchematype.schema : null;
|
|
63
|
+
if (filterBaseSchema != null &&
|
|
64
|
+
filterBaseSchema.discriminators != null &&
|
|
65
|
+
filter[filterWildcardPath + '.' + filterBaseSchema.options.discriminatorKey]) {
|
|
66
|
+
filterBaseSchema = filterBaseSchema.discriminators[filter[filterWildcardPath + '.' + filterBaseSchema.options.discriminatorKey]] || filterBaseSchema;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
for (const key of keys) {
|
|
70
|
+
if (updatedPathsByFilter[key] === null) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (Object.keys(updatedPathsByFilter).length === 0) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const dot = key.indexOf('.');
|
|
77
|
+
|
|
78
|
+
let filterPathRelativeToBase = dot === -1 ? null : key.substring(dot);
|
|
79
|
+
let schematype;
|
|
80
|
+
if (filterPathRelativeToBase == null || filterBaseSchema == null) {
|
|
81
|
+
schematype = baseSchematype;
|
|
82
|
+
} else {
|
|
83
|
+
// If there are multiple array filters in the path being updated, make sure
|
|
84
|
+
// to replace them so we can get the schema path.
|
|
85
|
+
filterPathRelativeToBase = cleanPositionalOperators(filterPathRelativeToBase);
|
|
86
|
+
schematype = getPath(filterBaseSchema, filterPathRelativeToBase);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (schematype == null) {
|
|
90
|
+
if (!strictQuery) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const filterPath = filterPathRelativeToBase == null ?
|
|
94
|
+
baseFilterPath + '.0' :
|
|
95
|
+
baseFilterPath + '.0' + filterPathRelativeToBase;
|
|
96
|
+
// For now, treat `strictQuery = true` and `strictQuery = 'throw'` as
|
|
97
|
+
// equivalent for casting array filters. `strictQuery = true` doesn't
|
|
98
|
+
// quite work in this context because we never want to silently strip out
|
|
99
|
+
// array filters, even if the path isn't in the schema.
|
|
100
|
+
throw new Error(`Could not find path "${filterPath}" in schema`);
|
|
101
|
+
}
|
|
102
|
+
if (typeof filter[key] === 'object') {
|
|
103
|
+
filter[key] = castFilterPath(query, schematype, filter[key]);
|
|
104
|
+
} else {
|
|
105
|
+
filter[key] = schematype.castForQuery(filter[key]);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _modifiedPaths = require('../common').modifiedPaths;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Given an update document with potential update operators (`$set`, etc.)
|
|
7
|
+
* returns an object whose keys are the directly modified paths.
|
|
8
|
+
*
|
|
9
|
+
* If there are any top-level keys that don't start with `$`, we assume those
|
|
10
|
+
* will get wrapped in a `$set`. The Mongoose Query is responsible for wrapping
|
|
11
|
+
* top-level keys in `$set`.
|
|
12
|
+
*
|
|
13
|
+
* @param {Object} update
|
|
14
|
+
* @return {Object} modified
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
module.exports = function modifiedPaths(update) {
|
|
18
|
+
const keys = Object.keys(update);
|
|
19
|
+
const res = {};
|
|
20
|
+
|
|
21
|
+
const withoutDollarKeys = {};
|
|
22
|
+
for (const key of keys) {
|
|
23
|
+
if (key.startsWith('$')) {
|
|
24
|
+
_modifiedPaths(update[key], '', res);
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
withoutDollarKeys[key] = update[key];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
_modifiedPaths(withoutDollarKeys, '', res);
|
|
31
|
+
|
|
32
|
+
return res;
|
|
33
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const get = require('../get');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Given an update, move all $set on immutable properties to $setOnInsert.
|
|
7
|
+
* This should only be called for upserts, because $setOnInsert bypasses the
|
|
8
|
+
* strictness check for immutable properties.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
module.exports = function moveImmutableProperties(schema, update, ctx) {
|
|
12
|
+
if (update == null) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const keys = Object.keys(update);
|
|
17
|
+
for (const key of keys) {
|
|
18
|
+
const isDollarKey = key.startsWith('$');
|
|
19
|
+
|
|
20
|
+
if (key === '$set') {
|
|
21
|
+
const updatedPaths = Object.keys(update[key]);
|
|
22
|
+
for (const path of updatedPaths) {
|
|
23
|
+
_walkUpdatePath(schema, update[key], path, update, ctx);
|
|
24
|
+
}
|
|
25
|
+
} else if (!isDollarKey) {
|
|
26
|
+
_walkUpdatePath(schema, update, key, update, ctx);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
function _walkUpdatePath(schema, op, path, update, ctx) {
|
|
33
|
+
const schematype = schema.path(path);
|
|
34
|
+
if (schematype == null) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let immutable = get(schematype, 'options.immutable', null);
|
|
39
|
+
if (immutable == null) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (typeof immutable === 'function') {
|
|
43
|
+
immutable = immutable.call(ctx, ctx);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (!immutable) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
update.$setOnInsert = update.$setOnInsert || {};
|
|
51
|
+
update.$setOnInsert[path] = op[path];
|
|
52
|
+
delete op[path];
|
|
53
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* MongoDB throws an error if there's unused array filters. That is, if `options.arrayFilters` defines
|
|
5
|
+
* a filter, but none of the `update` keys use it. This should be enough to filter out all unused array
|
|
6
|
+
* filters.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
module.exports = function removeUnusedArrayFilters(update, arrayFilters) {
|
|
10
|
+
const updateKeys = Object.keys(update).
|
|
11
|
+
map(key => Object.keys(update[key])).
|
|
12
|
+
reduce((cur, arr) => cur.concat(arr), []);
|
|
13
|
+
return arrayFilters.filter(obj => {
|
|
14
|
+
return _checkSingleFilterKey(obj, updateKeys);
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function _checkSingleFilterKey(arrayFilter, updateKeys) {
|
|
19
|
+
const firstKey = Object.keys(arrayFilter)[0];
|
|
20
|
+
|
|
21
|
+
if (firstKey === '$and' || firstKey === '$or') {
|
|
22
|
+
if (!Array.isArray(arrayFilter[firstKey])) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
return arrayFilter[firstKey].find(filter => _checkSingleFilterKey(filter, updateKeys)) != null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const firstDot = firstKey.indexOf('.');
|
|
29
|
+
const arrayFilterKey = firstDot === -1 ? firstKey : firstKey.slice(0, firstDot);
|
|
30
|
+
|
|
31
|
+
return updateKeys.find(key => key.includes('$[' + arrayFilterKey + ']')) != null;
|
|
32
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const modifiedPaths = require('./modifiedPaths');
|
|
4
|
+
|
|
5
|
+
module.exports = function updatedPathsByArrayFilter(update) {
|
|
6
|
+
if (update == null) {
|
|
7
|
+
return {};
|
|
8
|
+
}
|
|
9
|
+
const updatedPaths = modifiedPaths(update);
|
|
10
|
+
|
|
11
|
+
return Object.keys(updatedPaths).reduce((cur, path) => {
|
|
12
|
+
const matches = path.match(/\$\[[^\]]+\]/g);
|
|
13
|
+
if (matches == null) {
|
|
14
|
+
return cur;
|
|
15
|
+
}
|
|
16
|
+
for (const match of matches) {
|
|
17
|
+
const firstMatch = path.indexOf(match);
|
|
18
|
+
if (firstMatch !== path.lastIndexOf(match)) {
|
|
19
|
+
throw new Error(`Path '${path}' contains the same array filter multiple times`);
|
|
20
|
+
}
|
|
21
|
+
cur[match.substring(2, match.length - 1)] = path.
|
|
22
|
+
substring(0, firstMatch - 1).
|
|
23
|
+
replace(/\$\[[^\]]+\]/g, '0');
|
|
24
|
+
}
|
|
25
|
+
return cur;
|
|
26
|
+
}, {});
|
|
27
|
+
};
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*!
|
|
4
|
+
* Module dependencies.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const ValidationError = require('../error/validation');
|
|
8
|
+
const cleanPositionalOperators = require('./schema/cleanPositionalOperators');
|
|
9
|
+
const flatten = require('./common').flatten;
|
|
10
|
+
const modifiedPaths = require('./common').modifiedPaths;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Applies validators and defaults to update and findOneAndUpdate operations,
|
|
14
|
+
* specifically passing a null doc as `this` to validators and defaults
|
|
15
|
+
*
|
|
16
|
+
* @param {Query} query
|
|
17
|
+
* @param {Schema} schema
|
|
18
|
+
* @param {Object} castedDoc
|
|
19
|
+
* @param {Object} options
|
|
20
|
+
* @method runValidatorsOnUpdate
|
|
21
|
+
* @api private
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
module.exports = function(query, schema, castedDoc, options, callback) {
|
|
25
|
+
const keys = Object.keys(castedDoc || {});
|
|
26
|
+
let updatedKeys = {};
|
|
27
|
+
let updatedValues = {};
|
|
28
|
+
const isPull = {};
|
|
29
|
+
const arrayAtomicUpdates = {};
|
|
30
|
+
const numKeys = keys.length;
|
|
31
|
+
let hasDollarUpdate = false;
|
|
32
|
+
const modified = {};
|
|
33
|
+
let currentUpdate;
|
|
34
|
+
let key;
|
|
35
|
+
let i;
|
|
36
|
+
|
|
37
|
+
for (i = 0; i < numKeys; ++i) {
|
|
38
|
+
if (keys[i].startsWith('$')) {
|
|
39
|
+
hasDollarUpdate = true;
|
|
40
|
+
if (keys[i] === '$push' || keys[i] === '$addToSet') {
|
|
41
|
+
const _keys = Object.keys(castedDoc[keys[i]]);
|
|
42
|
+
for (let ii = 0; ii < _keys.length; ++ii) {
|
|
43
|
+
currentUpdate = castedDoc[keys[i]][_keys[ii]];
|
|
44
|
+
if (currentUpdate && currentUpdate.$each) {
|
|
45
|
+
arrayAtomicUpdates[_keys[ii]] = (arrayAtomicUpdates[_keys[ii]] || []).
|
|
46
|
+
concat(currentUpdate.$each);
|
|
47
|
+
} else {
|
|
48
|
+
arrayAtomicUpdates[_keys[ii]] = (arrayAtomicUpdates[_keys[ii]] || []).
|
|
49
|
+
concat([currentUpdate]);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
modifiedPaths(castedDoc[keys[i]], '', modified);
|
|
55
|
+
const flat = flatten(castedDoc[keys[i]], null, null, schema);
|
|
56
|
+
const paths = Object.keys(flat);
|
|
57
|
+
const numPaths = paths.length;
|
|
58
|
+
for (let j = 0; j < numPaths; ++j) {
|
|
59
|
+
const updatedPath = cleanPositionalOperators(paths[j]);
|
|
60
|
+
key = keys[i];
|
|
61
|
+
// With `$pull` we might flatten `$in`. Skip stuff nested under `$in`
|
|
62
|
+
// for the rest of the logic, it will get handled later.
|
|
63
|
+
if (updatedPath.includes('$')) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (key === '$set' || key === '$setOnInsert' ||
|
|
67
|
+
key === '$pull' || key === '$pullAll') {
|
|
68
|
+
updatedValues[updatedPath] = flat[paths[j]];
|
|
69
|
+
isPull[updatedPath] = key === '$pull' || key === '$pullAll';
|
|
70
|
+
} else if (key === '$unset') {
|
|
71
|
+
updatedValues[updatedPath] = undefined;
|
|
72
|
+
}
|
|
73
|
+
updatedKeys[updatedPath] = true;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (!hasDollarUpdate) {
|
|
79
|
+
modifiedPaths(castedDoc, '', modified);
|
|
80
|
+
updatedValues = flatten(castedDoc, null, null, schema);
|
|
81
|
+
updatedKeys = Object.keys(updatedValues);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const updates = Object.keys(updatedValues);
|
|
85
|
+
const numUpdates = updates.length;
|
|
86
|
+
const validatorsToExecute = [];
|
|
87
|
+
const validationErrors = [];
|
|
88
|
+
|
|
89
|
+
const alreadyValidated = [];
|
|
90
|
+
|
|
91
|
+
const context = query;
|
|
92
|
+
function iter(i, v) {
|
|
93
|
+
const schemaPath = schema._getSchema(updates[i]);
|
|
94
|
+
if (schemaPath == null) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (schemaPath.instance === 'Mixed' && schemaPath.path !== updates[i]) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (v && Array.isArray(v.$in)) {
|
|
102
|
+
v.$in.forEach((v, i) => {
|
|
103
|
+
validatorsToExecute.push(function(callback) {
|
|
104
|
+
schemaPath.doValidate(
|
|
105
|
+
v,
|
|
106
|
+
function(err) {
|
|
107
|
+
if (err) {
|
|
108
|
+
err.path = updates[i] + '.$in.' + i;
|
|
109
|
+
validationErrors.push(err);
|
|
110
|
+
}
|
|
111
|
+
callback(null);
|
|
112
|
+
},
|
|
113
|
+
context,
|
|
114
|
+
{ updateValidator: true });
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
} else {
|
|
118
|
+
if (isPull[updates[i]] &&
|
|
119
|
+
schemaPath.$isMongooseArray) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (schemaPath.$isMongooseDocumentArrayElement && v != null && v.$__ != null) {
|
|
124
|
+
alreadyValidated.push(updates[i]);
|
|
125
|
+
validatorsToExecute.push(function(callback) {
|
|
126
|
+
schemaPath.doValidate(v, function(err) {
|
|
127
|
+
if (err) {
|
|
128
|
+
if (err.errors) {
|
|
129
|
+
for (const key of Object.keys(err.errors)) {
|
|
130
|
+
const _err = err.errors[key];
|
|
131
|
+
_err.path = updates[i] + '.' + key;
|
|
132
|
+
validationErrors.push(_err);
|
|
133
|
+
}
|
|
134
|
+
} else {
|
|
135
|
+
err.path = updates[i];
|
|
136
|
+
validationErrors.push(err);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return callback(null);
|
|
141
|
+
}, context, { updateValidator: true });
|
|
142
|
+
});
|
|
143
|
+
} else {
|
|
144
|
+
validatorsToExecute.push(function(callback) {
|
|
145
|
+
for (const path of alreadyValidated) {
|
|
146
|
+
if (updates[i].startsWith(path + '.')) {
|
|
147
|
+
return callback(null);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
schemaPath.doValidate(v, function(err) {
|
|
152
|
+
if (schemaPath.schema != null &&
|
|
153
|
+
schemaPath.schema.options.storeSubdocValidationError === false &&
|
|
154
|
+
err instanceof ValidationError) {
|
|
155
|
+
return callback(null);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (err) {
|
|
159
|
+
err.path = updates[i];
|
|
160
|
+
validationErrors.push(err);
|
|
161
|
+
}
|
|
162
|
+
callback(null);
|
|
163
|
+
}, context, { updateValidator: true });
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
for (i = 0; i < numUpdates; ++i) {
|
|
169
|
+
iter(i, updatedValues[updates[i]]);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const arrayUpdates = Object.keys(arrayAtomicUpdates);
|
|
173
|
+
for (const arrayUpdate of arrayUpdates) {
|
|
174
|
+
let schemaPath = schema._getSchema(arrayUpdate);
|
|
175
|
+
if (schemaPath && schemaPath.$isMongooseDocumentArray) {
|
|
176
|
+
validatorsToExecute.push(function(callback) {
|
|
177
|
+
schemaPath.doValidate(
|
|
178
|
+
arrayAtomicUpdates[arrayUpdate],
|
|
179
|
+
getValidationCallback(arrayUpdate, validationErrors, callback),
|
|
180
|
+
options && options.context === 'query' ? query : null);
|
|
181
|
+
});
|
|
182
|
+
} else {
|
|
183
|
+
schemaPath = schema._getSchema(arrayUpdate + '.0');
|
|
184
|
+
for (const atomicUpdate of arrayAtomicUpdates[arrayUpdate]) {
|
|
185
|
+
validatorsToExecute.push(function(callback) {
|
|
186
|
+
schemaPath.doValidate(
|
|
187
|
+
atomicUpdate,
|
|
188
|
+
getValidationCallback(arrayUpdate, validationErrors, callback),
|
|
189
|
+
options && options.context === 'query' ? query : null,
|
|
190
|
+
{ updateValidator: true });
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (callback != null) {
|
|
197
|
+
let numValidators = validatorsToExecute.length;
|
|
198
|
+
if (numValidators === 0) {
|
|
199
|
+
return _done(callback);
|
|
200
|
+
}
|
|
201
|
+
for (const validator of validatorsToExecute) {
|
|
202
|
+
validator(function() {
|
|
203
|
+
if (--numValidators <= 0) {
|
|
204
|
+
_done(callback);
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return function(callback) {
|
|
213
|
+
let numValidators = validatorsToExecute.length;
|
|
214
|
+
if (numValidators === 0) {
|
|
215
|
+
return _done(callback);
|
|
216
|
+
}
|
|
217
|
+
for (const validator of validatorsToExecute) {
|
|
218
|
+
validator(function() {
|
|
219
|
+
if (--numValidators <= 0) {
|
|
220
|
+
_done(callback);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
function _done(callback) {
|
|
227
|
+
if (validationErrors.length) {
|
|
228
|
+
const err = new ValidationError(null);
|
|
229
|
+
|
|
230
|
+
for (const validationError of validationErrors) {
|
|
231
|
+
err.addError(validationError.path, validationError);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return callback(err);
|
|
235
|
+
}
|
|
236
|
+
callback(null);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function getValidationCallback(arrayUpdate, validationErrors, callback) {
|
|
240
|
+
return function(err) {
|
|
241
|
+
if (err) {
|
|
242
|
+
err.path = arrayUpdate;
|
|
243
|
+
validationErrors.push(err);
|
|
244
|
+
}
|
|
245
|
+
callback(null);
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
|