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 | /**
|
14 | * Generic types for Document:
|
15 | * * T - the type of _id
|
16 | * * TQueryHelpers - Object with any helpers that should be mixed into the Query type
|
17 | * * DocType - the type of the actual Document created
|
18 | */
|
19 | class Document<T = any, TQueryHelpers = any, DocType = any> {
|
20 | constructor(doc?: any);
|
21 |
|
22 | /** This documents _id. */
|
23 | _id?: T;
|
24 |
|
25 | /** This documents __v. */
|
26 | __v?: any;
|
27 |
|
28 | /** Assert that a given path or paths is populated. Throws an error if not populated. */
|
29 | $assertPopulated<Paths = {}>(path: string | string[], values?: Partial<Paths>): Omit<this, keyof Paths> & Paths;
|
30 |
|
31 | /** Returns a deep clone of this document */
|
32 | $clone(): this;
|
33 |
|
34 | /* Get all subdocs (by bfs) */
|
35 | $getAllSubdocs(): Document[];
|
36 |
|
37 | /** Don't run validation on this path or persist changes to this path. */
|
38 | $ignore(path: string): void;
|
39 |
|
40 | /** Checks if a path is set to its default. */
|
41 | $isDefault(path: string): boolean;
|
42 |
|
43 | /** Getter/setter, determines whether the document was removed or not. */
|
44 | $isDeleted(val?: boolean): boolean;
|
45 |
|
46 | /** Returns an array of all populated documents associated with the query */
|
47 | $getPopulatedDocs(): Document[];
|
48 |
|
49 | /**
|
50 | * Increments the numeric value at `path` by the given `val`.
|
51 | * When you call `save()` on this document, Mongoose will send a
|
52 | * `$inc` as opposed to a `$set`.
|
53 | */
|
54 | $inc(path: string | string[], val?: number): this;
|
55 |
|
56 | /**
|
57 | * Returns true if the given path is nullish or only contains empty objects.
|
58 | * Useful for determining whether this subdoc will get stripped out by the
|
59 | * [minimize option](/docs/guide.html#minimize).
|
60 | */
|
61 | $isEmpty(path: string): boolean;
|
62 |
|
63 | /** Checks if a path is invalid */
|
64 | $isValid(path: string): boolean;
|
65 |
|
66 | /**
|
67 | * Empty object that you can use for storing properties on the document. This
|
68 | * is handy for passing data to middleware without conflicting with Mongoose
|
69 | * internals.
|
70 | */
|
71 | $locals: Record<string, unknown>;
|
72 |
|
73 | /** Marks a path as valid, removing existing validation errors. */
|
74 | $markValid(path: string): void;
|
75 |
|
76 | /** Returns the model with the given name on this document's associated connection. */
|
77 | $model<ModelType = Model<unknown>>(name: string): ModelType;
|
78 | $model<ModelType = Model<DocType>>(): ModelType;
|
79 |
|
80 | /**
|
81 | * A string containing the current operation that Mongoose is executing
|
82 | * on this document. Can be `null`, `'save'`, `'validate'`, or `'remove'`.
|
83 | */
|
84 | $op: 'save' | 'validate' | 'remove' | null;
|
85 |
|
86 | /**
|
87 | * Getter/setter around the session associated with this document. Used to
|
88 | * automatically set `session` if you `save()` a doc that you got from a
|
89 | * query with an associated session.
|
90 | */
|
91 | $session(session?: ClientSession | null): ClientSession | null;
|
92 |
|
93 | /** Alias for `set()`, used internally to avoid conflicts */
|
94 | $set(path: string | Record<string, any>, val: any, type: any, options?: DocumentSetOptions): this;
|
95 | $set(path: string | Record<string, any>, val: any, options?: DocumentSetOptions): this;
|
96 | $set(value: string | Record<string, any>): this;
|
97 |
|
98 | /** Set this property to add additional query filters when Mongoose saves this document and `isNew` is false. */
|
99 | $where: Record<string, unknown>;
|
100 |
|
101 | /** If this is a discriminator model, `baseModelName` is the name of the base model. */
|
102 | baseModelName?: string;
|
103 |
|
104 | /** Collection the model uses. */
|
105 | collection: Collection;
|
106 |
|
107 | /** Connection the model uses. */
|
108 | db: Connection;
|
109 |
|
110 | /** Removes this document from the db. */
|
111 | deleteOne(options?: QueryOptions): QueryWithHelpers<
|
112 | mongodb.DeleteResult,
|
113 | this,
|
114 | TQueryHelpers,
|
115 | DocType,
|
116 | 'deleteOne'
|
117 | >;
|
118 |
|
119 | /**
|
120 | * Takes a populated field and returns it to its unpopulated state. If called with
|
121 | * no arguments, then all populated fields are returned to their unpopulated state.
|
122 | */
|
123 | depopulate(path?: string | string[]): this;
|
124 |
|
125 | /**
|
126 | * Returns the list of paths that have been directly modified. A direct
|
127 | * modified path is a path that you explicitly set, whether via `doc.foo = 'bar'`,
|
128 | * `Object.assign(doc, { foo: 'bar' })`, or `doc.set('foo', 'bar')`.
|
129 | */
|
130 | directModifiedPaths(): Array<string>;
|
131 |
|
132 | /**
|
133 | * Returns true if this document is equal to another document.
|
134 | *
|
135 | * Documents are considered equal when they have matching `_id`s, unless neither
|
136 | * document has an `_id`, in which case this function falls back to using
|
137 | * `deepEqual()`.
|
138 | */
|
139 | equals(doc: Document<T>): boolean;
|
140 |
|
141 | /** Returns the current validation errors. */
|
142 | errors?: Error.ValidationError;
|
143 |
|
144 | /** Returns the value of a path. */
|
145 | get<T extends keyof DocType>(path: T, type?: any, options?: any): DocType[T];
|
146 | get(path: string, type?: any, options?: any): any;
|
147 |
|
148 | /**
|
149 | * Returns the changes that happened to the document
|
150 | * in the format that will be sent to MongoDB.
|
151 | */
|
152 | getChanges(): UpdateQuery<this>;
|
153 |
|
154 | /** The string version of this documents _id. */
|
155 | id?: any;
|
156 |
|
157 | /** Signal that we desire an increment of this documents version. */
|
158 | increment(): this;
|
159 |
|
160 | /**
|
161 | * Initializes the document without setters or marking anything modified.
|
162 | * Called internally after a document is returned from mongodb. Normally,
|
163 | * you do **not** need to call this function on your own.
|
164 | */
|
165 | init(obj: AnyObject, opts?: AnyObject): this;
|
166 |
|
167 | /** Marks a path as invalid, causing validation to fail. */
|
168 | invalidate<T extends keyof DocType>(path: T, errorMsg: string | NativeError, value?: any, kind?: string): NativeError | null;
|
169 | invalidate(path: string, errorMsg: string | NativeError, value?: any, kind?: string): NativeError | null;
|
170 |
|
171 | /** Returns true if `path` was directly set and modified, else false. */
|
172 | isDirectModified<T extends keyof DocType>(path: T | Array<T>): boolean;
|
173 | isDirectModified(path: string | Array<string>): boolean;
|
174 |
|
175 | /** Checks if `path` was explicitly selected. If no projection, always returns true. */
|
176 | isDirectSelected<T extends keyof DocType>(path: T): boolean;
|
177 | isDirectSelected(path: string): boolean;
|
178 |
|
179 | /** Checks if `path` is in the `init` state, that is, it was set by `Document#init()` and not modified since. */
|
180 | isInit<T extends keyof DocType>(path: T): boolean;
|
181 | isInit(path: string): boolean;
|
182 |
|
183 | /**
|
184 | * Returns true if any of the given paths are modified, else false. If no arguments, returns `true` if any path
|
185 | * in this document is modified.
|
186 | */
|
187 | isModified<T extends keyof DocType>(path?: T | Array<T>, options?: { ignoreAtomics?: boolean } | null): boolean;
|
188 | isModified(path?: string | Array<string>, options?: { ignoreAtomics?: boolean } | null): boolean;
|
189 |
|
190 | /** Boolean flag specifying if the document is new. */
|
191 | isNew: boolean;
|
192 |
|
193 | /** Checks if `path` was selected in the source query which initialized this document. */
|
194 | isSelected<T extends keyof DocType>(path: T): boolean;
|
195 | isSelected(path: string): boolean;
|
196 |
|
197 | /** Marks the path as having pending changes to write to the db. */
|
198 | markModified<T extends keyof DocType>(path: T, scope?: any): void;
|
199 | markModified(path: string, scope?: any): void;
|
200 |
|
201 | /** Returns the model with the given name on this document's associated connection. */
|
202 | model<ModelType = Model<unknown>>(name: string): ModelType;
|
203 | model<ModelType = Model<DocType>>(): ModelType;
|
204 |
|
205 | /** Returns the list of paths that have been modified. */
|
206 | modifiedPaths(options?: { includeChildren?: boolean }): Array<string>;
|
207 |
|
208 | /**
|
209 | * Overwrite all values in this document with the values of `obj`, except
|
210 | * for immutable properties. Behaves similarly to `set()`, except for it
|
211 | * unsets all properties that aren't in `obj`.
|
212 | */
|
213 | overwrite(obj: AnyObject): this;
|
214 |
|
215 | /**
|
216 | * If this document is a subdocument or populated document, returns the
|
217 | * document's parent. Returns undefined otherwise.
|
218 | */
|
219 | $parent(): Document | undefined;
|
220 |
|
221 | /** Populates document references. */
|
222 | populate<Paths = {}>(path: string | PopulateOptions | (string | PopulateOptions)[]): Promise<MergeType<this, Paths>>;
|
223 | populate<Paths = {}>(path: string, select?: string | AnyObject, model?: Model<any>, match?: AnyObject, options?: PopulateOptions): Promise<MergeType<this, Paths>>;
|
224 |
|
225 | /** Gets _id(s) used during population of the given `path`. If the path was not populated, returns `undefined`. */
|
226 | populated(path: string): any;
|
227 |
|
228 | /** Sends a replaceOne command with this document `_id` as the query selector. */
|
229 | replaceOne(replacement?: AnyObject, options?: QueryOptions | null): Query<any, this>;
|
230 |
|
231 | /** 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`. */
|
232 | save(options?: SaveOptions): Promise<this>;
|
233 |
|
234 | /** The document's schema. */
|
235 | schema: Schema;
|
236 |
|
237 | /** Sets the value of a path, or many paths. */
|
238 | set<T extends keyof DocType>(path: T, val: DocType[T], type: any, options?: DocumentSetOptions): this;
|
239 | set(path: string | Record<string, any>, val: any, type: any, options?: DocumentSetOptions): this;
|
240 | set(path: string | Record<string, any>, val: any, options?: DocumentSetOptions): this;
|
241 | set(value: string | Record<string, any>): this;
|
242 |
|
243 | /** The return value of this method is used in calls to JSON.stringify(doc). */
|
244 | toJSON<T = Require_id<DocType>>(options?: ToObjectOptions & { flattenMaps?: true }): FlattenMaps<T>;
|
245 | toJSON<T = Require_id<DocType>>(options: ToObjectOptions & { flattenMaps: false }): T;
|
246 |
|
247 | /** Converts this document into a plain-old JavaScript object ([POJO](https://masteringjs.io/tutorials/fundamentals/pojo)). */
|
248 | toObject<T = Require_id<DocType>>(options?: ToObjectOptions): Require_id<T>;
|
249 |
|
250 | /** Clears the modified state on the specified path. */
|
251 | unmarkModified<T extends keyof DocType>(path: T): void;
|
252 | unmarkModified(path: string): void;
|
253 |
|
254 | /** Sends an updateOne command with this document `_id` as the query selector. */
|
255 | updateOne(update?: UpdateQuery<this> | UpdateWithAggregationPipeline, options?: QueryOptions | null): Query<any, this>;
|
256 |
|
257 | /** Executes registered validation rules for this document. */
|
258 | validate<T extends keyof DocType>(pathsToValidate?: T | T[], options?: AnyObject): Promise<void>;
|
259 | validate(pathsToValidate?: pathsToValidate, options?: AnyObject): Promise<void>;
|
260 | validate(options: { pathsToSkip?: pathsToSkip }): Promise<void>;
|
261 |
|
262 | /** Executes registered validation rules (skipping asynchronous validators) for this document. */
|
263 | validateSync(options: { pathsToSkip?: pathsToSkip, [k: string]: any }): Error.ValidationError | null;
|
264 | validateSync<T extends keyof DocType>(pathsToValidate?: T | T[], options?: AnyObject): Error.ValidationError | null;
|
265 | validateSync(pathsToValidate?: pathsToValidate, options?: AnyObject): Error.ValidationError | null;
|
266 | }
|
267 | }
|