@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,374 @@
1
+ declare module 'mongoose' {
2
+ import mongodb = require('mongodb');
3
+
4
+ /** A list of paths to skip. If set, Mongoose will validate every modified path that is not in this list. */
5
+ type pathsToSkip = string[] | string;
6
+
7
+ interface DocumentSetOptions {
8
+ merge?: boolean;
9
+
10
+ [key: string]: any;
11
+ }
12
+
13
+ class ModifiedPathsSnapshot {}
14
+
15
+ /**
16
+ * Generic types for Document:
17
+ * * T - the type of _id
18
+ * * TQueryHelpers - Object with any helpers that should be mixed into the Query type
19
+ * * DocType - the type of the actual Document created
20
+ */
21
+ class Document<T = Types.ObjectId, TQueryHelpers = any, DocType = any, TVirtuals = Record<string, any>, TSchemaOptions = {}> {
22
+ constructor(doc?: any);
23
+
24
+ /** This documents _id. */
25
+ _id: T;
26
+
27
+ /** Assert that a given path or paths is populated. Throws an error if not populated. */
28
+ $assertPopulated<Paths = {}>(path: string | string[], values?: Partial<Paths>): Omit<this, keyof Paths> & Paths;
29
+
30
+ /** Clear the document's modified paths. */
31
+ $clearModifiedPaths(): this;
32
+
33
+ /** Returns a deep clone of this document */
34
+ $clone(): this;
35
+
36
+ /**
37
+ * Creates a snapshot of this document's internal change tracking state. You can later
38
+ * reset this document's change tracking state using `$restoreModifiedPathsSnapshot()`.
39
+ */
40
+ $createModifiedPathsSnapshot(): ModifiedPathsSnapshot;
41
+
42
+ /* Get all subdocs (by bfs) */
43
+ $getAllSubdocs(): Document[];
44
+
45
+ /** Don't run validation on this path or persist changes to this path. */
46
+ $ignore(path: string): void;
47
+
48
+ /** Checks if a path is set to its default. If no path set, checks if any path is set to its default. */
49
+ $isDefault(path?: string): boolean;
50
+
51
+ /** Getter/setter, determines whether the document was removed or not. */
52
+ $isDeleted(val?: boolean): boolean;
53
+
54
+ /** Returns an array of all populated documents associated with the query */
55
+ $getPopulatedDocs(): Document[];
56
+
57
+ /**
58
+ * Increments the numeric value at `path` by the given `val`.
59
+ * When you call `save()` on this document, Mongoose will send a
60
+ * `$inc` as opposed to a `$set`.
61
+ */
62
+ $inc(path: string | string[], val?: number): this;
63
+
64
+ /**
65
+ * Returns true if the given path is nullish or only contains empty objects.
66
+ * Useful for determining whether this subdoc will get stripped out by the
67
+ * [minimize option](/docs/guide.html#minimize).
68
+ */
69
+ $isEmpty(path: string): boolean;
70
+
71
+ /** Checks if a path is invalid */
72
+ $isValid(path: string): boolean;
73
+
74
+ /**
75
+ * Empty object that you can use for storing properties on the document. This
76
+ * is handy for passing data to middleware without conflicting with Mongoose
77
+ * internals.
78
+ */
79
+ $locals: Record<string, unknown>;
80
+
81
+ /** Marks a path as valid, removing existing validation errors. */
82
+ $markValid(path: string): void;
83
+
84
+ /** Returns the model with the given name on this document's associated connection. */
85
+ $model<ModelType = Model<unknown>>(name: string): ModelType;
86
+ $model<ModelType = Model<DocType>>(): ModelType;
87
+
88
+ /**
89
+ * A string containing the current operation that Mongoose is executing
90
+ * on this document. Can be `null`, `'save'`, `'validate'`, or `'remove'`.
91
+ */
92
+ $op: 'save' | 'validate' | 'remove' | null;
93
+
94
+ /**
95
+ * Restore this document's change tracking state to the given snapshot.
96
+ * Note that `$restoreModifiedPathsSnapshot()` does **not** modify the document's
97
+ * properties, just resets the change tracking state.
98
+ */
99
+ $restoreModifiedPathsSnapshot(snapshot: ModifiedPathsSnapshot): this;
100
+
101
+ /**
102
+ * Getter/setter around the session associated with this document. Used to
103
+ * automatically set `session` if you `save()` a doc that you got from a
104
+ * query with an associated session.
105
+ */
106
+ $session(session?: ClientSession | null): ClientSession | null;
107
+
108
+ /** Alias for `set()`, used internally to avoid conflicts */
109
+ $set(path: string | Record<string, any>, val: any, type: any, options?: DocumentSetOptions): this;
110
+ $set(path: string | Record<string, any>, val: any, options?: DocumentSetOptions): this;
111
+ $set(value: string | Record<string, any>): this;
112
+
113
+ /** Set this property to add additional query filters when Mongoose saves this document and `isNew` is false. */
114
+ $where: Record<string, unknown>;
115
+
116
+ /** If this is a discriminator model, `baseModelName` is the name of the base model. */
117
+ baseModelName?: string;
118
+
119
+ /** Collection the model uses. */
120
+ collection: Collection;
121
+
122
+ /** Connection the model uses. */
123
+ db: Connection;
124
+
125
+ /** Removes this document from the db. */
126
+ deleteOne(options?: QueryOptions): QueryWithHelpers<
127
+ mongodb.DeleteResult,
128
+ this,
129
+ TQueryHelpers,
130
+ DocType,
131
+ 'deleteOne'
132
+ >;
133
+
134
+ /**
135
+ * Takes a populated field and returns it to its unpopulated state. If called with
136
+ * no arguments, then all populated fields are returned to their unpopulated state.
137
+ */
138
+ depopulate<Paths = {}>(path?: string | string[]): MergeType<this, Paths>;
139
+
140
+ /**
141
+ * Returns the list of paths that have been directly modified. A direct
142
+ * modified path is a path that you explicitly set, whether via `doc.foo = 'bar'`,
143
+ * `Object.assign(doc, { foo: 'bar' })`, or `doc.set('foo', 'bar')`.
144
+ */
145
+ directModifiedPaths(): Array<string>;
146
+
147
+ /**
148
+ * Returns true if this document is equal to another document.
149
+ *
150
+ * Documents are considered equal when they have matching `_id`s, unless neither
151
+ * document has an `_id`, in which case this function falls back to using
152
+ * `deepEqual()`.
153
+ */
154
+ equals(doc: Document<T>): boolean;
155
+
156
+ /** Returns the current validation errors. */
157
+ errors?: Error.ValidationError;
158
+
159
+ /** Returns the value of a path. */
160
+ get<T extends keyof DocType>(path: T, type?: any, options?: any): DocType[T];
161
+ get(path: string, type?: any, options?: any): any;
162
+
163
+ /**
164
+ * Returns the changes that happened to the document
165
+ * in the format that will be sent to MongoDB.
166
+ */
167
+ getChanges(): UpdateQuery<this>;
168
+
169
+ /** Signal that we desire an increment of this documents version. */
170
+ increment(): this;
171
+
172
+ /**
173
+ * Initializes the document without setters or marking anything modified.
174
+ * Called internally after a document is returned from mongodb. Normally,
175
+ * you do **not** need to call this function on your own.
176
+ */
177
+ init(obj: AnyObject, opts?: AnyObject): this;
178
+
179
+ /** Marks a path as invalid, causing validation to fail. */
180
+ invalidate<T extends keyof DocType>(path: T, errorMsg: string | NativeError, value?: any, kind?: string): NativeError | null;
181
+ invalidate(path: string, errorMsg: string | NativeError, value?: any, kind?: string): NativeError | null;
182
+
183
+ /** Returns true if `path` was directly set and modified, else false. */
184
+ isDirectModified<T extends keyof DocType>(path: T | Array<T>): boolean;
185
+ isDirectModified(path: string | Array<string>): boolean;
186
+
187
+ /** Checks if `path` was explicitly selected. If no projection, always returns true. */
188
+ isDirectSelected<T extends keyof DocType>(path: T): boolean;
189
+ isDirectSelected(path: string): boolean;
190
+
191
+ /** Checks if `path` is in the `init` state, that is, it was set by `Document#init()` and not modified since. */
192
+ isInit<T extends keyof DocType>(path: T): boolean;
193
+ isInit(path: string): boolean;
194
+
195
+ /**
196
+ * Returns true if any of the given paths are modified, else false. If no arguments, returns `true` if any path
197
+ * in this document is modified.
198
+ */
199
+ isModified<T extends keyof DocType>(path?: T | Array<T>, options?: { ignoreAtomics?: boolean } | null): boolean;
200
+ isModified(path?: string | Array<string>, options?: { ignoreAtomics?: boolean } | null): boolean;
201
+
202
+ /** Boolean flag specifying if the document is new. */
203
+ isNew: boolean;
204
+
205
+ /** Checks if `path` was selected in the source query which initialized this document. */
206
+ isSelected<T extends keyof DocType>(path: T): boolean;
207
+ isSelected(path: string): boolean;
208
+
209
+ /** Marks the path as having pending changes to write to the db. */
210
+ markModified<T extends keyof DocType>(path: T, scope?: any): void;
211
+ markModified(path: string, scope?: any): void;
212
+
213
+ /** Returns the model with the given name on this document's associated connection. */
214
+ model<ModelType = Model<unknown>>(name: string): ModelType;
215
+ model<ModelType = Model<DocType>>(): ModelType;
216
+
217
+ /** Returns the list of paths that have been modified. */
218
+ modifiedPaths(options?: { includeChildren?: boolean }): Array<string>;
219
+
220
+ /**
221
+ * Overwrite all values in this document with the values of `obj`, except
222
+ * for immutable properties. Behaves similarly to `set()`, except for it
223
+ * unsets all properties that aren't in `obj`.
224
+ */
225
+ overwrite(obj: AnyObject): this;
226
+
227
+ /**
228
+ * If this document is a subdocument or populated document, returns the
229
+ * document's parent. Returns undefined otherwise.
230
+ */
231
+ $parent(): Document | undefined;
232
+
233
+ /** Populates document references. */
234
+ populate<Paths = {}>(path: string | PopulateOptions | (string | PopulateOptions)[]): Promise<MergeType<this, Paths>>;
235
+ populate<Paths = {}>(path: string, select?: string | AnyObject, model?: Model<any>, match?: AnyObject, options?: PopulateOptions): Promise<MergeType<this, Paths>>;
236
+
237
+ /** Gets _id(s) used during population of the given `path`. If the path was not populated, returns `undefined`. */
238
+ populated(path: string): any;
239
+
240
+ /** Sends a replaceOne command with this document `_id` as the query selector. */
241
+ replaceOne(replacement?: AnyObject, options?: QueryOptions | null): Query<any, this>;
242
+
243
+ /** Saves this document by inserting a new document into the database if [document.isNew](/docs/api/document.html#document_Document-isNew) is `true`, or sends an [updateOne](/docs/api/document.html#document_Document-updateOne) operation with just the modified paths if `isNew` is `false`. */
244
+ save(options?: SaveOptions): Promise<this>;
245
+
246
+ /** The document's schema. */
247
+ schema: Schema;
248
+
249
+ /** Sets the value of a path, or many paths. */
250
+ set<T extends keyof DocType>(path: T, val: DocType[T], type: any, options?: DocumentSetOptions): this;
251
+ set(path: string | Record<string, any>, val: any, type: any, options?: DocumentSetOptions): this;
252
+ set(path: string | Record<string, any>, val: any, options?: DocumentSetOptions): this;
253
+ set(value: string | Record<string, any>): this;
254
+
255
+ /** The return value of this method is used in calls to JSON.stringify(doc). */
256
+ // Consistently handle combinations of flattenMaps, flattenObjectIds, virtuals, and versionKey
257
+
258
+ // All combinations, including with and without TVirtuals when virtuals is true/false,
259
+ // and proper nesting/order: flattenMaps -> ObjectIdToString -> Omit<..., '__v'> when needed.
260
+
261
+ // flattenMaps: false (default) cases
262
+ toJSON(options: ToObjectOptions & { flattenMaps: false, flattenObjectIds: true, virtuals: true, versionKey: false }): ObjectIdToString<Omit<Require_id<DocType & TVirtuals>, '__v'>>;
263
+ toJSON(options: ToObjectOptions & { flattenMaps: false, flattenObjectIds: true, virtuals: true }): ObjectIdToString<Require_id<DocType & TVirtuals>>;
264
+ toJSON(options: ToObjectOptions & { flattenMaps: false, flattenObjectIds: false, virtuals: true, versionKey: false }): Omit<Require_id<DocType & TVirtuals>, '__v'>;
265
+ toJSON(options: ToObjectOptions & { flattenMaps: false, flattenObjectIds: false, virtuals: true }): Require_id<DocType & TVirtuals>;
266
+ toJSON(options: ToObjectOptions & { flattenMaps: false, flattenObjectIds: true, virtuals: false, versionKey: false }): ObjectIdToString<Omit<Require_id<DocType>, '__v'>>;
267
+ toJSON(options: ToObjectOptions & { flattenMaps: false, flattenObjectIds: true, virtuals: false }): ObjectIdToString<Require_id<DocType>>;
268
+ toJSON(options: ToObjectOptions & { flattenMaps: false, flattenObjectIds: false, virtuals: false, versionKey: false }): Omit<Require_id<DocType>, '__v'>;
269
+ toJSON(options: ToObjectOptions & { flattenMaps: false, flattenObjectIds: false, virtuals: false }): Require_id<DocType>;
270
+
271
+ // flattenMaps: true cases
272
+ toJSON(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: true, virtuals: true, versionKey: false }): ObjectIdToString<Omit<FlattenMaps<Require_id<DocType & TVirtuals>>, '__v'>>;
273
+ toJSON(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: true, virtuals: true }): ObjectIdToString<FlattenMaps<Require_id<DocType & TVirtuals>>>;
274
+ toJSON(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: false, virtuals: true, versionKey: false }): Omit<FlattenMaps<Require_id<DocType & TVirtuals>>, '__v'>;
275
+ toJSON(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: false, virtuals: true }): FlattenMaps<Require_id<DocType & TVirtuals>>;
276
+ toJSON(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: true, virtuals: false, versionKey: false }): ObjectIdToString<Omit<FlattenMaps<Require_id<DocType>>, '__v'>>;
277
+ toJSON(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: true, virtuals: false }): ObjectIdToString<FlattenMaps<Require_id<DocType>>>;
278
+ toJSON(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: false, virtuals: false, versionKey: false }): Omit<FlattenMaps<Require_id<DocType>>, '__v'>;
279
+ toJSON(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: false, virtuals: false }): FlattenMaps<Require_id<DocType>>;
280
+
281
+ // Handle "at least" short cross-combinations
282
+ // (Some permutations above will catch these, but make some explicit for clarity)
283
+ toJSON(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: true, virtuals: true }): ObjectIdToString<FlattenMaps<Require_id<DocType & TVirtuals>>>;
284
+ toJSON(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: true }): ObjectIdToString<FlattenMaps<Require_id<DocType>>>;
285
+ toJSON(options: ToObjectOptions & { flattenObjectIds: true, virtuals: true }): ObjectIdToString<Require_id<DocType & TVirtuals>>;
286
+ toJSON(options: ToObjectOptions & { flattenObjectIds: true }): ObjectIdToString<Require_id<DocType>>;
287
+ toJSON(options: ToObjectOptions & { flattenMaps: true, virtuals: true }): FlattenMaps<Require_id<DocType & TVirtuals>>;
288
+ toJSON(options: ToObjectOptions & { flattenMaps: true }): FlattenMaps<Require_id<DocType>>;
289
+
290
+ // Handle versionKey: false (regardless of the others - most specific overloads should come first)
291
+ toJSON(options: ToObjectOptions & { versionKey: false, flattenMaps: true, flattenObjectIds: true, virtuals: true }): ObjectIdToString<Omit<FlattenMaps<Require_id<DocType & TVirtuals>>, '__v'>>;
292
+ toJSON(options: ToObjectOptions & { versionKey: false, flattenMaps: false, flattenObjectIds: true, virtuals: true }): ObjectIdToString<Omit<Require_id<DocType & TVirtuals>, '__v'>>;
293
+ toJSON(options: ToObjectOptions & { versionKey: false, flattenMaps: true, virtuals: true }): Omit<FlattenMaps<Require_id<DocType & TVirtuals>>, '__v'>;
294
+ toJSON(options: ToObjectOptions & { versionKey: false, flattenObjectIds: true, virtuals: true }): ObjectIdToString<Omit<Require_id<DocType & TVirtuals>, '__v'>>;
295
+ toJSON(options: ToObjectOptions & { versionKey: false, virtuals: true }): Omit<Require_id<DocType & TVirtuals>, '__v'>;
296
+ toJSON(options: ToObjectOptions & { versionKey: false, flattenMaps: true, flattenObjectIds: true }): ObjectIdToString<Omit<FlattenMaps<Require_id<DocType>>, '__v'>>;
297
+ toJSON(options: ToObjectOptions & { versionKey: false, flattenObjectIds: true }): ObjectIdToString<Omit<Require_id<DocType>, '__v'>>;
298
+ toJSON(options: ToObjectOptions & { versionKey: false, flattenMaps: true }): Omit<FlattenMaps<Require_id<DocType>>, '__v'>;
299
+ toJSON(options: ToObjectOptions & { versionKey: false }): Omit<Require_id<DocType>, '__v'>;
300
+
301
+ // Handle virtuals: true
302
+ toJSON(options: ToObjectOptions & { virtuals: true }): Require_id<DocType & TVirtuals>;
303
+
304
+ // Default - no special options
305
+ toJSON(options?: ToObjectOptions): Default__v<Require_id<DocType>, TSchemaOptions>;
306
+
307
+ toJSON<T>(options?: ToObjectOptions): Default__v<Require_id<T>, ResolveSchemaOptions<TSchemaOptions>>;
308
+
309
+ /** Converts this document into a plain-old JavaScript object ([POJO](https://masteringjs.io/tutorials/fundamentals/pojo)). */
310
+ // flattenMaps: false (default) cases
311
+ toObject(options: ToObjectOptions & { flattenMaps: false, flattenObjectIds: true, virtuals: true, versionKey: false }): ObjectIdToString<Omit<Require_id<DocType & TVirtuals>, '__v'>>;
312
+ toObject(options: ToObjectOptions & { flattenMaps: false, flattenObjectIds: true, virtuals: true }): ObjectIdToString<Require_id<DocType & TVirtuals>>;
313
+ toObject(options: ToObjectOptions & { flattenMaps: false, flattenObjectIds: false, virtuals: true, versionKey: false }): Omit<Require_id<DocType & TVirtuals>, '__v'>;
314
+ toObject(options: ToObjectOptions & { flattenMaps: false, flattenObjectIds: false, virtuals: true }): Require_id<DocType & TVirtuals>;
315
+ toObject(options: ToObjectOptions & { flattenMaps: false, flattenObjectIds: true, virtuals: false, versionKey: false }): ObjectIdToString<Omit<Require_id<DocType>, '__v'>>;
316
+ toObject(options: ToObjectOptions & { flattenMaps: false, flattenObjectIds: true, virtuals: false }): ObjectIdToString<Require_id<DocType>>;
317
+ toObject(options: ToObjectOptions & { flattenMaps: false, flattenObjectIds: false, virtuals: false, versionKey: false }): Omit<Require_id<DocType>, '__v'>;
318
+ toObject(options: ToObjectOptions & { flattenMaps: false, flattenObjectIds: false, virtuals: false }): Require_id<DocType>;
319
+
320
+ // flattenMaps: true cases
321
+ toObject(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: true, virtuals: true, versionKey: false }): ObjectIdToString<Omit<FlattenMaps<Require_id<DocType & TVirtuals>>, '__v'>>;
322
+ toObject(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: true, virtuals: true }): ObjectIdToString<FlattenMaps<Require_id<DocType & TVirtuals>>>;
323
+ toObject(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: false, virtuals: true, versionKey: false }): Omit<FlattenMaps<Require_id<DocType & TVirtuals>>, '__v'>;
324
+ toObject(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: false, virtuals: true }): FlattenMaps<Require_id<DocType & TVirtuals>>;
325
+ toObject(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: true, virtuals: false, versionKey: false }): ObjectIdToString<Omit<FlattenMaps<Require_id<DocType>>, '__v'>>;
326
+ toObject(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: true, virtuals: false }): ObjectIdToString<FlattenMaps<Require_id<DocType>>>;
327
+ toObject(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: false, virtuals: false, versionKey: false }): Omit<FlattenMaps<Require_id<DocType>>, '__v'>;
328
+ toObject(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: false, virtuals: false }): FlattenMaps<Require_id<DocType>>;
329
+
330
+ // Handle "at least" short cross-combinations
331
+ toObject(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: true, virtuals: true }): ObjectIdToString<FlattenMaps<Require_id<DocType & TVirtuals>>>;
332
+ toObject(options: ToObjectOptions & { flattenMaps: true, flattenObjectIds: true }): ObjectIdToString<FlattenMaps<Require_id<DocType>>>;
333
+ toObject(options: ToObjectOptions & { flattenObjectIds: true, virtuals: true }): ObjectIdToString<Require_id<DocType & TVirtuals>>;
334
+ toObject(options: ToObjectOptions & { flattenObjectIds: true }): ObjectIdToString<Require_id<DocType>>;
335
+ toObject(options: ToObjectOptions & { flattenMaps: true, virtuals: true }): FlattenMaps<Require_id<DocType & TVirtuals>>;
336
+ toObject(options: ToObjectOptions & { flattenMaps: true }): FlattenMaps<Require_id<DocType>>;
337
+
338
+ // Handle versionKey: false (regardless of the others - most specific overloads should come first)
339
+ toObject(options: ToObjectOptions & { versionKey: false, flattenMaps: true, flattenObjectIds: true, virtuals: true }): ObjectIdToString<Omit<FlattenMaps<Require_id<DocType & TVirtuals>>, '__v'>>;
340
+ toObject(options: ToObjectOptions & { versionKey: false, flattenMaps: false, flattenObjectIds: true, virtuals: true }): ObjectIdToString<Omit<Require_id<DocType & TVirtuals>, '__v'>>;
341
+ toObject(options: ToObjectOptions & { versionKey: false, flattenMaps: true, virtuals: true }): Omit<FlattenMaps<Require_id<DocType & TVirtuals>>, '__v'>;
342
+ toObject(options: ToObjectOptions & { versionKey: false, flattenObjectIds: true, virtuals: true }): ObjectIdToString<Omit<Require_id<DocType & TVirtuals>, '__v'>>;
343
+ toObject(options: ToObjectOptions & { versionKey: false, virtuals: true }): Omit<Require_id<DocType & TVirtuals>, '__v'>;
344
+ toObject(options: ToObjectOptions & { versionKey: false, flattenMaps: true, flattenObjectIds: true }): ObjectIdToString<Omit<FlattenMaps<Require_id<DocType>>, '__v'>>;
345
+ toObject(options: ToObjectOptions & { versionKey: false, flattenObjectIds: true }): ObjectIdToString<Omit<Require_id<DocType>, '__v'>>;
346
+ toObject(options: ToObjectOptions & { versionKey: false, flattenMaps: true }): Omit<FlattenMaps<Require_id<DocType>>, '__v'>;
347
+ toObject(options: ToObjectOptions & { versionKey: false }): Omit<Require_id<DocType>, '__v'>;
348
+
349
+ // Handle virtuals: true
350
+ toObject(options: ToObjectOptions & { virtuals: true }): Require_id<DocType & TVirtuals>;
351
+
352
+ // Default - no special options
353
+ toObject(options?: ToObjectOptions): Default__v<Require_id<DocType>, TSchemaOptions>;
354
+
355
+ toObject<T>(options?: ToObjectOptions): Default__v<Require_id<T>, ResolveSchemaOptions<TSchemaOptions>>;
356
+
357
+ /** Clears the modified state on the specified path. */
358
+ unmarkModified<T extends keyof DocType>(path: T): void;
359
+ unmarkModified(path: string): void;
360
+
361
+ /** Sends an updateOne command with this document `_id` as the query selector. */
362
+ updateOne(update?: UpdateQuery<this> | UpdateWithAggregationPipeline, options?: QueryOptions | null): Query<any, this>;
363
+
364
+ /** Executes registered validation rules for this document. */
365
+ validate<T extends keyof DocType>(pathsToValidate?: T | T[], options?: AnyObject): Promise<void>;
366
+ validate(pathsToValidate?: pathsToValidate, options?: AnyObject): Promise<void>;
367
+ validate(options: { pathsToSkip?: pathsToSkip }): Promise<void>;
368
+
369
+ /** Executes registered validation rules (skipping asynchronous validators) for this document. */
370
+ validateSync(options: { pathsToSkip?: pathsToSkip, [k: string]: any }): Error.ValidationError | null;
371
+ validateSync<T extends keyof DocType>(pathsToValidate?: T | T[], options?: AnyObject): Error.ValidationError | null;
372
+ validateSync(pathsToValidate?: pathsToValidate, options?: AnyObject): Error.ValidationError | null;
373
+ }
374
+ }
@@ -0,0 +1,143 @@
1
+ declare class NativeError extends global.Error { }
2
+
3
+ declare module 'mongoose' {
4
+ import mongodb = require('mongodb');
5
+
6
+ type CastError = Error.CastError;
7
+ type SyncIndexesError = Error.SyncIndexesError;
8
+
9
+ export class MongooseError extends global.Error {
10
+ constructor(msg: string);
11
+
12
+ /** The type of error. "MongooseError" for generic errors. */
13
+ name: string;
14
+
15
+ static messages: any;
16
+
17
+ static Messages: any;
18
+ }
19
+
20
+ class Error extends MongooseError { }
21
+
22
+ namespace Error {
23
+
24
+ export class CastError extends MongooseError {
25
+ name: 'CastError';
26
+ stringValue: string;
27
+ kind: string;
28
+ value: any;
29
+ path: string;
30
+ reason?: NativeError | null;
31
+
32
+ constructor(type: string, value: any, path: string, reason?: NativeError, schemaType?: SchemaType);
33
+ }
34
+ export class SyncIndexesError extends MongooseError {
35
+ name: 'SyncIndexesError';
36
+ errors?: Record<string, mongodb.MongoServerError>;
37
+
38
+ constructor(type: string, value: any, path: string, reason?: NativeError, schemaType?: SchemaType);
39
+ }
40
+
41
+ export class DivergentArrayError extends MongooseError {
42
+ name: 'DivergentArrayError';
43
+ }
44
+
45
+ export class MissingSchemaError extends MongooseError {
46
+ name: 'MissingSchemaError';
47
+ }
48
+
49
+ export class DocumentNotFoundError extends MongooseError {
50
+ name: 'DocumentNotFoundError';
51
+ result: any;
52
+ numAffected: number;
53
+ filter: any;
54
+ query: any;
55
+ }
56
+
57
+ export class ObjectExpectedError extends MongooseError {
58
+ name: 'ObjectExpectedError';
59
+ path: string;
60
+ }
61
+
62
+ export class ObjectParameterError extends MongooseError {
63
+ name: 'ObjectParameterError';
64
+ }
65
+
66
+ export class OverwriteModelError extends MongooseError {
67
+ name: 'OverwriteModelError';
68
+ }
69
+
70
+ export class ParallelSaveError extends MongooseError {
71
+ name: 'ParallelSaveError';
72
+ }
73
+
74
+ export class ParallelValidateError extends MongooseError {
75
+ name: 'ParallelValidateError';
76
+ }
77
+
78
+ export class MongooseServerSelectionError extends MongooseError {
79
+ name: 'MongooseServerSelectionError';
80
+ }
81
+
82
+ export class StrictModeError extends MongooseError {
83
+ name: 'StrictModeError';
84
+ isImmutableError: boolean;
85
+ path: string;
86
+ }
87
+
88
+ export class ValidationError extends MongooseError {
89
+ name: 'ValidationError';
90
+
91
+ errors: { [path: string]: ValidatorError | CastError };
92
+ addError: (path: string, error: ValidatorError | CastError) => void;
93
+
94
+ /** Index of the document in insertMany() that failed validation (only set for unordered insertMany) */
95
+ index?: number;
96
+
97
+ constructor(instance?: MongooseError);
98
+ }
99
+
100
+ export class ValidatorError extends MongooseError {
101
+ name: 'ValidatorError';
102
+ properties: {
103
+ message: string,
104
+ type?: string,
105
+ path?: string,
106
+ value?: any,
107
+ reason?: any
108
+ };
109
+ kind: string;
110
+ path: string;
111
+ value: any;
112
+ reason?: MongooseError | null;
113
+
114
+ constructor(properties: {
115
+ message?: string,
116
+ type?: string,
117
+ path?: string,
118
+ value?: any,
119
+ reason?: any
120
+ });
121
+ }
122
+
123
+ export class VersionError extends MongooseError {
124
+ name: 'VersionError';
125
+ version: number;
126
+ modifiedPaths: Array<string>;
127
+
128
+ constructor(doc: Document, currentVersion: number, modifiedPaths: Array<string>);
129
+ }
130
+
131
+ export class StrictPopulateError extends MongooseError {
132
+ name: 'StrictPopulateError';
133
+ path: string;
134
+ }
135
+
136
+ export class MongooseBulkSaveIncompleteError extends MongooseError {
137
+ name: 'MongooseBulkSaveIncompleteError';
138
+ modelName: string;
139
+ bulkWriteResult: mongodb.BulkWriteResult;
140
+ numDocumentsNotUpdated: number;
141
+ }
142
+ }
143
+ }