@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,36 @@
1
+
2
+ /*!
3
+ * Valid mongoose options
4
+ */
5
+
6
+ 'use strict';
7
+
8
+ const VALID_OPTIONS = Object.freeze([
9
+ 'allowDiskUse',
10
+ 'applyPluginsToChildSchemas',
11
+ 'applyPluginsToDiscriminators',
12
+ 'autoCreate',
13
+ 'autoIndex',
14
+ 'bufferCommands',
15
+ 'bufferTimeoutMS',
16
+ 'cloneSchemas',
17
+ 'debug',
18
+ 'id',
19
+ 'timestamps.createdAt.immutable',
20
+ 'maxTimeMS',
21
+ 'objectIdGetter',
22
+ 'overwriteModels',
23
+ 'returnOriginal',
24
+ 'runValidators',
25
+ 'sanitizeFilter',
26
+ 'sanitizeProjection',
27
+ 'selectPopulatedPaths',
28
+ 'setDefaultsOnInsert',
29
+ 'strict',
30
+ 'strictPopulate',
31
+ 'strictQuery',
32
+ 'toJSON',
33
+ 'toObject'
34
+ ]);
35
+
36
+ module.exports = VALID_OPTIONS;
@@ -0,0 +1,175 @@
1
+ 'use strict';
2
+
3
+ const utils = require('./utils');
4
+
5
+ /**
6
+ * VirtualType constructor
7
+ *
8
+ * This is what mongoose uses to define virtual attributes via `Schema.prototype.virtual`.
9
+ *
10
+ * #### Example:
11
+ *
12
+ * const fullname = schema.virtual('fullname');
13
+ * fullname instanceof mongoose.VirtualType // true
14
+ *
15
+ * @param {Object} options
16
+ * @param {String|Function} [options.ref] if `ref` is not nullish, this becomes a [populated virtual](/docs/populate.html#populate-virtuals)
17
+ * @param {String|Function} [options.localField] the local field to populate on if this is a populated virtual.
18
+ * @param {String|Function} [options.foreignField] the foreign field to populate on if this is a populated virtual.
19
+ * @param {Boolean} [options.justOne=false] by default, a populated virtual is an array. If you set `justOne`, the populated virtual will be a single doc or `null`.
20
+ * @param {Boolean} [options.getters=false] if you set this to `true`, Mongoose will call any custom getters you defined on this virtual
21
+ * @param {Boolean} [options.count=false] if you set this to `true`, `populate()` will set this virtual to the number of populated documents, as opposed to the documents themselves, using [`Query#countDocuments()`](/docs/api/query.html#query_Query-countDocuments)
22
+ * @param {Object|Function} [options.match=null] add an extra match condition to `populate()`
23
+ * @param {Number} [options.limit=null] add a default `limit` to the `populate()` query
24
+ * @param {Number} [options.skip=null] add a default `skip` to the `populate()` query
25
+ * @param {Number} [options.perDocumentLimit=null] For legacy reasons, `limit` with `populate()` may give incorrect results because it only executes a single query for every document being populated. If you set `perDocumentLimit`, Mongoose will ensure correct `limit` per document by executing a separate query for each document to `populate()`. For example, `.find().populate({ path: 'test', perDocumentLimit: 2 })` will execute 2 additional queries if `.find()` returns 2 documents.
26
+ * @param {Object} [options.options=null] Additional options like `limit` and `lean`.
27
+ * @param {String} name
28
+ * @api public
29
+ */
30
+
31
+ function VirtualType(options, name) {
32
+ this.path = name;
33
+ this.getters = [];
34
+ this.setters = [];
35
+ this.options = Object.assign({}, options);
36
+ }
37
+
38
+ /**
39
+ * If no getters/setters, add a default
40
+ *
41
+ * @api private
42
+ */
43
+
44
+ VirtualType.prototype._applyDefaultGetters = function() {
45
+ if (this.getters.length > 0 || this.setters.length > 0) {
46
+ return;
47
+ }
48
+
49
+ const path = this.path;
50
+ const internalProperty = '$' + path;
51
+ this.getters.push(function() {
52
+ return this.$locals[internalProperty];
53
+ });
54
+ this.setters.push(function(v) {
55
+ this.$locals[internalProperty] = v;
56
+ });
57
+ };
58
+
59
+ /*!
60
+ * ignore
61
+ */
62
+
63
+ VirtualType.prototype.clone = function() {
64
+ const clone = new VirtualType(this.options, this.path);
65
+ clone.getters = [].concat(this.getters);
66
+ clone.setters = [].concat(this.setters);
67
+ return clone;
68
+ };
69
+
70
+ /**
71
+ * Adds a custom getter to this virtual.
72
+ *
73
+ * Mongoose calls the getter function with the below 3 parameters.
74
+ *
75
+ * - `value`: the value returned by the previous getter. If there is only one getter, `value` will be `undefined`.
76
+ * - `virtual`: the virtual object you called `.get()` on.
77
+ * - `doc`: the document this virtual is attached to. Equivalent to `this`.
78
+ *
79
+ * #### Example:
80
+ *
81
+ * const virtual = schema.virtual('fullname');
82
+ * virtual.get(function(value, virtual, doc) {
83
+ * return this.name.first + ' ' + this.name.last;
84
+ * });
85
+ *
86
+ * @param {Function} fn
87
+ * @return {VirtualType} this
88
+ * @api public
89
+ */
90
+
91
+ VirtualType.prototype.get = function(fn) {
92
+ this.getters.push(fn);
93
+ return this;
94
+ };
95
+
96
+ /**
97
+ * Adds a custom setter to this virtual.
98
+ *
99
+ * Mongoose calls the setter function with the below 3 parameters.
100
+ *
101
+ * - `value`: the value being set.
102
+ * - `virtual`: the virtual object you're calling `.set()` on.
103
+ * - `doc`: the document this virtual is attached to. Equivalent to `this`.
104
+ *
105
+ * #### Example:
106
+ *
107
+ * const virtual = schema.virtual('fullname');
108
+ * virtual.set(function(value, virtual, doc) {
109
+ * const parts = value.split(' ');
110
+ * this.name.first = parts[0];
111
+ * this.name.last = parts[1];
112
+ * });
113
+ *
114
+ * const Model = mongoose.model('Test', schema);
115
+ * const doc = new Model();
116
+ * // Calls the setter with `value = 'Jean-Luc Picard'`
117
+ * doc.fullname = 'Jean-Luc Picard';
118
+ * doc.name.first; // 'Jean-Luc'
119
+ * doc.name.last; // 'Picard'
120
+ *
121
+ * @param {Function} fn
122
+ * @return {VirtualType} this
123
+ * @api public
124
+ */
125
+
126
+ VirtualType.prototype.set = function(fn) {
127
+ this.setters.push(fn);
128
+ return this;
129
+ };
130
+
131
+ /**
132
+ * Applies getters to `value`.
133
+ *
134
+ * @param {Object} value
135
+ * @param {Document} doc The document this virtual is attached to
136
+ * @return {Any} the value after applying all getters
137
+ * @api public
138
+ */
139
+
140
+ VirtualType.prototype.applyGetters = function(value, doc) {
141
+ if (utils.hasUserDefinedProperty(this.options, ['ref', 'refPath']) &&
142
+ doc.$$populatedVirtuals &&
143
+ doc.$$populatedVirtuals.hasOwnProperty(this.path)) {
144
+ value = doc.$$populatedVirtuals[this.path];
145
+ }
146
+
147
+ let v = value;
148
+ for (const getter of this.getters) {
149
+ v = getter.call(doc, v, this, doc);
150
+ }
151
+ return v;
152
+ };
153
+
154
+ /**
155
+ * Applies setters to `value`.
156
+ *
157
+ * @param {Object} value
158
+ * @param {Document} doc
159
+ * @return {Any} the value after applying all setters
160
+ * @api public
161
+ */
162
+
163
+ VirtualType.prototype.applySetters = function(value, doc) {
164
+ let v = value;
165
+ for (const setter of this.setters) {
166
+ v = setter.call(doc, v, this, doc);
167
+ }
168
+ return v;
169
+ };
170
+
171
+ /*!
172
+ * exports
173
+ */
174
+
175
+ module.exports = VirtualType;
package/package.json ADDED
@@ -0,0 +1,164 @@
1
+ {
2
+ "name": "@datanimbus/mongoose",
3
+ "description": "Mongoose MongoDB ODM",
4
+ "version": "6.13.10",
5
+ "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
+ "keywords": [
7
+ "mongodb",
8
+ "document",
9
+ "model",
10
+ "schema",
11
+ "database",
12
+ "odm",
13
+ "data",
14
+ "datastore",
15
+ "query",
16
+ "nosql",
17
+ "orm",
18
+ "db"
19
+ ],
20
+ "license": "MIT",
21
+ "dependencies": {
22
+ "bson": "^4.7.2",
23
+ "kareem": "2.5.1",
24
+ "mongodb": "4.17.2",
25
+ "mpath": "0.9.0",
26
+ "mquery": "4.0.3",
27
+ "ms": "2.1.3",
28
+ "sift": "16.0.1"
29
+ },
30
+ "devDependencies": {
31
+ "@babel/core": "7.20.12",
32
+ "@babel/preset-env": "7.20.2",
33
+ "@sinonjs/fake-timers": "11.2.2",
34
+ "@typescript-eslint/eslint-plugin": "5.50.0",
35
+ "@typescript-eslint/parser": "5.50.0",
36
+ "acquit": "1.3.0",
37
+ "acquit-ignore": "0.2.1",
38
+ "acquit-require": "0.1.1",
39
+ "assert-browserify": "2.0.0",
40
+ "axios": "1.1.3",
41
+ "babel-loader": "8.2.5",
42
+ "benchmark": "2.1.4",
43
+ "bluebird": "3.7.2",
44
+ "broken-link-checker": "^0.7.8",
45
+ "buffer": "^5.6.0",
46
+ "cheerio": "1.0.0-rc.12",
47
+ "crypto-browserify": "3.12.0",
48
+ "dotenv": "16.0.3",
49
+ "dox": "1.0.0",
50
+ "eslint": "8.33.0",
51
+ "eslint-plugin-markdown": "^3.0.0",
52
+ "eslint-plugin-mocha-no-only": "1.1.1",
53
+ "express": "^4.18.1",
54
+ "fs-extra": "~11.1.1",
55
+ "highlight.js": "11.7.0",
56
+ "lodash.isequal": "4.5.0",
57
+ "lodash.isequalwith": "4.4.0",
58
+ "marked": "4.2.12",
59
+ "mkdirp": "^2.1.3",
60
+ "mocha": "10.2.0",
61
+ "moment": "2.x",
62
+ "mongodb-memory-server": "8.11.4",
63
+ "ncp": "^2.0.0",
64
+ "nyc": "15.1.0",
65
+ "pug": "3.0.2",
66
+ "q": "1.5.1",
67
+ "sinon": "15.0.1",
68
+ "stream-browserify": "3.0.0",
69
+ "tmp": "0.2.1",
70
+ "tsd": "0.25.0",
71
+ "typescript": "4.9.5",
72
+ "uuid": "9.0.0",
73
+ "webpack": "5.75.0"
74
+ },
75
+ "directories": {
76
+ "lib": "./lib/mongoose"
77
+ },
78
+ "scripts": {
79
+ "docs:clean": "npm run docs:clean:stable",
80
+ "docs:clean:stable": "rimraf index.html && rimraf -rf ./docs/*.html && rimraf -rf ./docs/api && rimraf -rf ./docs/tutorials/*.html && rimraf -rf ./docs/typescript/*.html && rimraf -rf ./docs/*.html && rimraf -rf ./docs/source/_docs && rimraf -rf ./tmp",
81
+ "docs:clean:5x": "rimraf index.html && rimraf -rf ./docs/5.x && rimraf -rf ./docs/source/_docs && rimraf -rf ./tmp",
82
+ "docs:clean:6x": "rimraf index.html && rimraf -rf ./docs/6.x && rimraf -rf ./docs/source/_docs && rimraf -rf ./tmp",
83
+ "docs:copy:tmp": "mkdirp ./tmp/docs/css && mkdirp ./tmp/docs/js && mkdirp ./tmp/docs/images && mkdirp ./tmp/docs/tutorials && mkdirp ./tmp/docs/typescript && mkdirp ./tmp/docs/api && ncp ./docs/css ./tmp/docs/css --filter=.css$ && ncp ./docs/js ./tmp/docs/js --filter=.js$ && ncp ./docs/images ./tmp/docs/images && ncp ./docs/tutorials ./tmp/docs/tutorials && ncp ./docs/typescript ./tmp/docs/typescript && ncp ./docs/api ./tmp/docs/api && cp index.html ./tmp && cp docs/*.html ./tmp/docs/",
84
+ "docs:copy:tmp:5x": "rimraf ./docs/5.x && ncp ./tmp ./docs/5.x",
85
+ "docs:move:6x:tmp": "mv ./docs/6.x ./tmp",
86
+ "docs:copy:tmp:6x": "rimraf ./docs/6.x && ncp ./tmp ./docs/6.x",
87
+ "docs:checkout:gh-pages": "git checkout gh-pages",
88
+ "docs:checkout:5x": "git checkout 5.x",
89
+ "docs:checkout:6x": "git checkout 6.x",
90
+ "docs:generate": "node ./scripts/website.js",
91
+ "docs:generate:search": "node ./scripts/generateSearch.js",
92
+ "docs:generate:sponsorData": "node ./scripts/loadSponsorData.js",
93
+ "docs:merge:stable": "git merge master",
94
+ "docs:merge:5x": "git merge 5.x",
95
+ "docs:merge:6x": "git merge 6.x",
96
+ "docs:test": "npm run docs:generate && npm run docs:generate:search",
97
+ "docs:view": "node ./scripts/static.js",
98
+ "docs:prepare:publish:stable": "npm run docs:checkout:gh-pages && npm run docs:merge:stable && npm run docs:clean:stable && npm run docs:generate && npm run docs:generate:search",
99
+ "docs:prepare:publish:5x": "npm run docs:checkout:5x && npm run docs:merge:5x && npm run docs:clean:stable && npm run docs:generate && npm run docs:copy:tmp && npm run docs:checkout:gh-pages && npm run docs:copy:tmp:5x",
100
+ "docs:prepare:publish:6x": "npm run docs:checkout:6x && npm run docs:merge:6x && npm run docs:clean:stable && env DOCS_DEPLOY=true npm run docs:generate && npm run docs:move:6x:tmp && npm run docs:checkout:gh-pages && npm run docs:copy:tmp:6x",
101
+ "docs:check-links": "blc http://127.0.0.1:8089 -ro",
102
+ "lint": "eslint .",
103
+ "lint-js": "eslint . --ext .js",
104
+ "lint-ts": "eslint . --ext .ts",
105
+ "lint-md": "eslint . --ext .md",
106
+ "build-browser": "(rm ./dist/* || true) && node ./scripts/build-browser.js",
107
+ "prepublishOnly": "npm run build-browser",
108
+ "release": "git pull && git push origin master --tags && npm publish",
109
+ "release-5x": "git pull origin 5.x && git push origin 5.x && git push origin 5.x --tags && npm publish --tag 5x",
110
+ "release-6x": "git pull origin 6.x && git push origin 6.x && git push origin 6.x --tags && npm publish --tag 6x",
111
+ "mongo": "node ./tools/repl.js",
112
+ "test": "mocha --exit ./test/*.test.js",
113
+ "test-deno": "deno run --allow-env --allow-read --allow-net --allow-run --allow-sys --allow-write ./test/deno.js",
114
+ "test-rs": "START_REPLICA_SET=1 mocha --timeout 30000 --exit ./test/*.test.js",
115
+ "test-tsd": "node ./test/types/check-types-filename && tsd",
116
+ "tdd": "mocha ./test/*.test.js --inspect --watch --recursive --watch-files ./**/*.{js,ts}",
117
+ "test-coverage": "nyc --reporter=html --reporter=text npm test",
118
+ "ts-benchmark": "cd ./benchmarks/typescript/simple && npm install && npm run benchmark | node ../../../scripts/tsc-diagnostics-check"
119
+ },
120
+ "main": "./index.js",
121
+ "types": "./types/index.d.ts",
122
+ "engines": {
123
+ "node": ">=12.0.0"
124
+ },
125
+ "bugs": {
126
+ "url": "https://github.com/Automattic/mongoose/issues/new"
127
+ },
128
+ "repository": {
129
+ "type": "git",
130
+ "url": "git://github.com/Automattic/mongoose.git"
131
+ },
132
+ "homepage": "https://mongoosejs.com",
133
+ "browser": "./dist/browser.umd.js",
134
+ "mocha": {
135
+ "extension": [
136
+ "test.js"
137
+ ],
138
+ "watch-files": [
139
+ "test/**/*.js"
140
+ ]
141
+ },
142
+ "config": {
143
+ "mongodbMemoryServer": {
144
+ "disablePostinstall": true
145
+ }
146
+ },
147
+ "funding": {
148
+ "type": "opencollective",
149
+ "url": "https://opencollective.com/mongoose"
150
+ },
151
+ "tsd": {
152
+ "directory": "test/types",
153
+ "compilerOptions": {
154
+ "esModuleInterop": false,
155
+ "strict": true,
156
+ "allowSyntheticDefaultImports": true,
157
+ "strictPropertyInitialization": false,
158
+ "noImplicitAny": false,
159
+ "strictNullChecks": true,
160
+ "module": "commonjs",
161
+ "target": "ES2017"
162
+ }
163
+ }
164
+ }
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+
3
+ const config = require('../webpack.config.js');
4
+ const webpack = require('webpack');
5
+
6
+ const compiler = webpack(config);
7
+
8
+ console.log('Starting browser build...');
9
+ compiler.run((err, stats) => {
10
+ if (err) {
11
+ console.err(stats.toString());
12
+ console.err('Browser build unsuccessful.');
13
+ process.exit(1);
14
+ }
15
+ console.log(stats.toString());
16
+ console.log('Browser build successful.');
17
+ process.exit(0);
18
+ });
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ const { execSync } = require('child_process');
4
+ const { name, version } = require('../package.json');
5
+
6
+ execSync('npm pack');
7
+ execSync(`mv ${name}-${version}.tgz ${name}.tgz`);
@@ -0,0 +1,154 @@
1
+ 'use strict';
2
+
3
+ const config = require('../.config');
4
+ const cheerio = require('cheerio');
5
+ const docsFilemap = require('../docs/source');
6
+ const fs = require('fs');
7
+ const pug = require('pug');
8
+ const mongoose = require('../');
9
+ let { version } = require('../package.json');
10
+
11
+ const { marked: markdown } = require('marked');
12
+ const highlight = require('highlight.js');
13
+ markdown.setOptions({
14
+ highlight: function(code) {
15
+ return highlight.highlight(code, { language: 'JavaScript' }).value;
16
+ }
17
+ });
18
+
19
+ // 5.13.5 -> 5.x, 6.8.2 -> 6.x, etc.
20
+ version = version.slice(0, version.indexOf('.')) + '.x';
21
+
22
+ const contentSchema = new mongoose.Schema({
23
+ title: { type: String, required: true },
24
+ body: { type: String, required: true },
25
+ url: { type: String, required: true },
26
+ version: { type: String, required: true, default: version },
27
+ versionNumber: { type: Number, required: true, default: version.replace(/\.x$/, '') }
28
+ });
29
+ contentSchema.index({ title: 'text', body: 'text' });
30
+ const Content = mongoose.model('Content', contentSchema, 'Content');
31
+
32
+ const contents = [];
33
+
34
+ for (const [filename, file] of Object.entries(docsFilemap.fileMap)) {
35
+ if (file.api) {
36
+ for (const prop of file.props) {
37
+ const content = new Content({
38
+ title: `API: ${prop.name}`,
39
+ body: prop.description,
40
+ url: `${filename.replace(/^docs/, '')}#${prop.anchorId}`
41
+ });
42
+ const err = content.validateSync();
43
+ if (err != null) {
44
+ console.error(content);
45
+ throw err;
46
+ }
47
+ contents.push(content);
48
+ }
49
+ } else if (file.markdown) {
50
+ let text = fs.readFileSync(filename, 'utf8');
51
+ text = markdown.parse(text);
52
+
53
+ const content = new Content({
54
+ title: file.title,
55
+ body: text,
56
+ url: filename.replace('.md', '.html').replace(/^docs/, '')
57
+ });
58
+
59
+ content.validateSync();
60
+
61
+ const $ = cheerio.load(text);
62
+
63
+ contents.push(content);
64
+
65
+ // Break up individual h3's into separate content for more fine grained search
66
+ $('h3').each((index, el) => {
67
+ el = $(el);
68
+ const title = el.text();
69
+ const html = el.nextUntil('h3').html();
70
+ const content = new Content({
71
+ title: `${file.title}: ${title}`,
72
+ body: html,
73
+ url: `${filename.replace('.md', '.html').replace(/^docs/, '')}#${el.prop('id')}`
74
+ });
75
+
76
+ content.validateSync();
77
+ contents.push(content);
78
+ });
79
+ } else if (file.guide) {
80
+ let text = fs.readFileSync(filename, 'utf8');
81
+ text = text.substr(text.indexOf('block content') + 'block content\n'.length);
82
+ text = pug.render(`div\n${text}`, { filters: { markdown }, filename });
83
+
84
+ const content = new Content({
85
+ title: file.title,
86
+ body: text,
87
+ url: filename.replace('.pug', '.html').replace(/^docs/, '')
88
+ });
89
+
90
+ content.validateSync();
91
+
92
+ const $ = cheerio.load(text);
93
+
94
+ contents.push(content);
95
+
96
+ // Break up individual h3's into separate content for more fine grained search
97
+ $('h3').each((index, el) => {
98
+ el = $(el);
99
+ const title = el.text();
100
+ const html = el.nextUntil('h3').html();
101
+ const content = new Content({
102
+ title: `${file.title}: ${title}`,
103
+ body: html,
104
+ url: `${filename.replace('.pug', '.html').replace(/^docs/, '')}#${el.prop('id')}`
105
+ });
106
+
107
+ content.validateSync();
108
+ contents.push(content);
109
+ });
110
+ }
111
+ }
112
+
113
+ run().catch(async error => {
114
+ console.error(error.stack);
115
+
116
+ // ensure the script exists in case of error
117
+ await mongoose.disconnect();
118
+ });
119
+
120
+ async function run() {
121
+ if (!config || !config.uri) {
122
+ console.error('No Config or config.URI given, please create a .config.js file with those values');
123
+ process.exit(-1);
124
+ }
125
+
126
+ await mongoose.connect(config.uri, { dbName: 'mongoose', serverSelectionTimeoutMS: 5000 });
127
+
128
+ // wait for the index to be created
129
+ await Content.init();
130
+
131
+ await Content.deleteMany({ version });
132
+ for (const content of contents) {
133
+ if (version === '7.x') {
134
+ let url = content.url.startsWith('/') ? content.url : `/${content.url}`;
135
+ if (!url.startsWith('/docs')) {
136
+ url = '/docs' + url;
137
+ }
138
+ content.url = url;
139
+ } else {
140
+ const url = content.url.startsWith('/') ? content.url : `/${content.url}`;
141
+ content.url = `/docs/${version}/docs${url}`;
142
+ }
143
+ await content.save();
144
+ }
145
+
146
+ const results = await Content.
147
+ find({ $text: { $search: 'validate' }, version }, { score: { $meta: 'textScore' } }).
148
+ sort({ score: { $meta: 'textScore' } }).
149
+ limit(10);
150
+
151
+ console.log(results.map(res => res.url));
152
+
153
+ process.exit(0);
154
+ }
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+
5
+ const stdin = fs.readFileSync(0).toString('utf8');
6
+ const maxInstantiations = isNaN(process.argv[2]) ? 100000 : parseInt(process.argv[2], 10);
7
+
8
+ console.log(stdin);
9
+
10
+ const numInstantiations = parseInt(stdin.match(/Instantiations:\s+(\d+)/)[1], 10);
11
+ if (numInstantiations > maxInstantiations) {
12
+ throw new Error(`Instantiations ${numInstantiations} > max ${maxInstantiations}`);
13
+ }
14
+
15
+ process.exit(0);
package/tools/auth.js ADDED
@@ -0,0 +1,31 @@
1
+ 'use strict';
2
+
3
+ const Server = require('mongodb-topology-manager').Server;
4
+ const mongodb = require('mongodb');
5
+
6
+ run().catch(error => {
7
+ console.error(error);
8
+ process.exit(-1);
9
+ });
10
+
11
+ async function run() {
12
+ // Create new instance
13
+ const server = new Server('mongod', {
14
+ auth: null,
15
+ dbpath: '/data/db/27017'
16
+ });
17
+
18
+ // Purge the directory
19
+ await server.purge();
20
+
21
+ // Start process
22
+ await server.start();
23
+
24
+ const db = await mongodb.MongoClient.connect('mongodb://127.0.0.1:27017/admin');
25
+
26
+ await db.addUser('passwordIsTaco', 'taco', {
27
+ roles: ['dbOwner']
28
+ });
29
+
30
+ console.log('done');
31
+ }
package/tools/repl.js ADDED
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ run().catch(error => {
4
+ console.error(error);
5
+ process.exit(-1);
6
+ });
7
+
8
+ async function run() {
9
+ const ReplSet = require('mongodb-memory-server').MongoMemoryReplSet;
10
+
11
+ // Create new instance
12
+ const replSet = new ReplSet({
13
+ binary: {
14
+ version: process.argv[2]
15
+ },
16
+ instanceOpts: [
17
+ // Set the expiry job in MongoDB to run every second
18
+ {
19
+ port: 27017,
20
+ args: ['--setParameter', 'ttlMonitorSleepSecs=1']
21
+ }
22
+ ],
23
+ dbName: 'mongoose_test',
24
+ replSet: {
25
+ name: 'rs0',
26
+ count: 2,
27
+ storageEngine: 'wiredTiger'
28
+ }
29
+ });
30
+
31
+ await replSet.start();
32
+ await replSet.waitUntilRunning();
33
+ console.log('MongoDB-ReplicaSet is now running.');
34
+ console.log(replSet.getUri('mongoose_test'));
35
+ }
@@ -0,0 +1,46 @@
1
+ 'use strict';
2
+
3
+ run().catch(error => {
4
+ console.error(error);
5
+ process.exit(-1);
6
+ });
7
+
8
+
9
+ async function run() {
10
+ const Sharded = require('mongodb-topology-manager').Sharded;
11
+
12
+ // Create new instance
13
+ const topology = new Sharded({
14
+ mongod: 'mongod',
15
+ mongos: 'mongos'
16
+ });
17
+
18
+ await topology.addShard([{
19
+ options: {
20
+ bind_ip: '127.0.0.1', port: 31000, dbpath: '/data/db/31000', shardsvr: null
21
+ }
22
+ }], { replSet: 'rs1' });
23
+
24
+ await topology.addConfigurationServers([{
25
+ options: {
26
+ bind_ip: '127.0.0.1', port: 35000, dbpath: '/data/db/35000'
27
+ }
28
+ }], { replSet: 'rs0' });
29
+
30
+ await topology.addProxies([{
31
+ bind_ip: '127.0.0.1', port: 51000, configdb: '127.0.0.1:35000'
32
+ }], {
33
+ binary: 'mongos'
34
+ });
35
+
36
+ console.log('Start...');
37
+ // Start up topology
38
+ await topology.start();
39
+
40
+ console.log('Started');
41
+
42
+ // Shard db
43
+ await topology.enableSharding('test');
44
+
45
+ console.log('done');
46
+ }