@depup/mongoose 9.1.3-depup.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (295) hide show
  1. package/LICENSE.md +22 -0
  2. package/README.md +397 -0
  3. package/SECURITY.md +1 -0
  4. package/eslint.config.mjs +198 -0
  5. package/index.js +64 -0
  6. package/lib/aggregate.js +1189 -0
  7. package/lib/cast/bigint.js +46 -0
  8. package/lib/cast/boolean.js +32 -0
  9. package/lib/cast/date.js +41 -0
  10. package/lib/cast/decimal128.js +39 -0
  11. package/lib/cast/double.js +50 -0
  12. package/lib/cast/int32.js +36 -0
  13. package/lib/cast/number.js +42 -0
  14. package/lib/cast/objectid.js +29 -0
  15. package/lib/cast/string.js +37 -0
  16. package/lib/cast/uuid.js +35 -0
  17. package/lib/cast.js +436 -0
  18. package/lib/collection.js +321 -0
  19. package/lib/connection.js +1855 -0
  20. package/lib/connectionState.js +26 -0
  21. package/lib/constants.js +73 -0
  22. package/lib/cursor/aggregationCursor.js +466 -0
  23. package/lib/cursor/changeStream.js +198 -0
  24. package/lib/cursor/queryCursor.js +622 -0
  25. package/lib/document.js +5521 -0
  26. package/lib/driver.js +15 -0
  27. package/lib/drivers/SPEC.md +4 -0
  28. package/lib/drivers/node-mongodb-native/bulkWriteResult.js +5 -0
  29. package/lib/drivers/node-mongodb-native/collection.js +393 -0
  30. package/lib/drivers/node-mongodb-native/connection.js +506 -0
  31. package/lib/drivers/node-mongodb-native/index.js +10 -0
  32. package/lib/error/browserMissingSchema.js +29 -0
  33. package/lib/error/bulkSaveIncompleteError.js +44 -0
  34. package/lib/error/bulkWriteError.js +41 -0
  35. package/lib/error/cast.js +158 -0
  36. package/lib/error/createCollectionsError.js +26 -0
  37. package/lib/error/divergentArray.js +40 -0
  38. package/lib/error/eachAsyncMultiError.js +41 -0
  39. package/lib/error/index.js +237 -0
  40. package/lib/error/invalidSchemaOption.js +32 -0
  41. package/lib/error/messages.js +47 -0
  42. package/lib/error/missingSchema.js +33 -0
  43. package/lib/error/mongooseError.js +13 -0
  44. package/lib/error/notFound.js +47 -0
  45. package/lib/error/objectExpected.js +31 -0
  46. package/lib/error/objectParameter.js +31 -0
  47. package/lib/error/overwriteModel.js +31 -0
  48. package/lib/error/parallelSave.js +33 -0
  49. package/lib/error/parallelValidate.js +33 -0
  50. package/lib/error/serverSelection.js +62 -0
  51. package/lib/error/setOptionError.js +103 -0
  52. package/lib/error/strict.js +35 -0
  53. package/lib/error/strictPopulate.js +31 -0
  54. package/lib/error/syncIndexes.js +30 -0
  55. package/lib/error/validation.js +97 -0
  56. package/lib/error/validator.js +100 -0
  57. package/lib/error/version.js +38 -0
  58. package/lib/helpers/aggregate/prepareDiscriminatorPipeline.js +39 -0
  59. package/lib/helpers/aggregate/stringifyFunctionOperators.js +50 -0
  60. package/lib/helpers/arrayDepth.js +33 -0
  61. package/lib/helpers/clone.js +204 -0
  62. package/lib/helpers/common.js +127 -0
  63. package/lib/helpers/createJSONSchemaTypeDefinition.js +24 -0
  64. package/lib/helpers/cursor/eachAsync.js +225 -0
  65. package/lib/helpers/discriminator/applyEmbeddedDiscriminators.js +36 -0
  66. package/lib/helpers/discriminator/areDiscriminatorValuesEqual.js +16 -0
  67. package/lib/helpers/discriminator/checkEmbeddedDiscriminatorKeyProjection.js +12 -0
  68. package/lib/helpers/discriminator/getConstructor.js +29 -0
  69. package/lib/helpers/discriminator/getDiscriminatorByValue.js +28 -0
  70. package/lib/helpers/discriminator/getSchemaDiscriminatorByValue.js +27 -0
  71. package/lib/helpers/discriminator/mergeDiscriminatorSchema.js +91 -0
  72. package/lib/helpers/document/applyDefaults.js +132 -0
  73. package/lib/helpers/document/applyTimestamps.js +106 -0
  74. package/lib/helpers/document/applyVirtuals.js +147 -0
  75. package/lib/helpers/document/cleanModifiedSubpaths.js +45 -0
  76. package/lib/helpers/document/compile.js +238 -0
  77. package/lib/helpers/document/getDeepestSubdocumentForPath.js +38 -0
  78. package/lib/helpers/document/getEmbeddedDiscriminatorPath.js +53 -0
  79. package/lib/helpers/document/handleSpreadDoc.js +35 -0
  80. package/lib/helpers/each.js +25 -0
  81. package/lib/helpers/error/combinePathErrors.js +22 -0
  82. package/lib/helpers/firstKey.js +8 -0
  83. package/lib/helpers/get.js +65 -0
  84. package/lib/helpers/getConstructorName.js +16 -0
  85. package/lib/helpers/getDefaultBulkwriteResult.js +18 -0
  86. package/lib/helpers/getFunctionName.js +10 -0
  87. package/lib/helpers/immediate.js +16 -0
  88. package/lib/helpers/indexes/applySchemaCollation.js +13 -0
  89. package/lib/helpers/indexes/decorateDiscriminatorIndexOptions.js +14 -0
  90. package/lib/helpers/indexes/getRelatedIndexes.js +63 -0
  91. package/lib/helpers/indexes/isDefaultIdIndex.js +18 -0
  92. package/lib/helpers/indexes/isIndexEqual.js +95 -0
  93. package/lib/helpers/indexes/isIndexSpecEqual.js +32 -0
  94. package/lib/helpers/indexes/isTextIndex.js +16 -0
  95. package/lib/helpers/indexes/isTimeseriesIndex.js +16 -0
  96. package/lib/helpers/isAsyncFunction.js +9 -0
  97. package/lib/helpers/isBsonType.js +15 -0
  98. package/lib/helpers/isMongooseObject.js +22 -0
  99. package/lib/helpers/isObject.js +16 -0
  100. package/lib/helpers/isPOJO.js +12 -0
  101. package/lib/helpers/isPromise.js +6 -0
  102. package/lib/helpers/isSimpleValidator.js +22 -0
  103. package/lib/helpers/minimize.js +41 -0
  104. package/lib/helpers/model/applyDefaultsToPOJO.js +52 -0
  105. package/lib/helpers/model/applyHooks.js +140 -0
  106. package/lib/helpers/model/applyMethods.js +70 -0
  107. package/lib/helpers/model/applyStaticHooks.js +33 -0
  108. package/lib/helpers/model/applyStatics.js +13 -0
  109. package/lib/helpers/model/castBulkWrite.js +316 -0
  110. package/lib/helpers/model/decorateBulkWriteResult.js +8 -0
  111. package/lib/helpers/model/discriminator.js +265 -0
  112. package/lib/helpers/model/pushNestedArrayPaths.js +15 -0
  113. package/lib/helpers/omitUndefined.js +20 -0
  114. package/lib/helpers/once.js +12 -0
  115. package/lib/helpers/parallelLimit.js +37 -0
  116. package/lib/helpers/path/parentPaths.js +18 -0
  117. package/lib/helpers/path/setDottedPath.js +33 -0
  118. package/lib/helpers/pluralize.js +95 -0
  119. package/lib/helpers/populate/assignRawDocsToIdStructure.js +129 -0
  120. package/lib/helpers/populate/assignVals.js +360 -0
  121. package/lib/helpers/populate/createPopulateQueryFilter.js +97 -0
  122. package/lib/helpers/populate/getModelsMapForPopulate.js +776 -0
  123. package/lib/helpers/populate/getSchemaTypes.js +228 -0
  124. package/lib/helpers/populate/getVirtual.js +103 -0
  125. package/lib/helpers/populate/leanPopulateMap.js +7 -0
  126. package/lib/helpers/populate/lookupLocalFields.js +40 -0
  127. package/lib/helpers/populate/markArraySubdocsPopulated.js +49 -0
  128. package/lib/helpers/populate/modelNamesFromRefPath.js +66 -0
  129. package/lib/helpers/populate/removeDeselectedForeignField.js +31 -0
  130. package/lib/helpers/populate/setPopulatedVirtualValue.js +33 -0
  131. package/lib/helpers/populate/skipPopulateValue.js +10 -0
  132. package/lib/helpers/populate/validateRef.js +19 -0
  133. package/lib/helpers/printJestWarning.js +21 -0
  134. package/lib/helpers/processConnectionOptions.js +65 -0
  135. package/lib/helpers/projection/applyProjection.js +83 -0
  136. package/lib/helpers/projection/hasIncludedChildren.js +41 -0
  137. package/lib/helpers/projection/isDefiningProjection.js +18 -0
  138. package/lib/helpers/projection/isExclusive.js +37 -0
  139. package/lib/helpers/projection/isInclusive.js +39 -0
  140. package/lib/helpers/projection/isNestedProjection.js +8 -0
  141. package/lib/helpers/projection/isPathExcluded.js +40 -0
  142. package/lib/helpers/projection/isPathSelectedInclusive.js +28 -0
  143. package/lib/helpers/projection/isSubpath.js +14 -0
  144. package/lib/helpers/projection/parseProjection.js +33 -0
  145. package/lib/helpers/query/applyGlobalOption.js +29 -0
  146. package/lib/helpers/query/cast$expr.js +287 -0
  147. package/lib/helpers/query/castFilterPath.js +54 -0
  148. package/lib/helpers/query/castUpdate.js +643 -0
  149. package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +103 -0
  150. package/lib/helpers/query/handleImmutable.js +44 -0
  151. package/lib/helpers/query/handleReadPreferenceAliases.js +23 -0
  152. package/lib/helpers/query/hasDollarKeys.js +23 -0
  153. package/lib/helpers/query/isOperator.js +14 -0
  154. package/lib/helpers/query/sanitizeFilter.js +38 -0
  155. package/lib/helpers/query/sanitizeProjection.js +14 -0
  156. package/lib/helpers/query/selectPopulatedFields.js +62 -0
  157. package/lib/helpers/query/trusted.js +13 -0
  158. package/lib/helpers/query/validOps.js +3 -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/applyReadConcern.js +20 -0
  163. package/lib/helpers/schema/applyWriteConcern.js +39 -0
  164. package/lib/helpers/schema/cleanPositionalOperators.js +12 -0
  165. package/lib/helpers/schema/getIndexes.js +171 -0
  166. package/lib/helpers/schema/getKeysInSchemaOrder.js +28 -0
  167. package/lib/helpers/schema/getPath.js +43 -0
  168. package/lib/helpers/schema/getSubdocumentStrictValue.js +32 -0
  169. package/lib/helpers/schema/handleIdOption.js +20 -0
  170. package/lib/helpers/schema/handleTimestampOption.js +24 -0
  171. package/lib/helpers/schema/idGetter.js +34 -0
  172. package/lib/helpers/schema/merge.js +36 -0
  173. package/lib/helpers/schematype/handleImmutable.js +50 -0
  174. package/lib/helpers/setDefaultsOnInsert.js +158 -0
  175. package/lib/helpers/specialProperties.js +3 -0
  176. package/lib/helpers/symbols.js +20 -0
  177. package/lib/helpers/timers.js +3 -0
  178. package/lib/helpers/timestamps/setDocumentTimestamps.js +26 -0
  179. package/lib/helpers/timestamps/setupTimestamps.js +116 -0
  180. package/lib/helpers/topology/allServersUnknown.js +12 -0
  181. package/lib/helpers/topology/isAtlas.js +31 -0
  182. package/lib/helpers/topology/isSSLError.js +16 -0
  183. package/lib/helpers/update/applyTimestampsToChildren.js +193 -0
  184. package/lib/helpers/update/applyTimestampsToUpdate.js +131 -0
  185. package/lib/helpers/update/castArrayFilters.js +113 -0
  186. package/lib/helpers/update/decorateUpdateWithVersionKey.js +35 -0
  187. package/lib/helpers/update/modifiedPaths.js +33 -0
  188. package/lib/helpers/update/moveImmutableProperties.js +53 -0
  189. package/lib/helpers/update/removeUnusedArrayFilters.js +32 -0
  190. package/lib/helpers/update/updatedPathsByArrayFilter.js +27 -0
  191. package/lib/helpers/updateValidators.js +193 -0
  192. package/lib/index.js +17 -0
  193. package/lib/internal.js +46 -0
  194. package/lib/model.js +5010 -0
  195. package/lib/modifiedPathsSnapshot.js +9 -0
  196. package/lib/mongoose.js +1411 -0
  197. package/lib/options/populateOptions.js +36 -0
  198. package/lib/options/propertyOptions.js +8 -0
  199. package/lib/options/saveOptions.js +16 -0
  200. package/lib/options/schemaArrayOptions.js +78 -0
  201. package/lib/options/schemaBufferOptions.js +38 -0
  202. package/lib/options/schemaDateOptions.js +71 -0
  203. package/lib/options/schemaDocumentArrayOptions.js +68 -0
  204. package/lib/options/schemaMapOptions.js +43 -0
  205. package/lib/options/schemaNumberOptions.js +101 -0
  206. package/lib/options/schemaObjectIdOptions.js +64 -0
  207. package/lib/options/schemaStringOptions.js +138 -0
  208. package/lib/options/schemaSubdocumentOptions.js +66 -0
  209. package/lib/options/schemaTypeOptions.js +244 -0
  210. package/lib/options/schemaUnionOptions.js +32 -0
  211. package/lib/options/virtualOptions.js +164 -0
  212. package/lib/options.js +17 -0
  213. package/lib/plugins/index.js +6 -0
  214. package/lib/plugins/saveSubdocs.js +76 -0
  215. package/lib/plugins/sharding.js +84 -0
  216. package/lib/plugins/trackTransaction.js +84 -0
  217. package/lib/plugins/validateBeforeSave.js +41 -0
  218. package/lib/query.js +5673 -0
  219. package/lib/queryHelpers.js +387 -0
  220. package/lib/schema/array.js +699 -0
  221. package/lib/schema/bigint.js +282 -0
  222. package/lib/schema/boolean.js +332 -0
  223. package/lib/schema/buffer.js +343 -0
  224. package/lib/schema/date.js +467 -0
  225. package/lib/schema/decimal128.js +263 -0
  226. package/lib/schema/documentArray.js +656 -0
  227. package/lib/schema/documentArrayElement.js +137 -0
  228. package/lib/schema/double.js +246 -0
  229. package/lib/schema/index.js +32 -0
  230. package/lib/schema/int32.js +289 -0
  231. package/lib/schema/map.js +201 -0
  232. package/lib/schema/mixed.js +146 -0
  233. package/lib/schema/number.js +510 -0
  234. package/lib/schema/objectId.js +333 -0
  235. package/lib/schema/operators/bitwise.js +38 -0
  236. package/lib/schema/operators/exists.js +12 -0
  237. package/lib/schema/operators/geospatial.js +107 -0
  238. package/lib/schema/operators/helpers.js +32 -0
  239. package/lib/schema/operators/text.js +39 -0
  240. package/lib/schema/operators/type.js +20 -0
  241. package/lib/schema/string.js +733 -0
  242. package/lib/schema/subdocument.js +436 -0
  243. package/lib/schema/symbols.js +5 -0
  244. package/lib/schema/union.js +113 -0
  245. package/lib/schema/uuid.js +305 -0
  246. package/lib/schema.js +3226 -0
  247. package/lib/schemaType.js +1835 -0
  248. package/lib/stateMachine.js +232 -0
  249. package/lib/types/array/index.js +119 -0
  250. package/lib/types/array/isMongooseArray.js +5 -0
  251. package/lib/types/array/methods/index.js +1095 -0
  252. package/lib/types/arraySubdocument.js +207 -0
  253. package/lib/types/buffer.js +294 -0
  254. package/lib/types/decimal128.js +13 -0
  255. package/lib/types/documentArray/index.js +113 -0
  256. package/lib/types/documentArray/isMongooseDocumentArray.js +5 -0
  257. package/lib/types/documentArray/methods/index.js +415 -0
  258. package/lib/types/double.js +13 -0
  259. package/lib/types/index.js +23 -0
  260. package/lib/types/map.js +419 -0
  261. package/lib/types/objectid.js +41 -0
  262. package/lib/types/subdocument.js +464 -0
  263. package/lib/types/uuid.js +13 -0
  264. package/lib/utils.js +1054 -0
  265. package/lib/validOptions.js +42 -0
  266. package/lib/virtualType.js +204 -0
  267. package/package.json +148 -0
  268. package/types/aggregate.d.ts +180 -0
  269. package/types/augmentations.d.ts +9 -0
  270. package/types/callback.d.ts +8 -0
  271. package/types/collection.d.ts +49 -0
  272. package/types/connection.d.ts +297 -0
  273. package/types/cursor.d.ts +67 -0
  274. package/types/document.d.ts +374 -0
  275. package/types/error.d.ts +143 -0
  276. package/types/expressions.d.ts +3053 -0
  277. package/types/helpers.d.ts +32 -0
  278. package/types/index.d.ts +1056 -0
  279. package/types/indexes.d.ts +97 -0
  280. package/types/inferhydrateddoctype.d.ts +115 -0
  281. package/types/inferrawdoctype.d.ts +135 -0
  282. package/types/inferschematype.d.ts +337 -0
  283. package/types/middlewares.d.ts +59 -0
  284. package/types/models.d.ts +1306 -0
  285. package/types/mongooseoptions.d.ts +228 -0
  286. package/types/pipelinestage.d.ts +333 -0
  287. package/types/populate.d.ts +53 -0
  288. package/types/query.d.ts +934 -0
  289. package/types/schemaoptions.d.ts +282 -0
  290. package/types/schematypes.d.ts +654 -0
  291. package/types/session.d.ts +32 -0
  292. package/types/types.d.ts +109 -0
  293. package/types/utility.d.ts +175 -0
  294. package/types/validation.d.ts +39 -0
  295. package/types/virtuals.d.ts +14 -0
