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