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 | declare class NativeDate extends global.Date { }
|
27 |
|
28 | declare module 'mongoose' {
|
29 | import events = require('events');
|
30 | import mongodb = require('mongodb');
|
31 | import mongoose = require('mongoose');
|
32 |
|
33 | export type Mongoose = typeof mongoose;
|
34 |
|
35 | |
36 |
|
37 |
|
38 |
|
39 | export const Mongoose: new (options?: MongooseOptions | null) => Mongoose;
|
40 |
|
41 | export let Promise: any;
|
42 | export const PromiseProvider: any;
|
43 |
|
44 | |
45 |
|
46 |
|
47 | export interface Models {
|
48 | [modelName: string]: Model<any>
|
49 | }
|
50 |
|
51 |
|
52 | export const models: Models;
|
53 |
|
54 | |
55 |
|
56 |
|
57 |
|
58 |
|
59 | export function deleteModel(name: string | RegExp): Mongoose;
|
60 |
|
61 | |
62 |
|
63 |
|
64 |
|
65 | export function sanitizeFilter<T>(filter: FilterQuery<T>): FilterQuery<T>;
|
66 |
|
67 |
|
68 | export function get<K extends keyof MongooseOptions>(key: K): MongooseOptions[K];
|
69 |
|
70 |
|
71 | export type CompileModelOptions = {
|
72 | overwriteModels?: boolean,
|
73 | connection?: Connection
|
74 | };
|
75 |
|
76 | export function model<TSchema extends Schema = any>(
|
77 | name: string,
|
78 | schema?: TSchema,
|
79 | collection?: string,
|
80 | options?: CompileModelOptions
|
81 | ): Model<InferSchemaType<TSchema>, ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>, ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>, {}, TSchema> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;
|
82 |
|
83 | export function model<T>(name: string, schema?: Schema<T, any, any> | Schema<T & Document, any, any>, collection?: string, options?: CompileModelOptions): Model<T>;
|
84 |
|
85 | export function model<T, U, TQueryHelpers = {}>(
|
86 | name: string,
|
87 | schema?: Schema<T, any, any, TQueryHelpers, any, any>,
|
88 | collection?: string,
|
89 | options?: CompileModelOptions
|
90 | ): U;
|
91 |
|
92 |
|
93 | export function modelNames(): Array<string>;
|
94 |
|
95 | |
96 |
|
97 |
|
98 |
|
99 | export function setDriver(driver: any): Mongoose;
|
100 |
|
101 |
|
102 | export const mongo: typeof mongodb;
|
103 |
|
104 |
|
105 | export function plugin(fn: (schema: Schema, opts?: any) => void, opts?: any): Mongoose;
|
106 |
|
107 |
|
108 | export function pluralize(fn?: ((str: string) => string) | null): ((str: string) => string) | null;
|
109 |
|
110 |
|
111 | export function set<K extends keyof MongooseOptions>(key: K, value: MongooseOptions[K]): Mongoose;
|
112 |
|
113 |
|
114 | export const version: string;
|
115 |
|
116 | export type AnyKeys<T> = { [P in keyof T]?: T[P] | any };
|
117 | export interface AnyObject {
|
118 | [k: string]: any
|
119 | }
|
120 |
|
121 | export type Require_id<T> = T extends { _id?: infer U }
|
122 | ? IfAny<U, T & { _id: Types.ObjectId }, T & Required<{ _id: U }>>
|
123 | : T & { _id: Types.ObjectId };
|
124 |
|
125 | export type HydratedDocument<DocType, TMethodsAndOverrides = {}, TVirtuals = {}> = DocType extends Document ? Require_id<DocType> : (Document<unknown, any, DocType> & Require_id<DocType> & TVirtuals & TMethodsAndOverrides);
|
126 |
|
127 | export type HydratedDocumentFromSchema<TSchema extends Schema> = HydratedDocument<
|
128 | InferSchemaType<TSchema>,
|
129 | ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>,
|
130 | ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>
|
131 | >;
|
132 |
|
133 | export interface TagSet {
|
134 | [k: string]: string;
|
135 | }
|
136 |
|
137 | export interface ToObjectOptions {
|
138 |
|
139 | getters?: boolean;
|
140 |
|
141 | virtuals?: boolean | string[];
|
142 |
|
143 | aliases?: boolean;
|
144 |
|
145 | minimize?: boolean;
|
146 |
|
147 | transform?: boolean | ((doc: any, ret: any, options: any) => any);
|
148 | /** if true, replace any conventionally populated paths with the original id in the output. Has no affect on virtual populated paths. */
|
149 | depopulate?: boolean;
|
150 | /** if false, exclude the version key (`__v` by default) from the output */
|
151 | versionKey?: boolean;
|
152 | /** if true, convert Maps to POJOs. Useful if you want to `JSON.stringify()` the result of `toObject()`. */
|
153 | flattenMaps?: boolean;
|
154 | /** If true, omits fields that are excluded in this document's projection. Unless you specified a projection, this will omit any field that has `select: false` in the schema. */
|
155 | useProjection?: boolean;
|
156 | }
|
157 |
|
158 | export type DiscriminatorModel<M, T> = T extends Model<infer T1, infer T2, infer T3, infer T4>
|
159 | ?
|
160 | M extends Model<infer M1, infer M2, infer M3, infer M4>
|
161 | ? Model<Omit<M1, keyof T1> & T1, M2 | T2, M3 | T3, M4 | T4>
|
162 | : M
|
163 | : M;
|
164 |
|
165 | export type DiscriminatorSchema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods, DisSchema> =
|
166 | DisSchema extends Schema<infer DisSchemaEDocType, infer DisSchemaM, infer DisSchemaInstanceMethods, infer DisSchemaQueryhelpers, infer DisSchemaVirtuals, infer DisSchemaStatics>
|
167 | ? Schema<Omit<DocType, keyof DisSchemaEDocType> & DisSchemaEDocType, DiscriminatorModel<DisSchemaM, M>, DisSchemaInstanceMethods | TInstanceMethods, DisSchemaQueryhelpers | TQueryHelpers, DisSchemaVirtuals | TVirtuals, DisSchemaStatics & TStaticMethods>
|
168 | : Schema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods>;
|
169 |
|
170 | type QueryResultType<T> = T extends Query<infer ResultType, any> ? ResultType : never;
|
171 |
|
172 | type PluginFunction<
|
173 | DocType,
|
174 | M,
|
175 | TInstanceMethods,
|
176 | TQueryHelpers,
|
177 | TVirtuals,
|
178 | TStaticMethods> = (schema: Schema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods>, opts?: any) => void;
|
179 |
|
180 | export class Schema<
|
181 | EnforcedDocType = any,
|
182 | M = Model<EnforcedDocType, any, any, any>,
|
183 | TInstanceMethods = {},
|
184 | TQueryHelpers = {},
|
185 | TVirtuals = {},
|
186 | TStaticMethods = {},
|
187 | TPathTypeKey extends TypeKeyBaseType = DefaultTypeKey,
|
188 | DocType extends ObtainDocumentType<DocType, EnforcedDocType, TPathTypeKey> = ObtainDocumentType<any, EnforcedDocType, TPathTypeKey>>
|
189 | extends events.EventEmitter {
|
190 | |
191 |
|
192 |
|
193 | constructor(definition?: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | DocType, options?: SchemaOptions<TPathTypeKey, FlatRecord<DocType>, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals>);
|
194 |
|
195 | /** Adds key path / schema type pairs to this schema. */
|
196 | add(obj: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | Schema, prefix?: string): this;
|
197 |
|
198 | /**
|
199 | * Array of child schemas (from document arrays and single nested subdocs)
|
200 | * and their corresponding compiled models. Each element of the array is
|
201 | * an object with 2 properties: `schema` and `model`.
|
202 | */
|
203 | childSchemas: { schema: Schema, model: any }[];
|
204 |
|
205 |
|
206 | clearIndexes(): this;
|
207 |
|
208 |
|
209 | clone<T = this>(): T;
|
210 |
|
211 | discriminator<DisSchema = Schema>(name: string, schema: DisSchema): DiscriminatorSchema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods, DisSchema>;
|
212 |
|
213 |
|
214 | pick<T = this>(paths: string[], options?: SchemaOptions): T;
|
215 |
|
216 |
|
217 | discriminators?: { [name: string]: Schema };
|
218 |
|
219 |
|
220 | eachPath(fn: (path: string, type: SchemaType) => void): this;
|
221 |
|
222 |
|
223 | index(fields: IndexDefinition, options?: IndexOptions): this;
|
224 |
|
225 | |
226 |
|
227 |
|
228 |
|
229 | indexes(): Array<IndexDefinition>;
|
230 |
|
231 |
|
232 | get<K extends keyof SchemaOptions>(key: K): SchemaOptions[K];
|
233 |
|
234 | |
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 | loadClass(model: Function, onlyVirtuals?: boolean): this;
|
242 |
|
243 |
|
244 | method<Context = any>(name: string, fn: (this: Context, ...args: any[]) => any, opts?: any): this;
|
245 | method(obj: Partial<TInstanceMethods>): this;
|
246 |
|
247 | /** Object of currently defined methods on this schema. */
|
248 | methods: { [F in keyof TInstanceMethods]: TInstanceMethods[F] } & AnyObject;
|
249 |
|
250 | /** The original object passed to the schema constructor */
|
251 | obj: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>>;
|
252 |
|
253 | /** Gets/sets schema paths. */
|
254 | path<pathGeneric extends keyof EnforcedDocType>(path: pathGeneric): SchemaType<EnforcedDocType[pathGeneric]>;
|
255 | path<ResultType extends SchemaType = SchemaType>(path: string): ResultType;
|
256 | path(path: string, constructor: any): this;
|
257 |
|
258 | /** Lists all paths and their type in the schema. */
|
259 | paths: {
|
260 | [key: string]: SchemaType;
|
261 | };
|
262 |
|
263 | /** Returns the pathType of `path` for this schema. */
|
264 | pathType(path: string): string;
|
265 |
|
266 | /** Registers a plugin for this schema. */
|
267 | plugin<PFunc extends PluginFunction<DocType, M, any, any, any, any>, POptions extends Parameters<PFunc>[1] = Parameters<PFunc>[1]>(fn: PFunc, opts?: POptions): this;
|
268 |
|
269 | /** Defines a post hook for the model. */
|
270 | post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PostMiddlewareFunction<T, T>): this;
|
271 | post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, T>): this;
|
272 | post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;
|
273 | post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, QueryResultType<T>>): this;
|
274 | post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: PostMiddlewareFunction<T, Array<AggregateExtract<T>>>): this;
|
275 | post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, Array<AggregateExtract<T>>>): this;
|
276 | post<T = M>(method: 'insertMany' | RegExp, fn: PostMiddlewareFunction<T, T>): this;
|
277 | post<T = M>(method: 'insertMany' | RegExp, options: SchemaPostOptions, fn: PostMiddlewareFunction<T, T>): this;
|
278 |
|
279 | post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
280 | post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
281 | post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
282 | post<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
283 | post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: ErrorHandlingMiddlewareFunction<T, Array<any>>): this;
|
284 | post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T, Array<any>>): this;
|
285 | post<T = M>(method: 'insertMany' | RegExp, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
286 | post<T = M>(method: 'insertMany' | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
287 |
|
288 | /** Defines a pre hook for the model. */
|
289 | pre<T = HydratedDocument<DocType, TInstanceMethods>>(method: 'save', fn: PreSaveMiddlewareFunction<T>): this;
|
290 | pre<T = HydratedDocument<DocType, TInstanceMethods>>(method: 'save', options: SchemaPreOptions, fn: PreSaveMiddlewareFunction<T>): this;
|
291 | pre<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PreMiddlewareFunction<T>): this;
|
292 | pre<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
|
293 | pre<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, fn: PreMiddlewareFunction<T>): this;
|
294 | pre<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
|
295 | pre<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: PreMiddlewareFunction<T>): this;
|
296 | pre<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
|
297 | pre<T = M>(method: 'insertMany' | RegExp, fn: (this: T, next: (err?: CallbackError) => void, docs: any | Array<any>) => void | Promise<void>): this;
|
298 | pre<T = M>(method: 'insertMany' | RegExp, options: SchemaPreOptions, fn: (this: T, next: (err?: CallbackError) => void, docs: any | Array<any>) => void | Promise<void>): this;
|
299 |
|
300 | /** Object of currently defined query helpers on this schema. */
|
301 | query: TQueryHelpers;
|
302 |
|
303 | /** Adds a method call to the queue. */
|
304 | queue(name: string, args: any[]): this;
|
305 |
|
306 | /** Removes the given `path` (or [`paths`]). */
|
307 | remove(paths: string | Array<string>): this;
|
308 |
|
309 | /** Removes index by name or index spec */
|
310 | remove(index: string | AnyObject): this;
|
311 |
|
312 | /** Returns an Array of path strings that are required by this schema. */
|
313 | requiredPaths(invalidate?: boolean): string[];
|
314 |
|
315 | /** Sets a schema option. */
|
316 | set<K extends keyof SchemaOptions>(key: K, value: SchemaOptions[K], _tags?: any): this;
|
317 |
|
318 | /** Adds static "class" methods to Models compiled from this schema. */
|
319 | static<K extends keyof TStaticMethods>(name: K, fn: TStaticMethods[K]): this;
|
320 | static(obj: { [F in keyof TStaticMethods]: TStaticMethods[F] } & { [name: string]: (this: M, ...args: any[]) => any }): this;
|
321 | static(name: string, fn: (this: M, ...args: any[]) => any): this;
|
322 |
|
323 | /** Object of currently defined statics on this schema. */
|
324 | statics: { [F in keyof TStaticMethods]: TStaticMethods[F] } & { [name: string]: (this: M, ...args: any[]) => any };
|
325 |
|
326 |
|
327 | virtual<T = HydratedDocument<DocType, TInstanceMethods>>(
|
328 | name: keyof TVirtuals | string,
|
329 | options?: VirtualTypeOptions<T, DocType>
|
330 | ): VirtualType<T>;
|
331 |
|
332 |
|
333 | virtuals: TVirtuals;
|
334 |
|
335 |
|
336 | virtualpath<T = HydratedDocument<DocType, TInstanceMethods>>(name: string): VirtualType<T> | null;
|
337 | }
|
338 |
|
339 | export type NumberSchemaDefinition = typeof Number | 'number' | 'Number' | typeof Schema.Types.Number;
|
340 | export type StringSchemaDefinition = typeof String | 'string' | 'String' | typeof Schema.Types.String;
|
341 | export type BooleanSchemaDefinition = typeof Boolean | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean;
|
342 | export type DateSchemaDefinition = typeof NativeDate | 'date' | 'Date' | typeof Schema.Types.Date;
|
343 | export type ObjectIdSchemaDefinition = 'ObjectId' | 'ObjectID' | typeof Schema.Types.ObjectId;
|
344 |
|
345 | export type SchemaDefinitionWithBuiltInClass<T> = T extends number
|
346 | ? NumberSchemaDefinition
|
347 | : T extends string
|
348 | ? StringSchemaDefinition
|
349 | : T extends boolean
|
350 | ? BooleanSchemaDefinition
|
351 | : T extends NativeDate
|
352 | ? DateSchemaDefinition
|
353 | : (Function | string);
|
354 |
|
355 | export type SchemaDefinitionProperty<T = undefined> = SchemaDefinitionWithBuiltInClass<T> |
|
356 | SchemaTypeOptions<T extends undefined ? any : T> |
|
357 | typeof SchemaType |
|
358 | Schema<any, any, any> |
|
359 | Schema<any, any, any>[] |
|
360 | SchemaTypeOptions<T extends undefined ? any : Unpacked<T>>[] |
|
361 | Function[] |
|
362 | SchemaDefinition<T> |
|
363 | SchemaDefinition<Unpacked<T>>[] |
|
364 | typeof Schema.Types.Mixed |
|
365 | MixedSchemaTypeOptions;
|
366 |
|
367 | export type SchemaDefinition<T = undefined> = T extends undefined
|
368 | ? { [path: string]: SchemaDefinitionProperty; }
|
369 | : { [path in keyof T]?: SchemaDefinitionProperty<T[path]>; };
|
370 |
|
371 | export type AnyArray<T> = T[] | ReadonlyArray<T>;
|
372 | export type ExtractMongooseArray<T> = T extends Types.Array<any> ? AnyArray<Unpacked<T>> : T;
|
373 |
|
374 | export interface MixedSchemaTypeOptions extends SchemaTypeOptions<Schema.Types.Mixed> {
|
375 | type: typeof Schema.Types.Mixed;
|
376 | }
|
377 |
|
378 | export type RefType =
|
379 | | number
|
380 | | string
|
381 | | Buffer
|
382 | | undefined
|
383 | | Types.ObjectId
|
384 | | Types.Buffer
|
385 | | typeof Schema.Types.Number
|
386 | | typeof Schema.Types.String
|
387 | | typeof Schema.Types.Buffer
|
388 | | typeof Schema.Types.ObjectId;
|
389 |
|
390 |
|
391 | export type InferId<T> = T extends { _id?: any } ? T['_id'] : Types.ObjectId;
|
392 |
|
393 | export interface VirtualTypeOptions<HydratedDocType = Document, DocType = unknown> {
|
394 |
|
395 | ref?: string | Function;
|
396 |
|
397 |
|
398 | localField?: string | ((this: HydratedDocType, doc: HydratedDocType) => string);
|
399 |
|
400 | /** The foreign field to populate on if this is a populated virtual. */
|
401 | foreignField?: string | ((this: HydratedDocType, doc: HydratedDocType) => string);
|
402 |
|
403 | /**
|
404 | * By default, a populated virtual is an array. If you set `justOne`,
|
405 | * the populated virtual will be a single doc or `null`.
|
406 | */
|
407 | justOne?: boolean;
|
408 |
|
409 | /** If you set this to `true`, Mongoose will call any custom getters you defined on this virtual. */
|
410 | getters?: boolean;
|
411 |
|
412 | /**
|
413 | * If you set this to `true`, `populate()` will set this virtual to the number of populated
|
414 | * documents, as opposed to the documents themselves, using `Query#countDocuments()`.
|
415 | */
|
416 | count?: boolean;
|
417 |
|
418 | /** Add an extra match condition to `populate()`. */
|
419 | match?: FilterQuery<any> | Function;
|
420 |
|
421 | /** Add a default `limit` to the `populate()` query. */
|
422 | limit?: number;
|
423 |
|
424 | /** Add a default `skip` to the `populate()` query. */
|
425 | skip?: number;
|
426 |
|
427 | /**
|
428 | * For legacy reasons, `limit` with `populate()` may give incorrect results because it only
|
429 | * executes a single query for every document being populated. If you set `perDocumentLimit`,
|
430 | * Mongoose will ensure correct `limit` per document by executing a separate query for each
|
431 | * document to `populate()`. For example, `.find().populate({ path: 'test', perDocumentLimit: 2 })`
|
432 | * will execute 2 additional queries if `.find()` returns 2 documents.
|
433 | */
|
434 | perDocumentLimit?: number;
|
435 |
|
436 | /** Additional options like `limit` and `lean`. */
|
437 | options?: QueryOptions<DocType> & { match?: AnyObject };
|
438 |
|
439 | /** Additional options for plugins */
|
440 | [extra: string]: any;
|
441 | }
|
442 |
|
443 | export class VirtualType<HydratedDocType> {
|
444 | /** Applies getters to `value`. */
|
445 | applyGetters(value: any, doc: Document): any;
|
446 |
|
447 | /** Applies setters to `value`. */
|
448 | applySetters(value: any, doc: Document): any;
|
449 |
|
450 | /** Adds a custom getter to this virtual. */
|
451 | get<T = HydratedDocType>(fn: (this: T, value: any, virtualType: VirtualType<T>, doc: T) => any): this;
|
452 |
|
453 | /** Adds a custom setter to this virtual. */
|
454 | set<T = HydratedDocType>(fn: (this: T, value: any, virtualType: VirtualType<T>, doc: T) => void): this;
|
455 | }
|
456 |
|
457 | export type ReturnsNewDoc = { new: true } | { returnOriginal: false } | { returnDocument: 'after' };
|
458 |
|
459 | export type ProjectionElementType = number | string;
|
460 | export type ProjectionType<T> = { [P in keyof T]?: ProjectionElementType } | AnyObject | string;
|
461 |
|
462 | export type SortValues = SortOrder;
|
463 |
|
464 | export type SortOrder = -1 | 1 | 'asc' | 'ascending' | 'desc' | 'descending';
|
465 |
|
466 | type _UpdateQuery<TSchema> = {
|
467 | /** @see https://docs.mongodb.com/manual/reference/operator/update-field/ */
|
468 | $currentDate?: AnyKeys<TSchema> & AnyObject;
|
469 | $inc?: AnyKeys<TSchema> & AnyObject;
|
470 | $min?: AnyKeys<TSchema> & AnyObject;
|
471 | $max?: AnyKeys<TSchema> & AnyObject;
|
472 | $mul?: AnyKeys<TSchema> & AnyObject;
|
473 | $rename?: Record<string, string>;
|
474 | $set?: AnyKeys<TSchema> & AnyObject;
|
475 | $setOnInsert?: AnyKeys<TSchema> & AnyObject;
|
476 | $unset?: AnyKeys<TSchema> & AnyObject;
|
477 |
|
478 | /** @see https://docs.mongodb.com/manual/reference/operator/update-array/ */
|
479 | $addToSet?: AnyKeys<TSchema> & AnyObject;
|
480 | $pop?: AnyKeys<TSchema> & AnyObject;
|
481 | $pull?: AnyKeys<TSchema> & AnyObject;
|
482 | $push?: AnyKeys<TSchema> & AnyObject;
|
483 | $pullAll?: AnyKeys<TSchema> & AnyObject;
|
484 |
|
485 | /** @see https://docs.mongodb.com/manual/reference/operator/update-bitwise/ */
|
486 | $bit?: Record<string, mongodb.NumericType>;
|
487 | };
|
488 |
|
489 | export type UpdateWithAggregationPipeline = UpdateAggregationStage[];
|
490 | export type UpdateAggregationStage = { $addFields: any } |
|
491 | { $set: any } |
|
492 | { $project: any } |
|
493 | { $unset: any } |
|
494 | { $replaceRoot: any } |
|
495 | { $replaceWith: any };
|
496 |
|
497 | /**
|
498 | * Update query command to perform on the document
|
499 | * @example
|
500 | * ```js
|
501 | * { age: 30 }
|
502 | * ```
|
503 | */
|
504 | export type UpdateQuery<T> = _UpdateQuery<T> & AnyObject;
|
505 |
|
506 | export type DocumentDefinition<T> = {
|
507 | [K in keyof Omit<T, Exclude<keyof Document, '_id' | 'id' | '__v'>>]:
|
508 | [Extract<T[K], mongodb.ObjectId>] extends [never]
|
509 | ? T[K] extends TreatAsPrimitives
|
510 | ? T[K]
|
511 | : LeanDocumentElement<T[K]>
|
512 | : T[K] | string;
|
513 | };
|
514 |
|
515 | export type FlattenMaps<T> = {
|
516 | [K in keyof T]: T[K] extends Map<any, any>
|
517 | ? AnyObject : T[K] extends TreatAsPrimitives
|
518 | ? T[K] : FlattenMaps<T[K]>;
|
519 | };
|
520 |
|
521 | export type actualPrimitives = string | boolean | number | bigint | symbol | null | undefined;
|
522 | export type TreatAsPrimitives = actualPrimitives | NativeDate | RegExp | symbol | Error | BigInt | Types.ObjectId;
|
523 |
|
524 | export type LeanType<T> =
|
525 | 0 extends (1 & T) ? T : // any
|
526 | T extends TreatAsPrimitives ? T : // primitives
|
527 | T extends Types.Subdocument ? Omit<LeanDocument<T>, '$isSingleNested' | 'ownerDocument' | 'parent'> :
|
528 | LeanDocument<T>; // Documents and everything else
|
529 |
|
530 | export type LeanArray<T extends unknown[]> = T extends unknown[][] ? LeanArray<T[number]>[] : LeanType<T[number]>[];
|
531 |
|
532 | export type _LeanDocument<T> = {
|
533 | [K in keyof T]: LeanDocumentElement<T[K]>;
|
534 | };
|
535 |
|
536 | // Keep this a separate type, to ensure that T is a naked type.
|
537 | // This way, the conditional type is distributive over union types.
|
538 | // This is required for PopulatedDoc.
|
539 | export type LeanDocumentElement<T> =
|
540 | T extends unknown[] ? LeanArray<T> : // Array
|
541 | T extends Document ? LeanDocument<T> : // Subdocument
|
542 | T;
|
543 |
|
544 | export type SchemaDefinitionType<T> = T extends Document ? Omit<T, Exclude<keyof Document, '_id' | 'id' | '__v'>> : T;
|
545 |
|
546 | // Helpers to simplify checks
|
547 | type IfAny<IFTYPE, THENTYPE, ELSETYPE = IFTYPE> = 0 extends (1 & IFTYPE) ? THENTYPE : ELSETYPE;
|
548 | type IfUnknown<IFTYPE, THENTYPE> = unknown extends IFTYPE ? THENTYPE : IFTYPE;
|
549 |
|
550 | // tests for these two types are located in test/types/lean.test.ts
|
551 | export type DocTypeFromUnion<T> = T extends (Document<infer T1, infer T2, infer T3> & infer U) ?
|
552 | [U] extends [Document<T1, T2, T3> & infer U] ? IfUnknown<IfAny<U, false>, false> : false : false;
|
553 |
|
554 | export type DocTypeFromGeneric<T> = T extends Document<infer IdType, infer TQueryHelpers, infer DocType> ?
|
555 | IfUnknown<IfAny<DocType, false>, false> : false;
|
556 |
|
557 | /**
|
558 | * Helper to choose the best option between two type helpers
|
559 | */
|
560 | export type _pickObject<T1, T2, Fallback> = T1 extends false ? T2 extends false ? Fallback : T2 : T1;
|
561 |
|
562 | /**
|
563 | * There may be a better way to do this, but the goal is to return the DocType if it can be infered
|
564 | * and if not to return a type which is easily identified as "not valid" so we fall back to
|
565 | * "strip out known things added by extending Document"
|
566 | * There are three basic ways to mix in Document -- "Document & T", "Document<ObjId, mixins, T>",
|
567 | * and "T extends Document". In the last case there is no type without Document mixins, so we can only
|
568 | * strip things out. In the other two cases we can infer the type, so we should
|
569 | */
|
570 | export type BaseDocumentType<T> = _pickObject<DocTypeFromUnion<T>, DocTypeFromGeneric<T>, false>;
|
571 |
|
572 | /**
|
573 | * Documents returned from queries with the lean option enabled.
|
574 | * Plain old JavaScript object documents (POJO).
|
575 | * @see https://mongoosejs.com/docs/tutorials/lean.html
|
576 | */
|
577 | export type LeanDocument<T> = Omit<_LeanDocument<T>, Exclude<keyof Document, '_id' | 'id' | '__v'> | '$isSingleNested'>;
|
578 |
|
579 | export type LeanDocumentOrArray<T> = 0 extends (1 & T) ? T :
|
580 | T extends unknown[] ? LeanDocument<T[number]>[] :
|
581 | T extends Document ? LeanDocument<T> :
|
582 | T;
|
583 |
|
584 | export type LeanDocumentOrArrayWithRawType<T, RawDocType> = 0 extends (1 & T) ? T :
|
585 | T extends unknown[] ? LeanDocument<RawDocType>[] :
|
586 | T extends Document ? LeanDocument<RawDocType> :
|
587 | T;
|
588 |
|
589 | /* for ts-mongoose */
|
590 | export class mquery { }
|
591 |
|
592 | export default mongoose;
|
593 | }
|
594 |
|
\ | No newline at end of file |