@@ -0,0 +1,42 @@
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
+ 'autoSearchIndex',
15
+ 'bufferCommands',
16
+ 'bufferTimeoutMS',
17
+ 'cloneSchemas',
18
+ 'createInitialConnection',
19
+ 'debug',
20
+ 'forceRepopulate',
21
+ 'id',
22
+ 'maxTimeMS',
23
+ 'objectIdGetter',
24
+ 'overwriteModels',
25
+ 'returnOriginal',
26
+ 'runValidators',
27
+ 'sanitizeFilter',
28
+ 'sanitizeProjection',
29
+ 'selectPopulatedPaths',
30
+ 'setDefaultsOnInsert',
31
+ 'strict',
32
+ 'strictPopulate',
33
+ 'strictQuery',
34
+ 'timestamps.createdAt.immutable',
35
+ 'toJSON',
36
+ 'toObject',
37
+ 'transactionAsyncLocalStorage',
38
+ 'translateAliases',
39
+ 'updatePipeline'
40
+ ]);
41
+
42
+ module.exports = VALID_OPTIONS;
@@ -0,0 +1,204 @@
1
+ 'use strict';
2
+
3
+ const modelNamesFromRefPath = require('./helpers/populate/modelNamesFromRefPath');
4
+ const utils = require('./utils');
5
+
6
+ const modelSymbol = require('./helpers/symbols').modelSymbol;
7
+
8
+ /**
9
+ * VirtualType constructor
10
+ *
11
+ * This is what mongoose uses to define virtual attributes via `Schema.prototype.virtual`.
12
+ *
13
+ * #### Example:
14
+ *
15
+ * const fullname = schema.virtual('fullname');
16
+ * fullname instanceof mongoose.VirtualType // true
17
+ *
18
+ * @param {Object} options
19
+ * @param {String|Function} [options.ref] if `ref` is not nullish, this becomes a [populated virtual](https://mongoosejs.com/docs/populate.html#populate-virtuals)
20
+ * @param {String|Function} [options.localField] the local field to populate on if this is a populated virtual.
21
+ * @param {String|Function} [options.foreignField] the foreign field to populate on if this is a populated virtual.
22
+ * @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`.
23
+ * @param {Boolean} [options.getters=false] if you set this to `true`, Mongoose will call any custom getters you defined on this virtual
24
+ * @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()`](https://mongoosejs.com/docs/api/query.html#Query.prototype.countDocuments())
25
+ * @param {Object|Function} [options.match=null] add an extra match condition to `populate()`
26
+ * @param {Number} [options.limit=null] add a default `limit` to the `populate()` query
27
+ * @param {Number} [options.skip=null] add a default `skip` to the `populate()` query
28
+ * @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.
29
+ * @param {Object} [options.options=null] Additional options like `limit` and `lean`.
30
+ * @param {String} name
31
+ * @api public
32
+ */
33
+
34
+ function VirtualType(options, name) {
35
+ this.path = name;
36
+ this.getters = [];
37
+ this.setters = [];
38
+ this.options = Object.assign({}, options);
39
+ }
40
+
41
+ /**
42
+ * If no getters/setters, add a default
43
+ *
44
+ * @api private
45
+ */
46
+
47
+ VirtualType.prototype._applyDefaultGetters = function() {
48
+ if (this.getters.length > 0 || this.setters.length > 0) {
49
+ return;
50
+ }
51
+
52
+ const path = this.path;
53
+ const internalProperty = '$' + path;
54
+ this.getters.push(function() {
55
+ return this.$locals[internalProperty];
56
+ });
57
+ this.setters.push(function(v) {
58
+ this.$locals[internalProperty] = v;
59
+ });
60
+ };
61
+
62
+ /*!
63
+ * ignore
64
+ */
65
+
66
+ VirtualType.prototype.clone = function() {
67
+ const clone = new VirtualType(this.options, this.path);
68
+ clone.getters = [].concat(this.getters);
69
+ clone.setters = [].concat(this.setters);
70
+ return clone;
71
+ };
72
+
73
+ /**
74
+ * Adds a custom getter to this virtual.
75
+ *
76
+ * Mongoose calls the getter function with the below 3 parameters.
77
+ *
78
+ * - `value`: the value returned by the previous getter. If there is only one getter, `value` will be `undefined`.
79
+ * - `virtual`: the virtual object you called `.get()` on.
80
+ * - `doc`: the document this virtual is attached to. Equivalent to `this`.
81
+ *
82
+ * #### Example:
83
+ *
84
+ * const virtual = schema.virtual('fullname');
85
+ * virtual.get(function(value, virtual, doc) {
86
+ * return this.name.first + ' ' + this.name.last;
87
+ * });
88
+ *
89
+ * @param {Function} fn
90
+ * @return {VirtualType} this
91
+ * @api public
92
+ */
93
+
94
+ VirtualType.prototype.get = function(fn) {
95
+ this.getters.push(fn);
96
+ return this;
97
+ };
98
+
99
+ /**
100
+ * Adds a custom setter to this virtual.
101
+ *
102
+ * Mongoose calls the setter function with the below 3 parameters.
103
+ *
104
+ * - `value`: the value being set.
105
+ * - `virtual`: the virtual object you're calling `.set()` on.
106
+ * - `doc`: the document this virtual is attached to. Equivalent to `this`.
107
+ *
108
+ * #### Example:
109
+ *
110
+ * const virtual = schema.virtual('fullname');
111
+ * virtual.set(function(value, virtual, doc) {
112
+ * const parts = value.split(' ');
113
+ * this.name.first = parts[0];
114
+ * this.name.last = parts[1];
115
+ * });
116
+ *
117
+ * const Model = mongoose.model('Test', schema);
118
+ * const doc = new Model();
119
+ * // Calls the setter with `value = 'Jean-Luc Picard'`
120
+ * doc.fullname = 'Jean-Luc Picard';
121
+ * doc.name.first; // 'Jean-Luc'
122
+ * doc.name.last; // 'Picard'
123
+ *
124
+ * @param {Function} fn
125
+ * @return {VirtualType} this
126
+ * @api public
127
+ */
128
+
129
+ VirtualType.prototype.set = function(fn) {
130
+ this.setters.push(fn);
131
+ return this;
132
+ };
133
+
134
+ /**
135
+ * Applies getters to `value`.
136
+ *
137
+ * @param {Object} value
138
+ * @param {Document} doc The document this virtual is attached to
139
+ * @return {Any} the value after applying all getters
140
+ * @api public
141
+ */
142
+
143
+ VirtualType.prototype.applyGetters = function(value, doc) {
144
+ if (utils.hasUserDefinedProperty(this.options, ['ref', 'refPath']) &&
145
+ doc.$$populatedVirtuals &&
146
+ Object.hasOwn(doc.$$populatedVirtuals, this.path)) {
147
+ value = doc.$$populatedVirtuals[this.path];
148
+ }
149
+
150
+ let v = value;
151
+ for (const getter of this.getters) {
152
+ v = getter.call(doc, v, this, doc);
153
+ }
154
+ return v;
155
+ };
156
+
157
+ /**
158
+ * Applies setters to `value`.
159
+ *
160
+ * @param {Object} value
161
+ * @param {Document} doc
162
+ * @return {Any} the value after applying all setters
163
+ * @api public
164
+ */
165
+
166
+ VirtualType.prototype.applySetters = function(value, doc) {
167
+ let v = value;
168
+ for (const setter of this.setters) {
169
+ v = setter.call(doc, v, this, doc);
170
+ }
171
+ return v;
172
+ };
173
+
174
+ /**
175
+ * Get the names of models used to populate this model given a doc
176
+ *
177
+ * @param {Document} doc
178
+ * @return {Array<string> | null}
179
+ * @api private
180
+ */
181
+
182
+ VirtualType.prototype._getModelNamesForPopulate = function _getModelNamesForPopulate(doc) {
183
+ if (this.options.refPath) {
184
+ return modelNamesFromRefPath(this.options.refPath, doc, this.path);
185
+ }
186
+
187
+ let normalizedRef = null;
188
+ if (typeof this.options.ref === 'function' && !this.options.ref[modelSymbol]) {
189
+ normalizedRef = this.options.ref.call(doc, doc);
190
+ } else {
191
+ normalizedRef = this.options.ref;
192
+ }
193
+ if (normalizedRef != null && !Array.isArray(normalizedRef)) {
194
+ return [normalizedRef];
195
+ }
196
+
197
+ return normalizedRef;
198
+ };
199
+
200
+ /*!
201
+ * exports
202
+ */
203
+
204
+ module.exports = VirtualType;
package/package.json ADDED
@@ -0,0 +1,148 @@
1
+ {
2
+ "name": "@depup/mongoose",
3
+ "description": "Mongoose MongoDB ODM",
4
+ "version": "9.1.3-depup.0",
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
+ "type": "commonjs",
21
+ "license": "MIT",
22
+ "dependencies": {
23
+ "kareem": "^3.1.0",
24
+ "mongodb": "~7.0",
25
+ "mpath": "0.9.0",
26
+ "mquery": "6.0.0",
27
+ "ms": "2.1.3",
28
+ "sift": "17.1.3"
29
+ },
30
+ "devDependencies": {
31
+ "@ark/attest": "0.56.0",
32
+ "@mongodb-js/mongodb-downloader": "^1.1.4",
33
+ "acquit": "1.4.0",
34
+ "acquit-ignore": "^0.2.2",
35
+ "acquit-require": "0.1.1",
36
+ "ajv": "8.17.1",
37
+ "broken-link-checker": "^0.7.8",
38
+ "cheerio": "1.1.2",
39
+ "dox": "1.0.0",
40
+ "eslint": "9.39.2",
41
+ "eslint-plugin-markdown": "^5.1.0",
42
+ "eslint-plugin-mocha-no-only": "1.2.0",
43
+ "express": "^5.2.1",
44
+ "fs-extra": "^11.3.3",
45
+ "highlight.js": "11.11.1",
46
+ "lodash.isequal": "4.5.0",
47
+ "lodash.isequalwith": "4.4.0",
48
+ "markdownlint-cli2": "^0.20.0",
49
+ "marked": "15.x",
50
+ "mkdirp": "^3.0.1",
51
+ "mocha": "11.7.5",
52
+ "moment": "2.30.1",
53
+ "mongodb-memory-server": "11.0.1",
54
+ "mongodb-runner": "^6.5.2",
55
+ "mongodb-client-encryption": "~7.0",
56
+ "ncp": "^2.0.0",
57
+ "nyc": "^17.1.0",
58
+ "pug": "3.0.3",
59
+ "sinon": "21.0.1",
60
+ "tsd": "0.33.0",
61
+ "typescript": "5.9.3",
62
+ "typescript-eslint": "^8.53.0",
63
+ "uuid": "^13.0.0"
64
+ },
65
+ "directories": {
66
+ "lib": "./lib/mongoose"
67
+ },
68
+ "scripts": {
69
+ "docs:clean": "npm run docs:clean:stable",
70
+ "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",
71
+ "docs:clean:5x": "rimraf index.html && rimraf -rf ./docs/5.x && rimraf -rf ./docs/source/_docs && rimraf -rf ./tmp",
72
+ "docs:clean:6x": "rimraf index.html && rimraf -rf ./docs/6.x && rimraf -rf ./docs/source/_docs && rimraf -rf ./tmp",
73
+ "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/",
74
+ "docs:copy:tmp:5x": "rimraf ./docs/5.x && ncp ./tmp ./docs/5.x",
75
+ "docs:copy:tmp:6x": "rimraf ./docs/6.x && ncp ./tmp ./docs/6.x",
76
+ "docs:generate": "node ./scripts/website.js",
77
+ "docs:generate:sponsorData": "node ./scripts/loadSponsorData.js",
78
+ "docs:test": "npm run docs:generate",
79
+ "docs:view": "node ./scripts/static.js",
80
+ "docs:prepare:publish:stable": "git checkout gh-pages && git merge master && npm run docs:generate",
81
+ "docs:prepare:publish:5x": "git checkout 5.x && git merge 5.x && npm run docs:clean:stable && npm run docs:generate && npm run docs:copy:tmp && git checkout gh-pages && npm run docs:copy:tmp:5x",
82
+ "docs:prepare:publish:6x": "git checkout 6.x && git merge 6.x && npm run docs:clean:stable && env DOCS_DEPLOY=true npm run docs:generate && mv ./docs/6.x ./tmp && git checkout gh-pages && npm run docs:copy:tmp:6x",
83
+ "docs:prepare:publish:7x": "env DOCS_DEPLOY=true npm run docs:generate && git checkout gh-pages && rimraf ./docs/7.x && mv ./tmp ./docs/7.x",
84
+ "docs:prepare:publish:8x": "env DOCS_DEPLOY=true npm run docs:generate && git checkout gh-pages && rimraf ./docs/8.x && mv ./tmp ./docs/8.x",
85
+ "docs:check-links": "blc http://127.0.0.1:8089 -ro",
86
+ "lint": "eslint .",
87
+ "lint-js": "eslint . --ext .js --ext .cjs",
88
+ "lint-ts": "eslint . --ext .ts",
89
+ "lint-md": "markdownlint-cli2 \"**/*.md\" \"#node_modules\" \"#benchmarks\"",
90
+ "release": "git pull && git push origin master --tags && npm publish",
91
+ "release-5x": "git pull origin 5.x && git push origin 5.x && git push origin 5.x --tags && npm publish --tag 5x",
92
+ "release-6x": "git pull origin 6.x && git push origin 6.x && git push origin 6.x --tags && npm publish --tag 6x",
93
+ "mongo": "node ./tools/repl.js",
94
+ "publish-7x": "npm publish --tag 7x",
95
+ "create-separate-require-instance": "rm -rf ./node_modules/mongoose-separate-require-instance && node ./scripts/create-tarball && tar -xzf mongoose.tgz -C ./node_modules && mv ./node_modules/package ./node_modules/mongoose-separate-require-instance",
96
+ "test": "mocha --exit ./test/*.test.js",
97
+ "test:ci": "npm run test -- --reporter min",
98
+ "test-deno": "deno run --allow-env --allow-read --allow-net --allow-run --allow-sys --allow-write ./test/deno.mjs",
99
+ "test-deno:ci": "npm run test-deno -- --reporter min",
100
+ "test-rs": "START_REPLICA_SET=1 mocha --timeout 30000 --exit ./test/*.test.js",
101
+ "test-rs:ci": "npm run test-rs -- --reporter min",
102
+ "test-tsd": "node ./test/types/check-types-filename && tsd --full",
103
+ "setup-test-encryption": "node scripts/setup-encryption-tests.js",
104
+ "test-encryption": "mocha --exit ./test/encryption/*.test.js",
105
+ "test-encryption:ci": "npm run test-encryption -- --reporter min",
106
+ "tdd": "mocha --watch --inspect --recursive ./test/*.test.js --watch-files lib/**/*.js test/**/*.js",
107
+ "test-coverage": "nyc --reporter=html --reporter=text npm test",
108
+ "test-coverage:ci": "nyc --reporter=html --reporter=text npm run test:ci",
109
+ "ts-benchmark": "cd ./benchmarks/typescript/simple && npm install && npm run benchmark | node ../../../scripts/tsc-diagnostics-check",
110
+ "ts-benchmark:local": "node ./scripts/create-tarball && cd ./benchmarks/typescript/simple && rm -rf ./node_modules && npm install && npm run benchmark | node ../../../scripts/tsc-diagnostics-check",
111
+ "attest-benchmark": "node ./benchmarks/typescript/infer.bench.mts"
112
+ },
113
+ "main": "./index.js",
114
+ "types": "./types/index.d.ts",
115
+ "engines": {
116
+ "node": ">=20.19.0"
117
+ },
118
+ "bugs": {
119
+ "url": "https://github.com/Automattic/mongoose/issues/new"
120
+ },
121
+ "repository": {
122
+ "type": "git",
123
+ "url": "git://github.com/Automattic/mongoose.git"
124
+ },
125
+ "homepage": "https://mongoosejs.com",
126
+ "config": {
127
+ "mongodbMemoryServer": {
128
+ "disablePostinstall": true
129
+ }
130
+ },
131
+ "funding": {
132
+ "type": "opencollective",
133
+ "url": "https://opencollective.com/mongoose"
134
+ },
135
+ "tsd": {
136
+ "directory": "test/types",
137
+ "compilerOptions": {
138
+ "esModuleInterop": false,
139
+ "strict": true,
140
+ "allowSyntheticDefaultImports": true,
141
+ "strictPropertyInitialization": false,
142
+ "noImplicitAny": false,
143
+ "strictNullChecks": true,
144
+ "module": "commonjs",
145
+ "target": "ES2022"
146
+ }
147
+ }
148
+ }
@@ -0,0 +1,180 @@
1
+ declare module 'mongoose' {
2
+ import mongodb = require('mongodb');
3
+
4
+ /** Extract generic type from Aggregate class */
5
+ type AggregateExtract<P> = P extends Aggregate<infer T> ? T : never;
6
+
7
+ interface AggregateOptions extends Omit<mongodb.AggregateOptions & mongodb.Abortable, 'session'>, SessionOption {
8
+ [key: string]: any;
9
+ }
10
+
11
+ class Aggregate<ResultType> implements SessionOperation {
12
+ /**
13
+ * Returns an asyncIterator for use with [`for/await/of` loops](https://thecodebarbarian.com/getting-started-with-async-iterators-in-node-js)
14
+ * You do not need to call this function explicitly, the JavaScript runtime
15
+ * will call it for you.
16
+ */
17
+ [Symbol.asyncIterator](): AsyncIterableIterator<Unpacked<ResultType>>;
18
+
19
+ // Returns a string representation of this aggregation.
20
+ [Symbol.toStringTag]: string;
21
+
22
+ options: AggregateOptions;
23
+
24
+ /**
25
+ * Sets an option on this aggregation. This function will be deprecated in a
26
+ * future release.
27
+ *
28
+ * @deprecated
29
+ */
30
+ addCursorFlag(flag: CursorFlag, value: boolean): this;
31
+
32
+ /**
33
+ * Appends a new $addFields operator to this aggregate pipeline.
34
+ * Requires MongoDB v3.4+ to work
35
+ */
36
+ addFields(arg: PipelineStage.AddFields['$addFields']): this;
37
+
38
+ /** Sets the allowDiskUse option for the aggregation query */
39
+ allowDiskUse(value: boolean): this;
40
+
41
+ /** Appends new operators to this aggregate pipeline */
42
+ append(...args: PipelineStage[]): this;
43
+
44
+ /**
45
+ * Executes the query returning a `Promise` which will be
46
+ * resolved with either the doc(s) or rejected with the error.
47
+ * Like [`.then()`](#query_Query-then), but only takes a rejection handler.
48
+ */
49
+ catch: Promise<ResultType>['catch'];
50
+
51
+ /** Set the collation. */
52
+ collation(options: mongodb.CollationOptions): this;
53
+
54
+ /** Appends a new $count operator to this aggregate pipeline. */
55
+ count(fieldName: PipelineStage.Count['$count']): this;
56
+
57
+ /** Appends a new $densify operator to this aggregate pipeline */
58
+ densify(arg: PipelineStage.Densify['$densify']): this;
59
+
60
+ /**
61
+ * Sets the cursor option for the aggregation query
62
+ */
63
+ cursor<DocType = any>(options?: Record<string, unknown>): Cursor<DocType>;
64
+
65
+
66
+ /** Executes the aggregate pipeline on the currently bound Model. */
67
+ exec(): Promise<ResultType>;
68
+
69
+ /** Execute the aggregation with explain */
70
+ explain(verbosity: mongodb.ExplainVerbosityLike): Promise<AnyObject>;
71
+ explain(): Promise<AnyObject>;
72
+
73
+ /** Combines multiple aggregation pipelines. */
74
+ facet(options: PipelineStage.Facet['$facet']): this;
75
+
76
+ /** Appends a new $fill operator to this aggregate pipeline */
77
+ fill(arg: PipelineStage.Fill['$fill']): this;
78
+
79
+ /**
80
+ * Executes the aggregation returning a `Promise` which will be
81
+ * resolved with `.finally()` chained.
82
+ */
83
+ finally: Promise<ResultType>['finally'];
84
+
85
+ /** Appends new custom $graphLookup operator(s) to this aggregate pipeline, performing a recursive search on a collection. */
86
+ graphLookup(options: PipelineStage.GraphLookup['$graphLookup']): this;
87
+
88
+ /** Appends new custom $group operator to this aggregate pipeline. */
89
+ group(arg: PipelineStage.Group['$group']): this;
90
+
91
+ /** Sets the hint option for the aggregation query */
92
+ hint(value: Record<string, unknown> | string): this;
93
+
94
+ /**
95
+ * Appends a new $limit operator to this aggregate pipeline.
96
+ * @param num maximum number of records to pass to the next stage
97
+ */
98
+ limit(num: PipelineStage.Limit['$limit']): this;
99
+
100
+ /** Appends new custom $lookup operator to this aggregate pipeline. */
101
+ lookup(options: PipelineStage.Lookup['$lookup']): this;
102
+
103
+ /**
104
+ * Appends a new custom $match operator to this aggregate pipeline.
105
+ * @param arg $match operator contents
106
+ */
107
+ match(arg: PipelineStage.Match['$match']): this;
108
+
109
+ /**
110
+ * Binds this aggregate to a model.
111
+ * @param model the model to which the aggregate is to be bound
112
+ */
113
+ model(model: Model<any>): this;
114
+
115
+ /**
116
+ * Returns the current model bound to this aggregate object
117
+ */
118
+ model(): Model<any>;
119
+
120
+ /** Appends a new $geoNear operator to this aggregate pipeline. */
121
+ near(arg: PipelineStage.GeoNear['$geoNear']): this;
122
+
123
+ /** Returns the current pipeline */
124
+ pipeline(): PipelineStage[];
125
+
126
+ /** Appends a new $project operator to this aggregate pipeline. */
127
+ project(arg: PipelineStage.Project['$project'] | string): this;
128
+
129
+ /** Sets the readPreference option for the aggregation query. */
130
+ read(pref: mongodb.ReadPreferenceLike): this;
131
+
132
+ /** Sets the readConcern level for the aggregation query. */
133
+ readConcern(level: string): this;
134
+
135
+ /** Appends a new $redact operator to this aggregate pipeline. */
136
+ redact(expression: PipelineStage.Redact['$redact'], thenExpr: '$$DESCEND' | '$$PRUNE' | '$$KEEP' | AnyObject, elseExpr: '$$DESCEND' | '$$PRUNE' | '$$KEEP' | AnyObject): this;
137
+
138
+ /** Appends a new $replaceRoot operator to this aggregate pipeline. */
139
+ replaceRoot(newRoot: PipelineStage.ReplaceRoot['$replaceRoot']['newRoot'] | string): this;
140
+
141
+ /**
142
+ * Helper for [Atlas Text Search](https://www.mongodb.com/docs/atlas/atlas-search/tutorial/)'s
143
+ * `$search` stage.
144
+ */
145
+ search(options: PipelineStage.Search['$search']): this;
146
+
147
+ /** Lets you set arbitrary options, for middlewares or plugins. */
148
+ option(value: AggregateOptions): this;
149
+
150
+ /** Appends new custom $sample operator to this aggregate pipeline. */
151
+ sample(arg: PipelineStage.Sample['$sample']['size']): this;
152
+
153
+ /** Sets the session for this aggregation. Useful for [transactions](/docs/transactions.html). */
154
+ session(session: mongodb.ClientSession | null): this;
155
+
156
+ /**
157
+ * Appends a new $skip operator to this aggregate pipeline.
158
+ * @param num number of records to skip before next stage
159
+ */
160
+ skip(num: PipelineStage.Skip['$skip']): this;
161
+
162
+ /** Appends a new $sort operator to this aggregate pipeline. */
163
+ sort(arg: string | Record<string, SortValues> | PipelineStage.Sort['$sort']): this;
164
+
165
+ /** Provides promise for aggregate. */
166
+ then: Promise<ResultType>['then'];
167
+
168
+ /**
169
+ * Appends a new $sortByCount operator to this aggregate pipeline. Accepts either a string field name
170
+ * or a pipeline object.
171
+ */
172
+ sortByCount(arg: string | PipelineStage.SortByCount['$sortByCount']): this;
173
+
174
+ /** Appends new $unionWith operator to this aggregate pipeline. */
175
+ unionWith(options: PipelineStage.UnionWith['$unionWith']): this;
176
+
177
+ /** Appends new custom $unwind operator(s) to this aggregate pipeline. */
178
+ unwind(...args: PipelineStage.Unwind['$unwind'][]): this;
179
+ }
180
+ }
@@ -0,0 +1,9 @@
1
+ // this import is required so that types get merged instead of completely overwritten
2
+ import 'bson';
3
+
4
+ declare module 'bson' {
5
+ interface ObjectId {
6
+ /** Mongoose automatically adds a conveniency "_id" getter on the base ObjectId class */
7
+ _id: this;
8
+ }
9
+ }
@@ -0,0 +1,8 @@
1
+ declare module 'mongoose' {
2
+ type CallbackError = NativeError | null;
3
+
4
+ type Callback<T = any> = (error: CallbackError, result: T) => void;
5
+
6
+ type CallbackWithoutResult = (error: CallbackError) => void;
7
+ type CallbackWithoutResultAndOptionalError = (error?: CallbackError) => void;
8
+ }
@@ -0,0 +1,49 @@
1
+ declare module 'mongoose' {
2
+ import mongodb = require('mongodb');
3
+
4
+ export class BaseCollection<T extends mongodb.Document> extends mongodb.Collection<T> {
5
+ /**
6
+ * Collection constructor
7
+ * @param name name of the collection
8
+ * @param conn A MongooseConnection instance
9
+ * @param opts optional collection options
10
+ */
11
+ constructor(name: string, conn: Connection, opts?: any);
12
+
13
+ /*
14
+ * Abstract methods. Some of these are already defined on the
15
+ * mongodb.Collection interface so they've been commented out.
16
+ */
17
+ ensureIndex(...args: any[]): any;
18
+ findAndModify(...args: any[]): any;
19
+ getIndexes(...args: any[]): any;
20
+
21
+ /** Formatter for debug print args */
22
+ $format(arg: any, color?: boolean, shell?: boolean): string;
23
+ /** Debug print helper */
24
+ $print(name: string, i: string | number, args: any[], color?: boolean, shell?: boolean): void;
25
+
26
+ /** The collection name */
27
+ get collectionName(): string;
28
+ /** The Connection instance */
29
+ conn: Connection;
30
+ /** The collection name */
31
+ name: string;
32
+ }
33
+
34
+ /*
35
+ * section drivers/node-mongodb-native/collection.js
36
+ */
37
+ class Collection<T extends mongodb.Document = mongodb.Document> extends BaseCollection<T> {
38
+ /**
39
+ * Collection constructor
40
+ * @param name name of the collection
41
+ * @param conn A MongooseConnection instance
42
+ * @param opts optional collection options
43
+ */
44
+ constructor(name: string, conn: Connection, opts?: any);
45
+
46
+ /** Retrieves information about this collections indexes. */
47
+ getIndexes(): ReturnType<mongodb.Collection<T>['indexInformation']>;
48
+ }
49
+ }