@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
package/lib/utils.js
ADDED
|
@@ -0,0 +1,1011 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*!
|
|
4
|
+
* Module dependencies.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const UUID = require('bson').UUID;
|
|
8
|
+
const ms = require('ms');
|
|
9
|
+
const mpath = require('mpath');
|
|
10
|
+
const ObjectId = require('./types/objectid');
|
|
11
|
+
const PopulateOptions = require('./options/PopulateOptions');
|
|
12
|
+
const clone = require('./helpers/clone');
|
|
13
|
+
const immediate = require('./helpers/immediate');
|
|
14
|
+
const isObject = require('./helpers/isObject');
|
|
15
|
+
const isMongooseArray = require('./types/array/isMongooseArray');
|
|
16
|
+
const isMongooseDocumentArray = require('./types/DocumentArray/isMongooseDocumentArray');
|
|
17
|
+
const isBsonType = require('./helpers/isBsonType');
|
|
18
|
+
const getFunctionName = require('./helpers/getFunctionName');
|
|
19
|
+
const isMongooseObject = require('./helpers/isMongooseObject');
|
|
20
|
+
const promiseOrCallback = require('./helpers/promiseOrCallback');
|
|
21
|
+
const schemaMerge = require('./helpers/schema/merge');
|
|
22
|
+
const specialProperties = require('./helpers/specialProperties');
|
|
23
|
+
const { trustedSymbol } = require('./helpers/query/trusted');
|
|
24
|
+
|
|
25
|
+
let Document;
|
|
26
|
+
|
|
27
|
+
exports.specialProperties = specialProperties;
|
|
28
|
+
|
|
29
|
+
exports.isMongooseArray = isMongooseArray.isMongooseArray;
|
|
30
|
+
exports.isMongooseDocumentArray = isMongooseDocumentArray.isMongooseDocumentArray;
|
|
31
|
+
exports.registerMongooseArray = isMongooseArray.registerMongooseArray;
|
|
32
|
+
exports.registerMongooseDocumentArray = isMongooseDocumentArray.registerMongooseDocumentArray;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Produces a collection name from model `name`. By default, just returns
|
|
36
|
+
* the model name
|
|
37
|
+
*
|
|
38
|
+
* @param {String} name a model name
|
|
39
|
+
* @param {Function} pluralize function that pluralizes the collection name
|
|
40
|
+
* @return {String} a collection name
|
|
41
|
+
* @api private
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
exports.toCollectionName = function(name, pluralize) {
|
|
45
|
+
if (name === 'system.profile') {
|
|
46
|
+
return name;
|
|
47
|
+
}
|
|
48
|
+
if (name === 'system.indexes') {
|
|
49
|
+
return name;
|
|
50
|
+
}
|
|
51
|
+
if (typeof pluralize === 'function') {
|
|
52
|
+
return pluralize(name);
|
|
53
|
+
}
|
|
54
|
+
return name;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Determines if `a` and `b` are deep equal.
|
|
59
|
+
*
|
|
60
|
+
* Modified from node/lib/assert.js
|
|
61
|
+
*
|
|
62
|
+
* @param {any} a a value to compare to `b`
|
|
63
|
+
* @param {any} b a value to compare to `a`
|
|
64
|
+
* @return {Boolean}
|
|
65
|
+
* @api private
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
exports.deepEqual = function deepEqual(a, b) {
|
|
69
|
+
if (a === b) {
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (typeof a !== 'object' || typeof b !== 'object') {
|
|
74
|
+
return a === b;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (a instanceof Date && b instanceof Date) {
|
|
78
|
+
return a.getTime() === b.getTime();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if ((isBsonType(a, 'ObjectID') && isBsonType(b, 'ObjectID')) ||
|
|
82
|
+
(isBsonType(a, 'Decimal128') && isBsonType(b, 'Decimal128'))) {
|
|
83
|
+
return a.toString() === b.toString();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (a instanceof RegExp && b instanceof RegExp) {
|
|
87
|
+
return a.source === b.source &&
|
|
88
|
+
a.ignoreCase === b.ignoreCase &&
|
|
89
|
+
a.multiline === b.multiline &&
|
|
90
|
+
a.global === b.global &&
|
|
91
|
+
a.dotAll === b.dotAll &&
|
|
92
|
+
a.unicode === b.unicode &&
|
|
93
|
+
a.sticky === b.sticky &&
|
|
94
|
+
a.hasIndices === b.hasIndices;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (a == null || b == null) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (a.prototype !== b.prototype) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (a instanceof Map || b instanceof Map) {
|
|
106
|
+
if (!(a instanceof Map) || !(b instanceof Map)) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
return deepEqual(Array.from(a.keys()), Array.from(b.keys())) &&
|
|
110
|
+
deepEqual(Array.from(a.values()), Array.from(b.values()));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Handle MongooseNumbers
|
|
114
|
+
if (a instanceof Number && b instanceof Number) {
|
|
115
|
+
return a.valueOf() === b.valueOf();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (Buffer.isBuffer(a)) {
|
|
119
|
+
return exports.buffer.areEqual(a, b);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
123
|
+
if (!Array.isArray(a) || !Array.isArray(b)) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
const len = a.length;
|
|
127
|
+
if (len !== b.length) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
for (let i = 0; i < len; ++i) {
|
|
131
|
+
if (!deepEqual(a[i], b[i])) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (a.$__ != null) {
|
|
139
|
+
a = a._doc;
|
|
140
|
+
} else if (isMongooseObject(a)) {
|
|
141
|
+
a = a.toObject();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (b.$__ != null) {
|
|
145
|
+
b = b._doc;
|
|
146
|
+
} else if (isMongooseObject(b)) {
|
|
147
|
+
b = b.toObject();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const ka = Object.keys(a);
|
|
151
|
+
const kb = Object.keys(b);
|
|
152
|
+
const kaLength = ka.length;
|
|
153
|
+
|
|
154
|
+
// having the same number of owned properties (keys incorporates
|
|
155
|
+
// hasOwnProperty)
|
|
156
|
+
if (kaLength !== kb.length) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// ~~~cheap key test
|
|
161
|
+
for (let i = kaLength - 1; i >= 0; i--) {
|
|
162
|
+
if (ka[i] !== kb[i]) {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// equivalent values for every corresponding key, and
|
|
168
|
+
// ~~~possibly expensive deep test
|
|
169
|
+
for (const key of ka) {
|
|
170
|
+
if (!deepEqual(a[key], b[key])) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return true;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Get the last element of an array
|
|
180
|
+
* @param {Array} arr
|
|
181
|
+
*/
|
|
182
|
+
|
|
183
|
+
exports.last = function(arr) {
|
|
184
|
+
if (arr.length > 0) {
|
|
185
|
+
return arr[arr.length - 1];
|
|
186
|
+
}
|
|
187
|
+
return void 0;
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
exports.clone = clone;
|
|
191
|
+
|
|
192
|
+
/*!
|
|
193
|
+
* ignore
|
|
194
|
+
*/
|
|
195
|
+
|
|
196
|
+
exports.promiseOrCallback = promiseOrCallback;
|
|
197
|
+
|
|
198
|
+
/*!
|
|
199
|
+
* ignore
|
|
200
|
+
*/
|
|
201
|
+
|
|
202
|
+
exports.cloneArrays = function cloneArrays(arr) {
|
|
203
|
+
if (!Array.isArray(arr)) {
|
|
204
|
+
return arr;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return arr.map(el => exports.cloneArrays(el));
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
/*!
|
|
211
|
+
* ignore
|
|
212
|
+
*/
|
|
213
|
+
|
|
214
|
+
exports.omit = function omit(obj, keys) {
|
|
215
|
+
if (keys == null) {
|
|
216
|
+
return Object.assign({}, obj);
|
|
217
|
+
}
|
|
218
|
+
if (!Array.isArray(keys)) {
|
|
219
|
+
keys = [keys];
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const ret = Object.assign({}, obj);
|
|
223
|
+
for (const key of keys) {
|
|
224
|
+
delete ret[key];
|
|
225
|
+
}
|
|
226
|
+
return ret;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Shallow copies defaults into options.
|
|
232
|
+
*
|
|
233
|
+
* @param {Object} defaults
|
|
234
|
+
* @param {Object} [options]
|
|
235
|
+
* @return {Object} the merged object
|
|
236
|
+
* @api private
|
|
237
|
+
*/
|
|
238
|
+
|
|
239
|
+
exports.options = function(defaults, options) {
|
|
240
|
+
const keys = Object.keys(defaults);
|
|
241
|
+
let i = keys.length;
|
|
242
|
+
let k;
|
|
243
|
+
|
|
244
|
+
options = options || {};
|
|
245
|
+
|
|
246
|
+
while (i--) {
|
|
247
|
+
k = keys[i];
|
|
248
|
+
if (!(k in options)) {
|
|
249
|
+
options[k] = defaults[k];
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return options;
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Merges `from` into `to` without overwriting existing properties.
|
|
258
|
+
*
|
|
259
|
+
* @param {Object} to
|
|
260
|
+
* @param {Object} from
|
|
261
|
+
* @param {Object} [options]
|
|
262
|
+
* @param {String} [path]
|
|
263
|
+
* @api private
|
|
264
|
+
*/
|
|
265
|
+
|
|
266
|
+
exports.merge = function merge(to, from, options, path) {
|
|
267
|
+
options = options || {};
|
|
268
|
+
|
|
269
|
+
const keys = Object.keys(from);
|
|
270
|
+
let i = 0;
|
|
271
|
+
const len = keys.length;
|
|
272
|
+
let key;
|
|
273
|
+
|
|
274
|
+
if (from[trustedSymbol]) {
|
|
275
|
+
to[trustedSymbol] = from[trustedSymbol];
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
path = path || '';
|
|
279
|
+
const omitNested = options.omitNested || {};
|
|
280
|
+
|
|
281
|
+
while (i < len) {
|
|
282
|
+
key = keys[i++];
|
|
283
|
+
if (options.omit && options.omit[key]) {
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
if (omitNested[path]) {
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
if (specialProperties.has(key)) {
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
if (to[key] == null) {
|
|
293
|
+
to[key] = from[key];
|
|
294
|
+
} else if (exports.isObject(from[key])) {
|
|
295
|
+
if (!exports.isObject(to[key])) {
|
|
296
|
+
to[key] = {};
|
|
297
|
+
}
|
|
298
|
+
if (from[key] != null) {
|
|
299
|
+
// Skip merging schemas if we're creating a discriminator schema and
|
|
300
|
+
// base schema has a given path as a single nested but discriminator schema
|
|
301
|
+
// has the path as a document array, or vice versa (gh-9534)
|
|
302
|
+
if (options.isDiscriminatorSchemaMerge &&
|
|
303
|
+
(from[key].$isSingleNested && to[key].$isMongooseDocumentArray) ||
|
|
304
|
+
(from[key].$isMongooseDocumentArray && to[key].$isSingleNested)) {
|
|
305
|
+
continue;
|
|
306
|
+
} else if (from[key].instanceOfSchema) {
|
|
307
|
+
if (to[key].instanceOfSchema) {
|
|
308
|
+
schemaMerge(to[key], from[key].clone(), options.isDiscriminatorSchemaMerge);
|
|
309
|
+
} else {
|
|
310
|
+
to[key] = from[key].clone();
|
|
311
|
+
}
|
|
312
|
+
continue;
|
|
313
|
+
} else if (isBsonType(from[key], 'ObjectID')) {
|
|
314
|
+
to[key] = new ObjectId(from[key]);
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
merge(to[key], from[key], options, path ? path + '.' + key : key);
|
|
319
|
+
} else if (options.overwrite) {
|
|
320
|
+
to[key] = from[key];
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Applies toObject recursively.
|
|
327
|
+
*
|
|
328
|
+
* @param {Document|Array|Object} obj
|
|
329
|
+
* @return {Object}
|
|
330
|
+
* @api private
|
|
331
|
+
*/
|
|
332
|
+
|
|
333
|
+
exports.toObject = function toObject(obj) {
|
|
334
|
+
Document || (Document = require('./document'));
|
|
335
|
+
let ret;
|
|
336
|
+
|
|
337
|
+
if (obj == null) {
|
|
338
|
+
return obj;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (obj instanceof Document) {
|
|
342
|
+
return obj.toObject();
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (Array.isArray(obj)) {
|
|
346
|
+
ret = [];
|
|
347
|
+
|
|
348
|
+
for (const doc of obj) {
|
|
349
|
+
ret.push(toObject(doc));
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return ret;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (exports.isPOJO(obj)) {
|
|
356
|
+
ret = {};
|
|
357
|
+
|
|
358
|
+
if (obj[trustedSymbol]) {
|
|
359
|
+
ret[trustedSymbol] = obj[trustedSymbol];
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
for (const k of Object.keys(obj)) {
|
|
363
|
+
if (specialProperties.has(k)) {
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
ret[k] = toObject(obj[k]);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
return ret;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
return obj;
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
exports.isObject = isObject;
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Determines if `arg` is a plain old JavaScript object (POJO). Specifically,
|
|
379
|
+
* `arg` must be an object but not an instance of any special class, like String,
|
|
380
|
+
* ObjectId, etc.
|
|
381
|
+
*
|
|
382
|
+
* `Object.getPrototypeOf()` is part of ES5: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf
|
|
383
|
+
*
|
|
384
|
+
* @param {Object|Array|String|Function|RegExp|any} arg
|
|
385
|
+
* @api private
|
|
386
|
+
* @return {Boolean}
|
|
387
|
+
*/
|
|
388
|
+
|
|
389
|
+
exports.isPOJO = function isPOJO(arg) {
|
|
390
|
+
if (arg == null || typeof arg !== 'object') {
|
|
391
|
+
return false;
|
|
392
|
+
}
|
|
393
|
+
const proto = Object.getPrototypeOf(arg);
|
|
394
|
+
// Prototype may be null if you used `Object.create(null)`
|
|
395
|
+
// Checking `proto`'s constructor is safe because `getPrototypeOf()`
|
|
396
|
+
// explicitly crosses the boundary from object data to object metadata
|
|
397
|
+
return !proto || proto.constructor.name === 'Object';
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Determines if `arg` is an object that isn't an instance of a built-in value
|
|
402
|
+
* class, like Array, Buffer, ObjectId, etc.
|
|
403
|
+
* @param {Any} val
|
|
404
|
+
*/
|
|
405
|
+
|
|
406
|
+
exports.isNonBuiltinObject = function isNonBuiltinObject(val) {
|
|
407
|
+
return typeof val === 'object' &&
|
|
408
|
+
!exports.isNativeObject(val) &&
|
|
409
|
+
!exports.isMongooseType(val) &&
|
|
410
|
+
!(val instanceof UUID) &&
|
|
411
|
+
val != null;
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Determines if `obj` is a built-in object like an array, date, boolean,
|
|
416
|
+
* etc.
|
|
417
|
+
* @param {Any} arg
|
|
418
|
+
*/
|
|
419
|
+
|
|
420
|
+
exports.isNativeObject = function(arg) {
|
|
421
|
+
return Array.isArray(arg) ||
|
|
422
|
+
arg instanceof Date ||
|
|
423
|
+
arg instanceof Boolean ||
|
|
424
|
+
arg instanceof Number ||
|
|
425
|
+
arg instanceof String;
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Determines if `val` is an object that has no own keys
|
|
430
|
+
* @param {Any} val
|
|
431
|
+
*/
|
|
432
|
+
|
|
433
|
+
exports.isEmptyObject = function(val) {
|
|
434
|
+
return val != null &&
|
|
435
|
+
typeof val === 'object' &&
|
|
436
|
+
Object.keys(val).length === 0;
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Search if `obj` or any POJOs nested underneath `obj` has a property named
|
|
441
|
+
* `key`
|
|
442
|
+
* @param {Object} obj
|
|
443
|
+
* @param {String} key
|
|
444
|
+
*/
|
|
445
|
+
|
|
446
|
+
exports.hasKey = function hasKey(obj, key) {
|
|
447
|
+
const props = Object.keys(obj);
|
|
448
|
+
for (const prop of props) {
|
|
449
|
+
if (prop === key) {
|
|
450
|
+
return true;
|
|
451
|
+
}
|
|
452
|
+
if (exports.isPOJO(obj[prop]) && exports.hasKey(obj[prop], key)) {
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return false;
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* process.nextTick helper.
|
|
461
|
+
*
|
|
462
|
+
* Wraps `callback` in a try/catch + nextTick.
|
|
463
|
+
*
|
|
464
|
+
* node-mongodb-native has a habit of state corruption when an error is immediately thrown from within a collection callback.
|
|
465
|
+
*
|
|
466
|
+
* @param {Function} callback
|
|
467
|
+
* @api private
|
|
468
|
+
*/
|
|
469
|
+
|
|
470
|
+
exports.tick = function tick(callback) {
|
|
471
|
+
if (typeof callback !== 'function') {
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
return function() {
|
|
475
|
+
try {
|
|
476
|
+
callback.apply(this, arguments);
|
|
477
|
+
} catch (err) {
|
|
478
|
+
// only nextTick on err to get out of
|
|
479
|
+
// the event loop and avoid state corruption.
|
|
480
|
+
immediate(function() {
|
|
481
|
+
throw err;
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
};
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Returns true if `v` is an object that can be serialized as a primitive in
|
|
489
|
+
* MongoDB
|
|
490
|
+
* @param {Any} v
|
|
491
|
+
*/
|
|
492
|
+
|
|
493
|
+
exports.isMongooseType = function(v) {
|
|
494
|
+
return isBsonType(v, 'ObjectID') || isBsonType(v, 'Decimal128') || v instanceof Buffer;
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
exports.isMongooseObject = isMongooseObject;
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Converts `expires` options of index objects to `expiresAfterSeconds` options for MongoDB.
|
|
501
|
+
*
|
|
502
|
+
* @param {Object} object
|
|
503
|
+
* @api private
|
|
504
|
+
*/
|
|
505
|
+
|
|
506
|
+
exports.expires = function expires(object) {
|
|
507
|
+
if (!(object && object.constructor.name === 'Object')) {
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
if (!('expires' in object)) {
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
object.expireAfterSeconds = (typeof object.expires !== 'string')
|
|
515
|
+
? object.expires
|
|
516
|
+
: Math.round(ms(object.expires) / 1000);
|
|
517
|
+
delete object.expires;
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* populate helper
|
|
522
|
+
* @param {String} path
|
|
523
|
+
* @param {String} select
|
|
524
|
+
* @param {Model} model
|
|
525
|
+
* @param {Object} match
|
|
526
|
+
* @param {Object} options
|
|
527
|
+
* @param {Any} subPopulate
|
|
528
|
+
* @param {Boolean} justOne
|
|
529
|
+
* @param {Boolean} count
|
|
530
|
+
*/
|
|
531
|
+
|
|
532
|
+
exports.populate = function populate(path, select, model, match, options, subPopulate, justOne, count) {
|
|
533
|
+
// might have passed an object specifying all arguments
|
|
534
|
+
let obj = null;
|
|
535
|
+
if (arguments.length === 1) {
|
|
536
|
+
if (path instanceof PopulateOptions) {
|
|
537
|
+
// If reusing old populate docs, avoid reusing `_docs` because that may
|
|
538
|
+
// lead to bugs and memory leaks. See gh-11641
|
|
539
|
+
path._docs = [];
|
|
540
|
+
path._childDocs = [];
|
|
541
|
+
return [path];
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
if (Array.isArray(path)) {
|
|
545
|
+
const singles = makeSingles(path);
|
|
546
|
+
return singles.map(o => exports.populate(o)[0]);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
if (exports.isObject(path)) {
|
|
550
|
+
obj = Object.assign({}, path);
|
|
551
|
+
} else {
|
|
552
|
+
obj = { path: path };
|
|
553
|
+
}
|
|
554
|
+
} else if (typeof model === 'object') {
|
|
555
|
+
obj = {
|
|
556
|
+
path: path,
|
|
557
|
+
select: select,
|
|
558
|
+
match: model,
|
|
559
|
+
options: match
|
|
560
|
+
};
|
|
561
|
+
} else {
|
|
562
|
+
obj = {
|
|
563
|
+
path: path,
|
|
564
|
+
select: select,
|
|
565
|
+
model: model,
|
|
566
|
+
match: match,
|
|
567
|
+
options: options,
|
|
568
|
+
populate: subPopulate,
|
|
569
|
+
justOne: justOne,
|
|
570
|
+
count: count
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
if (typeof obj.path !== 'string') {
|
|
575
|
+
throw new TypeError('utils.populate: invalid path. Expected string. Got typeof `' + typeof path + '`');
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
return _populateObj(obj);
|
|
579
|
+
|
|
580
|
+
// The order of select/conditions args is opposite Model.find but
|
|
581
|
+
// necessary to keep backward compatibility (select could be
|
|
582
|
+
// an array, string, or object literal).
|
|
583
|
+
function makeSingles(arr) {
|
|
584
|
+
const ret = [];
|
|
585
|
+
arr.forEach(function(obj) {
|
|
586
|
+
if (/[\s]/.test(obj.path)) {
|
|
587
|
+
const paths = obj.path.split(' ');
|
|
588
|
+
paths.forEach(function(p) {
|
|
589
|
+
const copy = Object.assign({}, obj);
|
|
590
|
+
copy.path = p;
|
|
591
|
+
ret.push(copy);
|
|
592
|
+
});
|
|
593
|
+
} else {
|
|
594
|
+
ret.push(obj);
|
|
595
|
+
}
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
return ret;
|
|
599
|
+
}
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
function _populateObj(obj) {
|
|
603
|
+
if (Array.isArray(obj.populate)) {
|
|
604
|
+
const ret = [];
|
|
605
|
+
obj.populate.forEach(function(obj) {
|
|
606
|
+
if (/[\s]/.test(obj.path)) {
|
|
607
|
+
const copy = Object.assign({}, obj);
|
|
608
|
+
const paths = copy.path.split(' ');
|
|
609
|
+
paths.forEach(function(p) {
|
|
610
|
+
copy.path = p;
|
|
611
|
+
ret.push(exports.populate(copy)[0]);
|
|
612
|
+
});
|
|
613
|
+
} else {
|
|
614
|
+
ret.push(exports.populate(obj)[0]);
|
|
615
|
+
}
|
|
616
|
+
});
|
|
617
|
+
obj.populate = exports.populate(ret);
|
|
618
|
+
} else if (obj.populate != null && typeof obj.populate === 'object') {
|
|
619
|
+
obj.populate = exports.populate(obj.populate);
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
const ret = [];
|
|
623
|
+
const paths = obj.path.split(' ');
|
|
624
|
+
if (obj.options != null) {
|
|
625
|
+
obj.options = exports.clone(obj.options);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
for (const path of paths) {
|
|
629
|
+
ret.push(new PopulateOptions(Object.assign({}, obj, { path: path })));
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
return ret;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/**
|
|
636
|
+
* Return the value of `obj` at the given `path`.
|
|
637
|
+
*
|
|
638
|
+
* @param {String} path
|
|
639
|
+
* @param {Object} obj
|
|
640
|
+
* @param {Any} map
|
|
641
|
+
*/
|
|
642
|
+
|
|
643
|
+
exports.getValue = function(path, obj, map) {
|
|
644
|
+
return mpath.get(path, obj, '_doc', map);
|
|
645
|
+
};
|
|
646
|
+
|
|
647
|
+
/**
|
|
648
|
+
* Sets the value of `obj` at the given `path`.
|
|
649
|
+
*
|
|
650
|
+
* @param {String} path
|
|
651
|
+
* @param {Anything} val
|
|
652
|
+
* @param {Object} obj
|
|
653
|
+
* @param {Any} map
|
|
654
|
+
* @param {Any} _copying
|
|
655
|
+
*/
|
|
656
|
+
|
|
657
|
+
exports.setValue = function(path, val, obj, map, _copying) {
|
|
658
|
+
mpath.set(path, val, obj, '_doc', map, _copying);
|
|
659
|
+
};
|
|
660
|
+
|
|
661
|
+
/**
|
|
662
|
+
* Returns an array of values from object `o`.
|
|
663
|
+
*
|
|
664
|
+
* @param {Object} o
|
|
665
|
+
* @return {Array}
|
|
666
|
+
* @api private
|
|
667
|
+
*/
|
|
668
|
+
|
|
669
|
+
exports.object = {};
|
|
670
|
+
exports.object.vals = function vals(o) {
|
|
671
|
+
const keys = Object.keys(o);
|
|
672
|
+
let i = keys.length;
|
|
673
|
+
const ret = [];
|
|
674
|
+
|
|
675
|
+
while (i--) {
|
|
676
|
+
ret.push(o[keys[i]]);
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
return ret;
|
|
680
|
+
};
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* @see exports.options
|
|
684
|
+
*/
|
|
685
|
+
|
|
686
|
+
exports.object.shallowCopy = exports.options;
|
|
687
|
+
|
|
688
|
+
const hop = Object.prototype.hasOwnProperty;
|
|
689
|
+
|
|
690
|
+
/**
|
|
691
|
+
* Safer helper for hasOwnProperty checks
|
|
692
|
+
*
|
|
693
|
+
* @param {Object} obj
|
|
694
|
+
* @param {String} prop
|
|
695
|
+
*/
|
|
696
|
+
|
|
697
|
+
exports.object.hasOwnProperty = function(obj, prop) {
|
|
698
|
+
return hop.call(obj, prop);
|
|
699
|
+
};
|
|
700
|
+
|
|
701
|
+
/**
|
|
702
|
+
* Determine if `val` is null or undefined
|
|
703
|
+
*
|
|
704
|
+
* @param {Any} val
|
|
705
|
+
* @return {Boolean}
|
|
706
|
+
*/
|
|
707
|
+
|
|
708
|
+
exports.isNullOrUndefined = function(val) {
|
|
709
|
+
return val === null || val === undefined;
|
|
710
|
+
};
|
|
711
|
+
|
|
712
|
+
/*!
|
|
713
|
+
* ignore
|
|
714
|
+
*/
|
|
715
|
+
|
|
716
|
+
exports.array = {};
|
|
717
|
+
|
|
718
|
+
/**
|
|
719
|
+
* Flattens an array.
|
|
720
|
+
*
|
|
721
|
+
* [ 1, [ 2, 3, [4] ]] -> [1,2,3,4]
|
|
722
|
+
*
|
|
723
|
+
* @param {Array} arr
|
|
724
|
+
* @param {Function} [filter] If passed, will be invoked with each item in the array. If `filter` returns a falsy value, the item will not be included in the results.
|
|
725
|
+
* @param {Array} ret
|
|
726
|
+
* @return {Array}
|
|
727
|
+
* @api private
|
|
728
|
+
*/
|
|
729
|
+
|
|
730
|
+
exports.array.flatten = function flatten(arr, filter, ret) {
|
|
731
|
+
ret || (ret = []);
|
|
732
|
+
|
|
733
|
+
arr.forEach(function(item) {
|
|
734
|
+
if (Array.isArray(item)) {
|
|
735
|
+
flatten(item, filter, ret);
|
|
736
|
+
} else {
|
|
737
|
+
if (!filter || filter(item)) {
|
|
738
|
+
ret.push(item);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
});
|
|
742
|
+
|
|
743
|
+
return ret;
|
|
744
|
+
};
|
|
745
|
+
|
|
746
|
+
/*!
|
|
747
|
+
* ignore
|
|
748
|
+
*/
|
|
749
|
+
|
|
750
|
+
const _hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
751
|
+
|
|
752
|
+
exports.hasUserDefinedProperty = function(obj, key) {
|
|
753
|
+
if (obj == null) {
|
|
754
|
+
return false;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
if (Array.isArray(key)) {
|
|
758
|
+
for (const k of key) {
|
|
759
|
+
if (exports.hasUserDefinedProperty(obj, k)) {
|
|
760
|
+
return true;
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
return false;
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
if (_hasOwnProperty.call(obj, key)) {
|
|
767
|
+
return true;
|
|
768
|
+
}
|
|
769
|
+
if (typeof obj === 'object' && key in obj) {
|
|
770
|
+
const v = obj[key];
|
|
771
|
+
return v !== Object.prototype[key] && v !== Array.prototype[key];
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
return false;
|
|
775
|
+
};
|
|
776
|
+
|
|
777
|
+
/*!
|
|
778
|
+
* ignore
|
|
779
|
+
*/
|
|
780
|
+
|
|
781
|
+
const MAX_ARRAY_INDEX = Math.pow(2, 32) - 1;
|
|
782
|
+
|
|
783
|
+
exports.isArrayIndex = function(val) {
|
|
784
|
+
if (typeof val === 'number') {
|
|
785
|
+
return val >= 0 && val <= MAX_ARRAY_INDEX;
|
|
786
|
+
}
|
|
787
|
+
if (typeof val === 'string') {
|
|
788
|
+
if (!/^\d+$/.test(val)) {
|
|
789
|
+
return false;
|
|
790
|
+
}
|
|
791
|
+
val = +val;
|
|
792
|
+
return val >= 0 && val <= MAX_ARRAY_INDEX;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
return false;
|
|
796
|
+
};
|
|
797
|
+
|
|
798
|
+
/**
|
|
799
|
+
* Removes duplicate values from an array
|
|
800
|
+
*
|
|
801
|
+
* [1, 2, 3, 3, 5] => [1, 2, 3, 5]
|
|
802
|
+
* [ ObjectId("550988ba0c19d57f697dc45e"), ObjectId("550988ba0c19d57f697dc45e") ]
|
|
803
|
+
* => [ObjectId("550988ba0c19d57f697dc45e")]
|
|
804
|
+
*
|
|
805
|
+
* @param {Array} arr
|
|
806
|
+
* @return {Array}
|
|
807
|
+
* @api private
|
|
808
|
+
*/
|
|
809
|
+
|
|
810
|
+
exports.array.unique = function(arr) {
|
|
811
|
+
const primitives = new Set();
|
|
812
|
+
const ids = new Set();
|
|
813
|
+
const ret = [];
|
|
814
|
+
|
|
815
|
+
for (const item of arr) {
|
|
816
|
+
if (typeof item === 'number' || typeof item === 'string' || item == null) {
|
|
817
|
+
if (primitives.has(item)) {
|
|
818
|
+
continue;
|
|
819
|
+
}
|
|
820
|
+
ret.push(item);
|
|
821
|
+
primitives.add(item);
|
|
822
|
+
} else if (isBsonType(item, 'ObjectID')) {
|
|
823
|
+
if (ids.has(item.toString())) {
|
|
824
|
+
continue;
|
|
825
|
+
}
|
|
826
|
+
ret.push(item);
|
|
827
|
+
ids.add(item.toString());
|
|
828
|
+
} else {
|
|
829
|
+
ret.push(item);
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
return ret;
|
|
834
|
+
};
|
|
835
|
+
|
|
836
|
+
exports.buffer = {};
|
|
837
|
+
|
|
838
|
+
/**
|
|
839
|
+
* Determines if two buffers are equal.
|
|
840
|
+
*
|
|
841
|
+
* @param {Buffer} a
|
|
842
|
+
* @param {Object} b
|
|
843
|
+
*/
|
|
844
|
+
|
|
845
|
+
exports.buffer.areEqual = function(a, b) {
|
|
846
|
+
if (!Buffer.isBuffer(a)) {
|
|
847
|
+
return false;
|
|
848
|
+
}
|
|
849
|
+
if (!Buffer.isBuffer(b)) {
|
|
850
|
+
return false;
|
|
851
|
+
}
|
|
852
|
+
if (a.length !== b.length) {
|
|
853
|
+
return false;
|
|
854
|
+
}
|
|
855
|
+
for (let i = 0, len = a.length; i < len; ++i) {
|
|
856
|
+
if (a[i] !== b[i]) {
|
|
857
|
+
return false;
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
return true;
|
|
861
|
+
};
|
|
862
|
+
|
|
863
|
+
exports.getFunctionName = getFunctionName;
|
|
864
|
+
|
|
865
|
+
/**
|
|
866
|
+
* Decorate buffers
|
|
867
|
+
* @param {Object} destination
|
|
868
|
+
* @param {Object} source
|
|
869
|
+
*/
|
|
870
|
+
|
|
871
|
+
exports.decorate = function(destination, source) {
|
|
872
|
+
for (const key in source) {
|
|
873
|
+
if (specialProperties.has(key)) {
|
|
874
|
+
continue;
|
|
875
|
+
}
|
|
876
|
+
destination[key] = source[key];
|
|
877
|
+
}
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
/**
|
|
881
|
+
* merges to with a copy of from
|
|
882
|
+
*
|
|
883
|
+
* @param {Object} to
|
|
884
|
+
* @param {Object} fromObj
|
|
885
|
+
* @api private
|
|
886
|
+
*/
|
|
887
|
+
|
|
888
|
+
exports.mergeClone = function(to, fromObj) {
|
|
889
|
+
if (isMongooseObject(fromObj)) {
|
|
890
|
+
fromObj = fromObj.toObject({
|
|
891
|
+
transform: false,
|
|
892
|
+
virtuals: false,
|
|
893
|
+
depopulate: true,
|
|
894
|
+
getters: false,
|
|
895
|
+
flattenDecimals: false
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
const keys = Object.keys(fromObj);
|
|
899
|
+
const len = keys.length;
|
|
900
|
+
let i = 0;
|
|
901
|
+
let key;
|
|
902
|
+
|
|
903
|
+
while (i < len) {
|
|
904
|
+
key = keys[i++];
|
|
905
|
+
if (specialProperties.has(key)) {
|
|
906
|
+
continue;
|
|
907
|
+
}
|
|
908
|
+
if (typeof to[key] === 'undefined') {
|
|
909
|
+
to[key] = exports.clone(fromObj[key], {
|
|
910
|
+
transform: false,
|
|
911
|
+
virtuals: false,
|
|
912
|
+
depopulate: true,
|
|
913
|
+
getters: false,
|
|
914
|
+
flattenDecimals: false
|
|
915
|
+
});
|
|
916
|
+
} else {
|
|
917
|
+
let val = fromObj[key];
|
|
918
|
+
if (val != null && val.valueOf && !(val instanceof Date)) {
|
|
919
|
+
val = val.valueOf();
|
|
920
|
+
}
|
|
921
|
+
if (exports.isObject(val)) {
|
|
922
|
+
let obj = val;
|
|
923
|
+
if (isMongooseObject(val) && !val.isMongooseBuffer) {
|
|
924
|
+
obj = obj.toObject({
|
|
925
|
+
transform: false,
|
|
926
|
+
virtuals: false,
|
|
927
|
+
depopulate: true,
|
|
928
|
+
getters: false,
|
|
929
|
+
flattenDecimals: false
|
|
930
|
+
});
|
|
931
|
+
}
|
|
932
|
+
if (val.isMongooseBuffer) {
|
|
933
|
+
obj = Buffer.from(obj);
|
|
934
|
+
}
|
|
935
|
+
exports.mergeClone(to[key], obj);
|
|
936
|
+
} else {
|
|
937
|
+
to[key] = exports.clone(val, {
|
|
938
|
+
flattenDecimals: false
|
|
939
|
+
});
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
};
|
|
944
|
+
|
|
945
|
+
/**
|
|
946
|
+
* Executes a function on each element of an array (like _.each)
|
|
947
|
+
*
|
|
948
|
+
* @param {Array} arr
|
|
949
|
+
* @param {Function} fn
|
|
950
|
+
* @api private
|
|
951
|
+
*/
|
|
952
|
+
|
|
953
|
+
exports.each = function(arr, fn) {
|
|
954
|
+
for (const item of arr) {
|
|
955
|
+
fn(item);
|
|
956
|
+
}
|
|
957
|
+
};
|
|
958
|
+
|
|
959
|
+
/*!
|
|
960
|
+
* ignore
|
|
961
|
+
*/
|
|
962
|
+
|
|
963
|
+
exports.getOption = function(name) {
|
|
964
|
+
const sources = Array.prototype.slice.call(arguments, 1);
|
|
965
|
+
|
|
966
|
+
for (const source of sources) {
|
|
967
|
+
if (source == null) {
|
|
968
|
+
continue;
|
|
969
|
+
}
|
|
970
|
+
if (source[name] != null) {
|
|
971
|
+
return source[name];
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
return null;
|
|
976
|
+
};
|
|
977
|
+
|
|
978
|
+
/*!
|
|
979
|
+
* ignore
|
|
980
|
+
*/
|
|
981
|
+
|
|
982
|
+
exports.noop = function() {};
|
|
983
|
+
|
|
984
|
+
exports.errorToPOJO = function errorToPOJO(error) {
|
|
985
|
+
const isError = error instanceof Error;
|
|
986
|
+
if (!isError) {
|
|
987
|
+
throw new Error('`error` must be `instanceof Error`.');
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
const ret = {};
|
|
991
|
+
for (const properyName of Object.getOwnPropertyNames(error)) {
|
|
992
|
+
ret[properyName] = error[properyName];
|
|
993
|
+
}
|
|
994
|
+
return ret;
|
|
995
|
+
};
|
|
996
|
+
|
|
997
|
+
/*!
|
|
998
|
+
* ignore
|
|
999
|
+
*/
|
|
1000
|
+
|
|
1001
|
+
exports.warn = function warn(message) {
|
|
1002
|
+
return process.emitWarning(message, { code: 'MONGOOSE' });
|
|
1003
|
+
};
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
exports.injectTimestampsOption = function injectTimestampsOption(writeOperation, timestampsOption) {
|
|
1007
|
+
if (timestampsOption == null) {
|
|
1008
|
+
return;
|
|
1009
|
+
}
|
|
1010
|
+
writeOperation.timestamps = timestampsOption;
|
|
1011
|
+
};
|