@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.
Files changed (292) hide show
  1. package/.eslintrc.js +232 -0
  2. package/.mocharc.yml +4 -0
  3. package/LICENSE.md +22 -0
  4. package/README.md +387 -0
  5. package/SECURITY.md +1 -0
  6. package/browser.js +8 -0
  7. package/dist/browser.umd.js +2 -0
  8. package/index.js +63 -0
  9. package/lgtm.yml +12 -0
  10. package/lib/aggregate.js +1164 -0
  11. package/lib/browser.js +158 -0
  12. package/lib/browserDocument.js +101 -0
  13. package/lib/cast/boolean.js +32 -0
  14. package/lib/cast/date.js +41 -0
  15. package/lib/cast/decimal128.js +36 -0
  16. package/lib/cast/number.js +42 -0
  17. package/lib/cast/objectid.js +29 -0
  18. package/lib/cast/string.js +37 -0
  19. package/lib/cast.js +418 -0
  20. package/lib/collection.js +311 -0
  21. package/lib/connection.js +1574 -0
  22. package/lib/connectionstate.js +26 -0
  23. package/lib/cursor/AggregationCursor.js +380 -0
  24. package/lib/cursor/ChangeStream.js +154 -0
  25. package/lib/cursor/QueryCursor.js +538 -0
  26. package/lib/document.js +4771 -0
  27. package/lib/document_provider.js +30 -0
  28. package/lib/driver.js +15 -0
  29. package/lib/drivers/SPEC.md +4 -0
  30. package/lib/drivers/browser/ReadPreference.js +7 -0
  31. package/lib/drivers/browser/binary.js +14 -0
  32. package/lib/drivers/browser/decimal128.js +7 -0
  33. package/lib/drivers/browser/index.js +16 -0
  34. package/lib/drivers/browser/objectid.js +29 -0
  35. package/lib/drivers/node-mongodb-native/ReadPreference.js +47 -0
  36. package/lib/drivers/node-mongodb-native/binary.js +10 -0
  37. package/lib/drivers/node-mongodb-native/collection.js +460 -0
  38. package/lib/drivers/node-mongodb-native/connection.js +166 -0
  39. package/lib/drivers/node-mongodb-native/decimal128.js +7 -0
  40. package/lib/drivers/node-mongodb-native/index.js +12 -0
  41. package/lib/drivers/node-mongodb-native/objectid.js +16 -0
  42. package/lib/error/browserMissingSchema.js +28 -0
  43. package/lib/error/bulkWriteError.js +41 -0
  44. package/lib/error/cast.js +158 -0
  45. package/lib/error/disconnected.js +33 -0
  46. package/lib/error/divergentArray.js +38 -0
  47. package/lib/error/eachAsyncMultiError.js +41 -0
  48. package/lib/error/index.js +228 -0
  49. package/lib/error/messages.js +47 -0
  50. package/lib/error/missingSchema.js +31 -0
  51. package/lib/error/mongooseError.js +13 -0
  52. package/lib/error/notFound.js +45 -0
  53. package/lib/error/objectExpected.js +30 -0
  54. package/lib/error/objectParameter.js +30 -0
  55. package/lib/error/overwriteModel.js +30 -0
  56. package/lib/error/parallelSave.js +30 -0
  57. package/lib/error/parallelValidate.js +31 -0
  58. package/lib/error/serverSelection.js +61 -0
  59. package/lib/error/setOptionError.js +101 -0
  60. package/lib/error/strict.js +33 -0
  61. package/lib/error/strictPopulate.js +29 -0
  62. package/lib/error/syncIndexes.js +30 -0
  63. package/lib/error/validation.js +103 -0
  64. package/lib/error/validator.js +99 -0
  65. package/lib/error/version.js +36 -0
  66. package/lib/helpers/aggregate/prepareDiscriminatorPipeline.js +39 -0
  67. package/lib/helpers/aggregate/stringifyFunctionOperators.js +50 -0
  68. package/lib/helpers/arrayDepth.js +33 -0
  69. package/lib/helpers/clone.js +177 -0
  70. package/lib/helpers/common.js +127 -0
  71. package/lib/helpers/cursor/eachAsync.js +222 -0
  72. package/lib/helpers/discriminator/areDiscriminatorValuesEqual.js +16 -0
  73. package/lib/helpers/discriminator/checkEmbeddedDiscriminatorKeyProjection.js +12 -0
  74. package/lib/helpers/discriminator/getConstructor.js +26 -0
  75. package/lib/helpers/discriminator/getDiscriminatorByValue.js +28 -0
  76. package/lib/helpers/discriminator/getSchemaDiscriminatorByValue.js +27 -0
  77. package/lib/helpers/discriminator/mergeDiscriminatorSchema.js +64 -0
  78. package/lib/helpers/document/applyDefaults.js +128 -0
  79. package/lib/helpers/document/cleanModifiedSubpaths.js +35 -0
  80. package/lib/helpers/document/compile.js +237 -0
  81. package/lib/helpers/document/getEmbeddedDiscriminatorPath.js +50 -0
  82. package/lib/helpers/document/handleSpreadDoc.js +35 -0
  83. package/lib/helpers/each.js +25 -0
  84. package/lib/helpers/error/combinePathErrors.js +22 -0
  85. package/lib/helpers/firstKey.js +8 -0
  86. package/lib/helpers/get.js +65 -0
  87. package/lib/helpers/getConstructorName.js +16 -0
  88. package/lib/helpers/getDefaultBulkwriteResult.js +27 -0
  89. package/lib/helpers/getFunctionName.js +10 -0
  90. package/lib/helpers/immediate.js +16 -0
  91. package/lib/helpers/indexes/applySchemaCollation.js +13 -0
  92. package/lib/helpers/indexes/decorateDiscriminatorIndexOptions.js +14 -0
  93. package/lib/helpers/indexes/getRelatedIndexes.js +59 -0
  94. package/lib/helpers/indexes/isDefaultIdIndex.js +18 -0
  95. package/lib/helpers/indexes/isIndexEqual.js +96 -0
  96. package/lib/helpers/indexes/isTextIndex.js +16 -0
  97. package/lib/helpers/isAsyncFunction.js +9 -0
  98. package/lib/helpers/isBsonType.js +16 -0
  99. package/lib/helpers/isMongooseObject.js +22 -0
  100. package/lib/helpers/isObject.js +16 -0
  101. package/lib/helpers/isPromise.js +6 -0
  102. package/lib/helpers/isSimpleValidator.js +22 -0
  103. package/lib/helpers/model/applyDefaultsToPOJO.js +52 -0
  104. package/lib/helpers/model/applyHooks.js +149 -0
  105. package/lib/helpers/model/applyMethods.js +70 -0
  106. package/lib/helpers/model/applyStaticHooks.js +71 -0
  107. package/lib/helpers/model/applyStatics.js +13 -0
  108. package/lib/helpers/model/castBulkWrite.js +240 -0
  109. package/lib/helpers/model/discriminator.js +218 -0
  110. package/lib/helpers/model/pushNestedArrayPaths.js +15 -0
  111. package/lib/helpers/once.js +12 -0
  112. package/lib/helpers/parallelLimit.js +55 -0
  113. package/lib/helpers/path/parentPaths.js +18 -0
  114. package/lib/helpers/path/setDottedPath.js +33 -0
  115. package/lib/helpers/pluralize.js +94 -0
  116. package/lib/helpers/populate/SkipPopulateValue.js +10 -0
  117. package/lib/helpers/populate/assignRawDocsToIdStructure.js +124 -0
  118. package/lib/helpers/populate/assignVals.js +337 -0
  119. package/lib/helpers/populate/createPopulateQueryFilter.js +97 -0
  120. package/lib/helpers/populate/getModelsMapForPopulate.js +756 -0
  121. package/lib/helpers/populate/getSchemaTypes.js +233 -0
  122. package/lib/helpers/populate/getVirtual.js +72 -0
  123. package/lib/helpers/populate/leanPopulateMap.js +7 -0
  124. package/lib/helpers/populate/lookupLocalFields.js +40 -0
  125. package/lib/helpers/populate/markArraySubdocsPopulated.js +47 -0
  126. package/lib/helpers/populate/modelNamesFromRefPath.js +68 -0
  127. package/lib/helpers/populate/removeDeselectedForeignField.js +31 -0
  128. package/lib/helpers/populate/validateRef.js +19 -0
  129. package/lib/helpers/printJestWarning.js +21 -0
  130. package/lib/helpers/printStrictQueryWarning.js +11 -0
  131. package/lib/helpers/processConnectionOptions.js +65 -0
  132. package/lib/helpers/projection/applyProjection.js +77 -0
  133. package/lib/helpers/projection/hasIncludedChildren.js +41 -0
  134. package/lib/helpers/projection/isDefiningProjection.js +18 -0
  135. package/lib/helpers/projection/isExclusive.js +35 -0
  136. package/lib/helpers/projection/isInclusive.js +38 -0
  137. package/lib/helpers/projection/isNestedProjection.js +8 -0
  138. package/lib/helpers/projection/isPathExcluded.js +40 -0
  139. package/lib/helpers/projection/isPathSelectedInclusive.js +28 -0
  140. package/lib/helpers/projection/isSubpath.js +14 -0
  141. package/lib/helpers/projection/parseProjection.js +33 -0
  142. package/lib/helpers/promiseOrCallback.js +55 -0
  143. package/lib/helpers/query/applyGlobalOption.js +29 -0
  144. package/lib/helpers/query/applyQueryMiddleware.js +79 -0
  145. package/lib/helpers/query/cast$expr.js +282 -0
  146. package/lib/helpers/query/castFilterPath.js +54 -0
  147. package/lib/helpers/query/castUpdate.js +560 -0
  148. package/lib/helpers/query/completeMany.js +52 -0
  149. package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +90 -0
  150. package/lib/helpers/query/handleImmutable.js +28 -0
  151. package/lib/helpers/query/hasDollarKeys.js +23 -0
  152. package/lib/helpers/query/isOperator.js +14 -0
  153. package/lib/helpers/query/sanitizeFilter.js +38 -0
  154. package/lib/helpers/query/sanitizeProjection.js +14 -0
  155. package/lib/helpers/query/selectPopulatedFields.js +49 -0
  156. package/lib/helpers/query/trusted.js +13 -0
  157. package/lib/helpers/query/validOps.js +24 -0
  158. package/lib/helpers/query/wrapThunk.js +31 -0
  159. package/lib/helpers/schema/addAutoId.js +7 -0
  160. package/lib/helpers/schema/applyBuiltinPlugins.js +12 -0
  161. package/lib/helpers/schema/applyPlugins.js +55 -0
  162. package/lib/helpers/schema/applyWriteConcern.js +30 -0
  163. package/lib/helpers/schema/cleanPositionalOperators.js +12 -0
  164. package/lib/helpers/schema/getIndexes.js +164 -0
  165. package/lib/helpers/schema/getKeysInSchemaOrder.js +28 -0
  166. package/lib/helpers/schema/getPath.js +37 -0
  167. package/lib/helpers/schema/handleIdOption.js +20 -0
  168. package/lib/helpers/schema/handleTimestampOption.js +24 -0
  169. package/lib/helpers/schema/idGetter.js +32 -0
  170. package/lib/helpers/schema/merge.js +28 -0
  171. package/lib/helpers/schematype/handleImmutable.js +50 -0
  172. package/lib/helpers/setDefaultsOnInsert.js +132 -0
  173. package/lib/helpers/specialProperties.js +3 -0
  174. package/lib/helpers/symbols.js +20 -0
  175. package/lib/helpers/timers.js +3 -0
  176. package/lib/helpers/timestamps/setDocumentTimestamps.js +26 -0
  177. package/lib/helpers/timestamps/setupTimestamps.js +101 -0
  178. package/lib/helpers/topology/allServersUnknown.js +12 -0
  179. package/lib/helpers/topology/isAtlas.js +31 -0
  180. package/lib/helpers/topology/isSSLError.js +16 -0
  181. package/lib/helpers/update/applyTimestampsToChildren.js +193 -0
  182. package/lib/helpers/update/applyTimestampsToUpdate.js +117 -0
  183. package/lib/helpers/update/castArrayFilters.js +109 -0
  184. package/lib/helpers/update/modifiedPaths.js +33 -0
  185. package/lib/helpers/update/moveImmutableProperties.js +53 -0
  186. package/lib/helpers/update/removeUnusedArrayFilters.js +32 -0
  187. package/lib/helpers/update/updatedPathsByArrayFilter.js +27 -0
  188. package/lib/helpers/updateValidators.js +249 -0
  189. package/lib/index.js +1338 -0
  190. package/lib/internal.js +46 -0
  191. package/lib/model.js +5474 -0
  192. package/lib/options/PopulateOptions.js +36 -0
  193. package/lib/options/SchemaArrayOptions.js +78 -0
  194. package/lib/options/SchemaBufferOptions.js +38 -0
  195. package/lib/options/SchemaDateOptions.js +71 -0
  196. package/lib/options/SchemaDocumentArrayOptions.js +68 -0
  197. package/lib/options/SchemaMapOptions.js +43 -0
  198. package/lib/options/SchemaNumberOptions.js +101 -0
  199. package/lib/options/SchemaObjectIdOptions.js +64 -0
  200. package/lib/options/SchemaStringOptions.js +138 -0
  201. package/lib/options/SchemaSubdocumentOptions.js +42 -0
  202. package/lib/options/SchemaTypeOptions.js +244 -0
  203. package/lib/options/VirtualOptions.js +164 -0
  204. package/lib/options/propertyOptions.js +8 -0
  205. package/lib/options/removeOptions.js +14 -0
  206. package/lib/options/saveOptions.js +14 -0
  207. package/lib/options.js +15 -0
  208. package/lib/plugins/index.js +7 -0
  209. package/lib/plugins/removeSubdocs.js +31 -0
  210. package/lib/plugins/saveSubdocs.js +66 -0
  211. package/lib/plugins/sharding.js +83 -0
  212. package/lib/plugins/trackTransaction.js +92 -0
  213. package/lib/plugins/validateBeforeSave.js +45 -0
  214. package/lib/promise_provider.js +49 -0
  215. package/lib/query.js +5982 -0
  216. package/lib/queryhelpers.js +353 -0
  217. package/lib/schema/DocumentArrayElement.js +100 -0
  218. package/lib/schema/SubdocumentPath.js +369 -0
  219. package/lib/schema/array.js +663 -0
  220. package/lib/schema/boolean.js +267 -0
  221. package/lib/schema/buffer.js +269 -0
  222. package/lib/schema/date.js +404 -0
  223. package/lib/schema/decimal128.js +210 -0
  224. package/lib/schema/documentarray.js +598 -0
  225. package/lib/schema/index.js +39 -0
  226. package/lib/schema/map.js +84 -0
  227. package/lib/schema/mixed.js +132 -0
  228. package/lib/schema/number.js +438 -0
  229. package/lib/schema/objectid.js +295 -0
  230. package/lib/schema/operators/bitwise.js +38 -0
  231. package/lib/schema/operators/exists.js +12 -0
  232. package/lib/schema/operators/geospatial.js +107 -0
  233. package/lib/schema/operators/helpers.js +32 -0
  234. package/lib/schema/operators/text.js +39 -0
  235. package/lib/schema/operators/type.js +20 -0
  236. package/lib/schema/string.js +694 -0
  237. package/lib/schema/symbols.js +5 -0
  238. package/lib/schema/uuid.js +334 -0
  239. package/lib/schema.js +2665 -0
  240. package/lib/schematype.js +1727 -0
  241. package/lib/statemachine.js +207 -0
  242. package/lib/types/ArraySubdocument.js +189 -0
  243. package/lib/types/DocumentArray/index.js +113 -0
  244. package/lib/types/DocumentArray/isMongooseDocumentArray.js +5 -0
  245. package/lib/types/DocumentArray/methods/index.js +383 -0
  246. package/lib/types/array/index.js +116 -0
  247. package/lib/types/array/isMongooseArray.js +5 -0
  248. package/lib/types/array/methods/index.js +1014 -0
  249. package/lib/types/buffer.js +277 -0
  250. package/lib/types/decimal128.js +13 -0
  251. package/lib/types/index.js +20 -0
  252. package/lib/types/map.js +354 -0
  253. package/lib/types/objectid.js +41 -0
  254. package/lib/types/subdocument.js +432 -0
  255. package/lib/utils.js +1011 -0
  256. package/lib/validoptions.js +36 -0
  257. package/lib/virtualtype.js +175 -0
  258. package/package.json +164 -0
  259. package/scripts/build-browser.js +18 -0
  260. package/scripts/create-tarball.js +7 -0
  261. package/scripts/generateSearch.js +154 -0
  262. package/scripts/tsc-diagnostics-check.js +15 -0
  263. package/tools/auth.js +31 -0
  264. package/tools/repl.js +35 -0
  265. package/tools/sharded.js +46 -0
  266. package/tsconfig.json +9 -0
  267. package/types/aggregate.d.ts +174 -0
  268. package/types/augmentations.d.ts +9 -0
  269. package/types/callback.d.ts +8 -0
  270. package/types/collection.d.ts +44 -0
  271. package/types/connection.d.ts +242 -0
  272. package/types/cursor.d.ts +62 -0
  273. package/types/document.d.ts +266 -0
  274. package/types/error.d.ts +138 -0
  275. package/types/expressions.d.ts +2936 -0
  276. package/types/helpers.d.ts +32 -0
  277. package/types/index.d.ts +627 -0
  278. package/types/indexes.d.ts +98 -0
  279. package/types/inferschematype.d.ts +228 -0
  280. package/types/middlewares.d.ts +32 -0
  281. package/types/models.d.ts +488 -0
  282. package/types/mongooseoptions.d.ts +206 -0
  283. package/types/pipelinestage.d.ts +297 -0
  284. package/types/populate.d.ts +45 -0
  285. package/types/query.d.ts +659 -0
  286. package/types/schemaoptions.d.ts +231 -0
  287. package/types/schematypes.d.ts +478 -0
  288. package/types/session.d.ts +36 -0
  289. package/types/types.d.ts +104 -0
  290. package/types/utility.d.ts +43 -0
  291. package/types/validation.d.ts +34 -0
  292. package/types/virtuals.d.ts +14 -0
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+
3
+ module.exports = function merge(s1, s2, skipConflictingPaths) {
4
+ const paths = Object.keys(s2.tree);
5
+ const pathsToAdd = {};
6
+ for (const key of paths) {
7
+ if (skipConflictingPaths && (s1.paths[key] || s1.nested[key] || s1.singleNestedPaths[key])) {
8
+ continue;
9
+ }
10
+ pathsToAdd[key] = s2.tree[key];
11
+ }
12
+ s1.add(pathsToAdd);
13
+
14
+ s1.callQueue = s1.callQueue.concat(s2.callQueue);
15
+ s1.method(s2.methods);
16
+ s1.static(s2.statics);
17
+
18
+ for (const query in s2.query) {
19
+ s1.query[query] = s2.query[query];
20
+ }
21
+
22
+ for (const virtual in s2.virtuals) {
23
+ s1.virtuals[virtual] = s2.virtuals[virtual].clone();
24
+ }
25
+
26
+ s1._indexes = s1._indexes.concat(s2._indexes || []);
27
+ s1.s.hooks.merge(s2.s.hooks, false);
28
+ };
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ const StrictModeError = require('../../error/strict');
4
+
5
+ /*!
6
+ * ignore
7
+ */
8
+
9
+ module.exports = function(schematype) {
10
+ if (schematype.$immutable) {
11
+ schematype.$immutableSetter = createImmutableSetter(schematype.path,
12
+ schematype.options.immutable);
13
+ schematype.set(schematype.$immutableSetter);
14
+ } else if (schematype.$immutableSetter) {
15
+ schematype.setters = schematype.setters.
16
+ filter(fn => fn !== schematype.$immutableSetter);
17
+ delete schematype.$immutableSetter;
18
+ }
19
+ };
20
+
21
+ function createImmutableSetter(path, immutable) {
22
+ return function immutableSetter(v, _priorVal, _doc, options) {
23
+ if (this == null || this.$__ == null) {
24
+ return v;
25
+ }
26
+ if (this.isNew) {
27
+ return v;
28
+ }
29
+ if (options && options.overwriteImmutable) {
30
+ return v;
31
+ }
32
+
33
+ const _immutable = typeof immutable === 'function' ?
34
+ immutable.call(this, this) :
35
+ immutable;
36
+ if (!_immutable) {
37
+ return v;
38
+ }
39
+
40
+ const _value = this.$__.priorDoc != null ?
41
+ this.$__.priorDoc.$__getValue(path) :
42
+ this.$__getValue(path);
43
+ if (this.$__.strictMode === 'throw' && v !== _value) {
44
+ throw new StrictModeError(path, 'Path `' + path + '` is immutable ' +
45
+ 'and strict mode is set to throw.', true);
46
+ }
47
+
48
+ return _value;
49
+ };
50
+ }
@@ -0,0 +1,132 @@
1
+ 'use strict';
2
+ const modifiedPaths = require('./common').modifiedPaths;
3
+ const get = require('./get');
4
+
5
+ /**
6
+ * Applies defaults to update and findOneAndUpdate operations.
7
+ *
8
+ * @param {Object} filter
9
+ * @param {Schema} schema
10
+ * @param {Object} castedDoc
11
+ * @param {Object} options
12
+ * @method setDefaultsOnInsert
13
+ * @api private
14
+ */
15
+
16
+ module.exports = function(filter, schema, castedDoc, options) {
17
+ options = options || {};
18
+
19
+ const shouldSetDefaultsOnInsert =
20
+ options.setDefaultsOnInsert != null ?
21
+ options.setDefaultsOnInsert :
22
+ schema.base.options.setDefaultsOnInsert;
23
+
24
+ if (!options.upsert || shouldSetDefaultsOnInsert === false) {
25
+ return castedDoc;
26
+ }
27
+
28
+ const keys = Object.keys(castedDoc || {});
29
+ const updatedKeys = {};
30
+ const updatedValues = {};
31
+ const numKeys = keys.length;
32
+ const modified = {};
33
+
34
+ let hasDollarUpdate = false;
35
+
36
+ for (let i = 0; i < numKeys; ++i) {
37
+ if (keys[i].startsWith('$')) {
38
+ modifiedPaths(castedDoc[keys[i]], '', modified);
39
+ hasDollarUpdate = true;
40
+ }
41
+ }
42
+
43
+ if (!hasDollarUpdate) {
44
+ modifiedPaths(castedDoc, '', modified);
45
+ }
46
+
47
+ const paths = Object.keys(filter);
48
+ const numPaths = paths.length;
49
+ for (let i = 0; i < numPaths; ++i) {
50
+ const path = paths[i];
51
+ const condition = filter[path];
52
+ if (condition && typeof condition === 'object') {
53
+ const conditionKeys = Object.keys(condition);
54
+ const numConditionKeys = conditionKeys.length;
55
+ let hasDollarKey = false;
56
+ for (let j = 0; j < numConditionKeys; ++j) {
57
+ if (conditionKeys[j].startsWith('$')) {
58
+ hasDollarKey = true;
59
+ break;
60
+ }
61
+ }
62
+ if (hasDollarKey) {
63
+ continue;
64
+ }
65
+ }
66
+ updatedKeys[path] = true;
67
+ modified[path] = true;
68
+ }
69
+
70
+ if (options && options.overwrite && !hasDollarUpdate) {
71
+ // Defaults will be set later, since we're overwriting we'll cast
72
+ // the whole update to a document
73
+ return castedDoc;
74
+ }
75
+
76
+ schema.eachPath(function(path, schemaType) {
77
+ // Skip single nested paths if underneath a map
78
+ if (schemaType.path === '_id' && schemaType.options.auto) {
79
+ return;
80
+ }
81
+ const def = schemaType.getDefault(null, true);
82
+ if (isModified(modified, path)) {
83
+ return;
84
+ }
85
+ if (typeof def === 'undefined') {
86
+ return;
87
+ }
88
+ if (schemaType.splitPath().includes('$*')) {
89
+ // Skip defaults underneath maps. We should never do `$setOnInsert` on a path with `$*`
90
+ return;
91
+ }
92
+
93
+ castedDoc = castedDoc || {};
94
+ castedDoc.$setOnInsert = castedDoc.$setOnInsert || {};
95
+ if (get(castedDoc, path) == null) {
96
+ castedDoc.$setOnInsert[path] = def;
97
+ }
98
+ updatedValues[path] = def;
99
+ });
100
+
101
+ return castedDoc;
102
+ };
103
+
104
+ function isModified(modified, path) {
105
+ if (modified[path]) {
106
+ return true;
107
+ }
108
+
109
+ // Is any parent path of `path` modified?
110
+ const sp = path.split('.');
111
+ let cur = sp[0];
112
+ for (let i = 1; i < sp.length; ++i) {
113
+ if (modified[cur]) {
114
+ return true;
115
+ }
116
+ cur += '.' + sp[i];
117
+ }
118
+
119
+ // Is any child of `path` modified?
120
+ const modifiedKeys = Object.keys(modified);
121
+ if (modifiedKeys.length) {
122
+ const parentPath = path + '.';
123
+
124
+ for (const modifiedPath of modifiedKeys) {
125
+ if (modifiedPath.slice(0, path.length + 1) === parentPath) {
126
+ return true;
127
+ }
128
+ }
129
+ }
130
+
131
+ return false;
132
+ }
@@ -0,0 +1,3 @@
1
+ 'use strict';
2
+
3
+ module.exports = new Set(['__proto__', 'constructor', 'prototype']);
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+
3
+ exports.arrayAtomicsBackupSymbol = Symbol('mongoose#Array#atomicsBackup');
4
+ exports.arrayAtomicsSymbol = Symbol('mongoose#Array#_atomics');
5
+ exports.arrayParentSymbol = Symbol('mongoose#Array#_parent');
6
+ exports.arrayPathSymbol = Symbol('mongoose#Array#_path');
7
+ exports.arraySchemaSymbol = Symbol('mongoose#Array#_schema');
8
+ exports.documentArrayParent = Symbol('mongoose:documentArrayParent');
9
+ exports.documentIsSelected = Symbol('mongoose#Document#isSelected');
10
+ exports.documentIsModified = Symbol('mongoose#Document#isModified');
11
+ exports.documentModifiedPaths = Symbol('mongoose#Document#modifiedPaths');
12
+ exports.documentSchemaSymbol = Symbol('mongoose#Document#schema');
13
+ exports.getSymbol = Symbol('mongoose#Document#get');
14
+ exports.modelSymbol = Symbol('mongoose#Model');
15
+ exports.objectIdSymbol = Symbol('mongoose#ObjectId');
16
+ exports.populateModelSymbol = Symbol('mongoose.PopulateOptions#Model');
17
+ exports.schemaTypeSymbol = Symbol('mongoose#schemaType');
18
+ exports.sessionNewDocuments = Symbol('mongoose:ClientSession#newDocuments');
19
+ exports.scopeSymbol = Symbol('mongoose#Document#scope');
20
+ exports.validatorErrorSymbol = Symbol('mongoose:validatorError');
@@ -0,0 +1,3 @@
1
+ 'use strict';
2
+
3
+ exports.setTimeout = setTimeout;
@@ -0,0 +1,26 @@
1
+ 'use strict';
2
+
3
+ module.exports = function setDocumentTimestamps(doc, timestampOption, currentTime, createdAt, updatedAt) {
4
+ const skipUpdatedAt = timestampOption != null && timestampOption.updatedAt === false;
5
+ const skipCreatedAt = timestampOption != null && timestampOption.createdAt === false;
6
+
7
+ const defaultTimestamp = currentTime != null ?
8
+ currentTime() :
9
+ doc.ownerDocument().constructor.base.now();
10
+
11
+ if (!skipCreatedAt &&
12
+ (doc.isNew || doc.$isSubdocument) &&
13
+ createdAt &&
14
+ !doc.$__getValue(createdAt) &&
15
+ doc.$__isSelected(createdAt)) {
16
+ doc.$set(createdAt, defaultTimestamp, undefined, { overwriteImmutable: true });
17
+ }
18
+
19
+ if (!skipUpdatedAt && updatedAt && (doc.isNew || doc.$isModified())) {
20
+ let ts = defaultTimestamp;
21
+ if (doc.isNew && createdAt != null) {
22
+ ts = doc.$__getValue(createdAt);
23
+ }
24
+ doc.$set(updatedAt, ts);
25
+ }
26
+ };
@@ -0,0 +1,101 @@
1
+ 'use strict';
2
+
3
+ const applyTimestampsToChildren = require('../update/applyTimestampsToChildren');
4
+ const applyTimestampsToUpdate = require('../update/applyTimestampsToUpdate');
5
+ const get = require('../get');
6
+ const handleTimestampOption = require('../schema/handleTimestampOption');
7
+ const setDocumentTimestamps = require('./setDocumentTimestamps');
8
+ const symbols = require('../../schema/symbols');
9
+
10
+ module.exports = function setupTimestamps(schema, timestamps) {
11
+ const childHasTimestamp = schema.childSchemas.find(withTimestamp);
12
+ function withTimestamp(s) {
13
+ const ts = s.schema.options.timestamps;
14
+ return !!ts;
15
+ }
16
+
17
+ if (!timestamps && !childHasTimestamp) {
18
+ return;
19
+ }
20
+
21
+ const createdAt = handleTimestampOption(timestamps, 'createdAt');
22
+ const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
23
+ const currentTime = timestamps != null && timestamps.hasOwnProperty('currentTime') ?
24
+ timestamps.currentTime :
25
+ null;
26
+ const schemaAdditions = {};
27
+
28
+ schema.$timestamps = { createdAt: createdAt, updatedAt: updatedAt };
29
+
30
+ if (createdAt && !schema.paths[createdAt]) {
31
+ const baseImmutableCreatedAt = schema.base != null ? schema.base.get('timestamps.createdAt.immutable') : null;
32
+ const immutable = baseImmutableCreatedAt != null ? baseImmutableCreatedAt : true;
33
+ schemaAdditions[createdAt] = { [schema.options.typeKey || 'type']: Date, immutable };
34
+ }
35
+
36
+ if (updatedAt && !schema.paths[updatedAt]) {
37
+ schemaAdditions[updatedAt] = Date;
38
+ }
39
+
40
+ schema.add(schemaAdditions);
41
+
42
+ schema.pre('save', function timestampsPreSave(next) {
43
+ const timestampOption = get(this, '$__.saveOptions.timestamps');
44
+ if (timestampOption === false) {
45
+ return next();
46
+ }
47
+
48
+ setDocumentTimestamps(this, timestampOption, currentTime, createdAt, updatedAt);
49
+
50
+ next();
51
+ });
52
+
53
+ schema.methods.initializeTimestamps = function() {
54
+ const ts = currentTime != null ?
55
+ currentTime() :
56
+ this.constructor.base.now();
57
+ if (createdAt && !this.get(createdAt)) {
58
+ this.$set(createdAt, ts);
59
+ }
60
+ if (updatedAt && !this.get(updatedAt)) {
61
+ this.$set(updatedAt, ts);
62
+ }
63
+
64
+ if (this.$isSubdocument) {
65
+ return this;
66
+ }
67
+
68
+ const subdocs = this.$getAllSubdocs();
69
+ for (const subdoc of subdocs) {
70
+ if (subdoc.initializeTimestamps) {
71
+ subdoc.initializeTimestamps();
72
+ }
73
+ }
74
+
75
+ return this;
76
+ };
77
+
78
+ _setTimestampsOnUpdate[symbols.builtInMiddleware] = true;
79
+
80
+ const opts = { query: true, model: false };
81
+ schema.pre('findOneAndReplace', opts, _setTimestampsOnUpdate);
82
+ schema.pre('findOneAndUpdate', opts, _setTimestampsOnUpdate);
83
+ schema.pre('replaceOne', opts, _setTimestampsOnUpdate);
84
+ schema.pre('update', opts, _setTimestampsOnUpdate);
85
+ schema.pre('updateOne', opts, _setTimestampsOnUpdate);
86
+ schema.pre('updateMany', opts, _setTimestampsOnUpdate);
87
+
88
+ function _setTimestampsOnUpdate(next) {
89
+ const now = currentTime != null ?
90
+ currentTime() :
91
+ this.model.base.now();
92
+ // Replacing with null update should still trigger timestamps
93
+ if (this.op === 'findOneAndReplace' && this.getUpdate() == null) {
94
+ this.setUpdate({});
95
+ }
96
+ applyTimestampsToUpdate(now, createdAt, updatedAt, this.getUpdate(),
97
+ this.options, this.schema);
98
+ applyTimestampsToChildren(now, this.getUpdate(), this.model.schema);
99
+ next();
100
+ }
101
+ };
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ const getConstructorName = require('../getConstructorName');
4
+
5
+ module.exports = function allServersUnknown(topologyDescription) {
6
+ if (getConstructorName(topologyDescription) !== 'TopologyDescription') {
7
+ return false;
8
+ }
9
+
10
+ const servers = Array.from(topologyDescription.servers.values());
11
+ return servers.length > 0 && servers.every(server => server.type === 'Unknown');
12
+ };
@@ -0,0 +1,31 @@
1
+ 'use strict';
2
+
3
+ const getConstructorName = require('../getConstructorName');
4
+
5
+ /**
6
+ * @typedef { import('mongodb').TopologyDescription } TopologyDescription
7
+ */
8
+
9
+ /**
10
+ * Checks if topologyDescription contains servers connected to an atlas instance
11
+ *
12
+ * @param {TopologyDescription} topologyDescription
13
+ * @returns {boolean}
14
+ */
15
+ module.exports = function isAtlas(topologyDescription) {
16
+ if (getConstructorName(topologyDescription) !== 'TopologyDescription') {
17
+ return false;
18
+ }
19
+
20
+ if (topologyDescription.servers.size === 0) {
21
+ return false;
22
+ }
23
+
24
+ for (const server of topologyDescription.servers.values()) {
25
+ if (server.host.endsWith('.mongodb.net') === false || server.port !== 27017) {
26
+ return false;
27
+ }
28
+ }
29
+
30
+ return true;
31
+ };
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ const getConstructorName = require('../getConstructorName');
4
+
5
+ const nonSSLMessage = 'Client network socket disconnected before secure TLS ' +
6
+ 'connection was established';
7
+
8
+ module.exports = function isSSLError(topologyDescription) {
9
+ if (getConstructorName(topologyDescription) !== 'TopologyDescription') {
10
+ return false;
11
+ }
12
+
13
+ const descriptions = Array.from(topologyDescription.servers.values());
14
+ return descriptions.length > 0 &&
15
+ descriptions.every(descr => descr.error && descr.error.message.indexOf(nonSSLMessage) !== -1);
16
+ };
@@ -0,0 +1,193 @@
1
+ 'use strict';
2
+
3
+ const cleanPositionalOperators = require('../schema/cleanPositionalOperators');
4
+ const handleTimestampOption = require('../schema/handleTimestampOption');
5
+
6
+ module.exports = applyTimestampsToChildren;
7
+
8
+ /*!
9
+ * ignore
10
+ */
11
+
12
+ function applyTimestampsToChildren(now, update, schema) {
13
+ if (update == null) {
14
+ return;
15
+ }
16
+
17
+ const keys = Object.keys(update);
18
+ const hasDollarKey = keys.some(key => key[0] === '$');
19
+
20
+ if (hasDollarKey) {
21
+ if (update.$push) {
22
+ _applyTimestampToUpdateOperator(update.$push);
23
+ }
24
+ if (update.$addToSet) {
25
+ _applyTimestampToUpdateOperator(update.$addToSet);
26
+ }
27
+ if (update.$set != null) {
28
+ const keys = Object.keys(update.$set);
29
+ for (const key of keys) {
30
+ applyTimestampsToUpdateKey(schema, key, update.$set, now);
31
+ }
32
+ }
33
+ if (update.$setOnInsert != null) {
34
+ const keys = Object.keys(update.$setOnInsert);
35
+ for (const key of keys) {
36
+ applyTimestampsToUpdateKey(schema, key, update.$setOnInsert, now);
37
+ }
38
+ }
39
+ }
40
+
41
+ const updateKeys = Object.keys(update).filter(key => key[0] !== '$');
42
+ for (const key of updateKeys) {
43
+ applyTimestampsToUpdateKey(schema, key, update, now);
44
+ }
45
+
46
+ function _applyTimestampToUpdateOperator(op) {
47
+ for (const key of Object.keys(op)) {
48
+ const $path = schema.path(key.replace(/\.\$\./i, '.').replace(/.\$$/, ''));
49
+ if (op[key] &&
50
+ $path &&
51
+ $path.$isMongooseDocumentArray &&
52
+ $path.schema.options.timestamps) {
53
+ const timestamps = $path.schema.options.timestamps;
54
+ const createdAt = handleTimestampOption(timestamps, 'createdAt');
55
+ const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
56
+ if (op[key].$each) {
57
+ op[key].$each.forEach(function(subdoc) {
58
+ if (updatedAt != null) {
59
+ subdoc[updatedAt] = now;
60
+ }
61
+ if (createdAt != null) {
62
+ subdoc[createdAt] = now;
63
+ }
64
+
65
+ applyTimestampsToChildren(now, subdoc, $path.schema);
66
+ });
67
+ } else {
68
+ if (updatedAt != null) {
69
+ op[key][updatedAt] = now;
70
+ }
71
+ if (createdAt != null) {
72
+ op[key][createdAt] = now;
73
+ }
74
+
75
+ applyTimestampsToChildren(now, op[key], $path.schema);
76
+ }
77
+ }
78
+ }
79
+ }
80
+ }
81
+
82
+ function applyTimestampsToDocumentArray(arr, schematype, now) {
83
+ const timestamps = schematype.schema.options.timestamps;
84
+
85
+ const len = arr.length;
86
+
87
+ if (!timestamps) {
88
+ for (let i = 0; i < len; ++i) {
89
+ applyTimestampsToChildren(now, arr[i], schematype.schema);
90
+ }
91
+ return;
92
+ }
93
+
94
+ const createdAt = handleTimestampOption(timestamps, 'createdAt');
95
+ const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
96
+ for (let i = 0; i < len; ++i) {
97
+ if (updatedAt != null) {
98
+ arr[i][updatedAt] = now;
99
+ }
100
+ if (createdAt != null) {
101
+ arr[i][createdAt] = now;
102
+ }
103
+
104
+ applyTimestampsToChildren(now, arr[i], schematype.schema);
105
+ }
106
+ }
107
+
108
+ function applyTimestampsToSingleNested(subdoc, schematype, now) {
109
+ const timestamps = schematype.schema.options.timestamps;
110
+ if (!timestamps) {
111
+ applyTimestampsToChildren(now, subdoc, schematype.schema);
112
+ return;
113
+ }
114
+
115
+ const createdAt = handleTimestampOption(timestamps, 'createdAt');
116
+ const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
117
+ if (updatedAt != null) {
118
+ subdoc[updatedAt] = now;
119
+ }
120
+ if (createdAt != null) {
121
+ subdoc[createdAt] = now;
122
+ }
123
+
124
+ applyTimestampsToChildren(now, subdoc, schematype.schema);
125
+ }
126
+
127
+ function applyTimestampsToUpdateKey(schema, key, update, now) {
128
+ // Replace positional operator `$` and array filters `$[]` and `$[.*]`
129
+ const keyToSearch = cleanPositionalOperators(key);
130
+ const path = schema.path(keyToSearch);
131
+ if (!path) {
132
+ return;
133
+ }
134
+
135
+ const parentSchemaTypes = [];
136
+ const pieces = keyToSearch.split('.');
137
+ for (let i = pieces.length - 1; i > 0; --i) {
138
+ const s = schema.path(pieces.slice(0, i).join('.'));
139
+ if (s != null &&
140
+ (s.$isMongooseDocumentArray || s.$isSingleNested)) {
141
+ parentSchemaTypes.push({ parentPath: key.split('.').slice(0, i).join('.'), parentSchemaType: s });
142
+ }
143
+ }
144
+
145
+ if (Array.isArray(update[key]) && path.$isMongooseDocumentArray) {
146
+ applyTimestampsToDocumentArray(update[key], path, now);
147
+ } else if (update[key] && path.$isSingleNested) {
148
+ applyTimestampsToSingleNested(update[key], path, now);
149
+ } else if (parentSchemaTypes.length > 0) {
150
+ for (const item of parentSchemaTypes) {
151
+ const parentPath = item.parentPath;
152
+ const parentSchemaType = item.parentSchemaType;
153
+ const timestamps = parentSchemaType.schema.options.timestamps;
154
+ const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
155
+
156
+ if (!timestamps || updatedAt == null) {
157
+ continue;
158
+ }
159
+
160
+ if (parentSchemaType.$isSingleNested) {
161
+ // Single nested is easy
162
+ update[parentPath + '.' + updatedAt] = now;
163
+ } else if (parentSchemaType.$isMongooseDocumentArray) {
164
+ let childPath = key.substring(parentPath.length + 1);
165
+
166
+ if (/^\d+$/.test(childPath)) {
167
+ update[parentPath + '.' + childPath][updatedAt] = now;
168
+ continue;
169
+ }
170
+
171
+ const firstDot = childPath.indexOf('.');
172
+ childPath = firstDot !== -1 ? childPath.substring(0, firstDot) : childPath;
173
+
174
+ update[parentPath + '.' + childPath + '.' + updatedAt] = now;
175
+ }
176
+ }
177
+ } else if (path.schema != null && path.schema != schema && update[key]) {
178
+ const timestamps = path.schema.options.timestamps;
179
+ const createdAt = handleTimestampOption(timestamps, 'createdAt');
180
+ const updatedAt = handleTimestampOption(timestamps, 'updatedAt');
181
+
182
+ if (!timestamps) {
183
+ return;
184
+ }
185
+
186
+ if (updatedAt != null) {
187
+ update[key][updatedAt] = now;
188
+ }
189
+ if (createdAt != null) {
190
+ update[key][createdAt] = now;
191
+ }
192
+ }
193
+ }