UNPKG

20.4 kBTypeScriptView Raw
1declare module 'mongoose' {
2
3 /** The Mongoose Date [SchemaType](/docs/schematypes.html). */
4 type Date = Schema.Types.Date;
5
6 /**
7 * The Mongoose Decimal128 [SchemaType](/docs/schematypes.html). Used for
8 * declaring paths in your schema that should be
9 * [128-bit decimal floating points](http://thecodebarbarian.com/a-nodejs-perspective-on-mongodb-34-decimal.html).
10 * Do not use this to create a new Decimal128 instance, use `mongoose.Types.Decimal128`
11 * instead.
12 */
13 type Decimal128 = Schema.Types.Decimal128;
14
15 /**
16 * The Mongoose Mixed [SchemaType](/docs/schematypes.html). Used for
17 * declaring paths in your schema that Mongoose's change tracking, casting,
18 * and validation should ignore.
19 */
20 type Mixed = Schema.Types.Mixed;
21
22 /**
23 * The Mongoose Number [SchemaType](/docs/schematypes.html). Used for
24 * declaring paths in your schema that Mongoose should cast to numbers.
25 */
26 type Number = Schema.Types.Number;
27
28 /**
29 * The Mongoose ObjectId [SchemaType](/docs/schematypes.html). Used for
30 * declaring paths in your schema that should be
31 * [MongoDB ObjectIds](https://www.mongodb.com/docs/manual/reference/method/ObjectId/).
32 * Do not use this to create a new ObjectId instance, use `mongoose.Types.ObjectId`
33 * instead.
34 */
35 type ObjectId = Schema.Types.ObjectId;
36
37 /** The various Mongoose SchemaTypes. */
38 const SchemaTypes: typeof Schema.Types;
39
40 type DefaultType<T> = T extends Schema.Types.Mixed ? any : Partial<ExtractMongooseArray<T>>;
41
42 class SchemaTypeOptions<T, EnforcedDocType = any> {
43 type?:
44 T extends string ? StringSchemaDefinition :
45 T extends number ? NumberSchemaDefinition :
46 T extends boolean ? BooleanSchemaDefinition :
47 T extends NativeDate ? DateSchemaDefinition :
48 T extends Map<any, any> ? SchemaDefinition<typeof Map> :
49 T extends Buffer ? SchemaDefinition<typeof Buffer> :
50 T extends Types.ObjectId ? ObjectIdSchemaDefinition :
51 T extends Types.ObjectId[] ? AnyArray<ObjectIdSchemaDefinition> | AnyArray<SchemaTypeOptions<ObjectId, EnforcedDocType>> :
52 T extends object[] ? (AnyArray<Schema<any, any, any>> | AnyArray<SchemaDefinition<Unpacked<T>>> | AnyArray<SchemaTypeOptions<Unpacked<T>, EnforcedDocType>>) :
53 T extends string[] ? AnyArray<StringSchemaDefinition> | AnyArray<SchemaTypeOptions<string, EnforcedDocType>> :
54 T extends number[] ? AnyArray<NumberSchemaDefinition> | AnyArray<SchemaTypeOptions<number, EnforcedDocType>> :
55 T extends boolean[] ? AnyArray<BooleanSchemaDefinition> | AnyArray<SchemaTypeOptions<boolean, EnforcedDocType>> :
56 T extends Function[] ? AnyArray<Function | string> | AnyArray<SchemaTypeOptions<Unpacked<T>, EnforcedDocType>> :
57 T | typeof SchemaType | Schema<any, any, any> | SchemaDefinition<T> | Function | AnyArray<Function>;
58
59 /** Defines a virtual with the given name that gets/sets this path. */
60 alias?: string | string[];
61
62 /** Function or object describing how to validate this schematype. See [validation docs](https://mongoosejs.com/docs/validation.html). */
63 validate?: SchemaValidator<T> | AnyArray<SchemaValidator<T>>;
64
65 /** Allows overriding casting logic for this individual path. If a string, the given string overwrites Mongoose's default cast error message. */
66 cast?: string |
67 boolean |
68 ((value: any) => T) |
69 [(value: any) => T, string] |
70 [((value: any) => T) | null, (value: any, path: string, model: Model<any>, kind: string) => string];
71
72 /**
73 * If true, attach a required validator to this path, which ensures this path
74 * path cannot be set to a nullish value. If a function, Mongoose calls the
75 * function and only checks for nullish values if the function returns a truthy value.
76 */
77 required?: boolean | ((this: EnforcedDocType) => boolean) | [boolean, string] | [(this: EnforcedDocType) => boolean, string];
78
79 /**
80 * The default value for this path. If a function, Mongoose executes the function
81 * and uses the return value as the default.
82 */
83 default?: DefaultType<T> | ((this: EnforcedDocType, doc: any) => DefaultType<T>) | null;
84
85 /**
86 * The model that `populate()` should use if populating this path.
87 */
88 ref?: string | Model<any> | ((this: any, doc: any) => string | Model<any>);
89
90 /**
91 * The path in the document that `populate()` should use to find the model
92 * to use.
93 */
94
95 refPath?: string | ((this: any, doc: any) => string);
96
97 /**
98 * Whether to include or exclude this path by default when loading documents
99 * using `find()`, `findOne()`, etc.
100 */
101 select?: boolean | number;
102
103 /**
104 * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will
105 * build an index on this path when the model is compiled.
106 */
107 index?: boolean | IndexDirection | IndexOptions;
108
109 /**
110 * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose
111 * will build a unique index on this path when the
112 * model is compiled. [The `unique` option is **not** a validator](/docs/validation.html#the-unique-option-is-not-a-validator).
113 */
114 unique?: boolean | number;
115
116 /**
117 * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will
118 * disallow changes to this path once the document is saved to the database for the first time. Read more
119 * about [immutability in Mongoose here](http://thecodebarbarian.com/whats-new-in-mongoose-5-6-immutable-properties.html).
120 */
121 immutable?: boolean | ((this: any, doc: any) => boolean);
122
123 /**
124 * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will
125 * build a sparse index on this path.
126 */
127 sparse?: boolean | number;
128
129 /**
130 * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose
131 * will build a text index on this path.
132 */
133 text?: boolean | number | any;
134
135 /**
136 * Define a transform function for this individual schema type.
137 * Only called when calling `toJSON()` or `toObject()`.
138 */
139 transform?: (this: any, val: T) => any;
140
141 /** defines a custom getter for this property using [`Object.defineProperty()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty). */
142 get?: (value: any, doc?: this) => T | undefined;
143
144 /** defines a custom setter for this property using [`Object.defineProperty()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty). */
145 set?: (value: any, priorVal?: T, doc?: this) => any;
146
147 /** array of allowed values for this path. Allowed for strings, numbers, and arrays of strings */
148 enum?: Array<string | number | null> | ReadonlyArray<string | number | null> | { values: Array<string | number | null> | ReadonlyArray<string | number | null>, message?: string } | { [path: string]: string | number | null };
149
150 /** The default [subtype](http://bsonspec.org/spec.html) associated with this buffer when it is stored in MongoDB. Only allowed for buffer paths */
151 subtype?: number;
152
153 /** The minimum value allowed for this path. Only allowed for numbers and dates. */
154 min?: number | NativeDate | [number, string] | [NativeDate, string] | readonly [number, string] | readonly [NativeDate, string];
155
156 /** The maximum value allowed for this path. Only allowed for numbers and dates. */
157 max?: number | NativeDate | [number, string] | [NativeDate, string] | readonly [number, string] | readonly [NativeDate, string];
158
159 /** Defines a TTL index on this path. Only allowed for dates. */
160 expires?: string | number;
161
162 /** If `true`, Mongoose will skip gathering indexes on subpaths. Only allowed for subdocuments and subdocument arrays. */
163 excludeIndexes?: boolean;
164
165 /** If set, overrides the child schema's `_id` option. Only allowed for subdocuments and subdocument arrays. */
166 _id?: boolean;
167
168 /** If set, specifies the type of this map's values. Mongoose will cast this map's values to the given type. */
169 of?: Function | SchemaDefinitionProperty<any>;
170
171 /** If true, uses Mongoose's default `_id` settings. Only allowed for ObjectIds */
172 auto?: boolean;
173
174 /** Attaches a validator that succeeds if the data string matches the given regular expression, and fails otherwise. */
175 match?: RegExp | [RegExp, string] | readonly [RegExp, string];
176
177 /** If truthy, Mongoose will add a custom setter that lowercases this string using JavaScript's built-in `String#toLowerCase()`. */
178 lowercase?: boolean;
179
180 /** If truthy, Mongoose will add a custom setter that removes leading and trailing whitespace using JavaScript's built-in `String#trim()`. */
181 trim?: boolean;
182
183 /** If truthy, Mongoose will add a custom setter that uppercases this string using JavaScript's built-in `String#toUpperCase()`. */
184 uppercase?: boolean;
185
186 /** If set, Mongoose will add a custom validator that ensures the given string's `length` is at least the given number. */
187 minlength?: number | [number, string] | readonly [number, string];
188
189 /** If set, Mongoose will add a custom validator that ensures the given string's `length` is at most the given number. */
190 maxlength?: number | [number, string] | readonly [number, string];
191
192 [other: string]: any;
193 }
194
195 interface Validator {
196 message?: string; type?: string; validator?: Function
197 }
198
199 class SchemaType<T = any, DocType = any> {
200 /** SchemaType constructor */
201 constructor(path: string, options?: AnyObject, instance?: string);
202
203 /** Get/set the function used to cast arbitrary values to this type. */
204 static cast(caster?: Function | boolean): Function;
205
206 static checkRequired(checkRequired?: (v: any) => boolean): (v: any) => boolean;
207
208 /** Sets a default option for this schema type. */
209 static set(option: string, value: any): void;
210
211 /** Attaches a getter for all instances of this schema type. */
212 static get(getter: (value: any) => any): void;
213
214 /** The class that Mongoose uses internally to instantiate this SchemaType's `options` property. */
215 OptionsConstructor: SchemaTypeOptions<T>;
216
217 /** Cast `val` to this schema type. Each class that inherits from schema type should implement this function. */
218 cast(val: any, doc: Document<any>, init: boolean, prev?: any, options?: any): any;
219
220 /** Sets a default value for this SchemaType. */
221 default(val: any): any;
222
223 /** Adds a getter to this schematype. */
224 get(fn: Function): this;
225
226 /**
227 * Defines this path as immutable. Mongoose prevents you from changing
228 * immutable paths unless the parent document has [`isNew: true`](/docs/api/document.html#document_Document-isNew).
229 */
230 immutable(bool: boolean): this;
231
232 /** Declares the index options for this schematype. */
233 index(options: any): this;
234
235 /** String representation of what type this is, like 'ObjectID' or 'Number' */
236 instance: string;
237
238 /** True if this SchemaType has a required validator. False otherwise. */
239 isRequired?: boolean;
240
241 /** The options this SchemaType was instantiated with */
242 options: AnyObject;
243
244 /** The path to this SchemaType in a Schema. */
245 path: string;
246
247 /**
248 * Set the model that this path refers to. This is the option that [populate](https://mongoosejs.com/docs/populate.html)
249 * looks at to determine the foreign collection it should query.
250 */
251 ref(ref: string | boolean | Model<any>): this;
252
253 /**
254 * Adds a required validator to this SchemaType. The validator gets added
255 * to the front of this SchemaType's validators array using unshift().
256 */
257 required(required: boolean, message?: string): this;
258
259 /** The schema this SchemaType instance is part of */
260 schema: Schema<any>;
261
262 /** Sets default select() behavior for this path. */
263 select(val: boolean): this;
264
265 /** Adds a setter to this schematype. */
266 set(fn: Function): this;
267
268 /** Declares a sparse index. */
269 sparse(bool: boolean): this;
270
271 /** Declares a full text index. */
272 text(bool: boolean): this;
273
274 /** Defines a custom function for transforming this path when converting a document to JSON. */
275 transform(fn: (value: any) => any): this;
276
277 /** Declares an unique index. */
278 unique(bool: boolean): this;
279
280 /** The validators that Mongoose should run to validate properties at this SchemaType's path. */
281 validators: Validator[];
282
283 /** Adds validator(s) for this document path. */
284 validate(obj: RegExp | ((this: DocType, value: any, validatorProperties?: Validator) => any), errorMsg?: string, type?: string): this;
285
286 /** Default options for this SchemaType */
287 defaultOptions?: Record<string, any>;
288 }
289
290 namespace Schema {
291 namespace Types {
292 class Array extends SchemaType implements AcceptsDiscriminator {
293 /** This schema type's name, to defend against minifiers that mangle function names. */
294 static schemaName: 'Array';
295
296 static options: { castNonArrays: boolean; };
297
298 discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string): U;
299 discriminator<D>(name: string | number, schema: Schema, value?: string): Model<D>;
300
301 /** The schematype embedded in this array */
302 caster?: SchemaType;
303
304 /** Default options for this SchemaType */
305 defaultOptions: Record<string, any>;
306
307 /**
308 * Adds an enum validator if this is an array of strings or numbers. Equivalent to
309 * `SchemaString.prototype.enum()` or `SchemaNumber.prototype.enum()`
310 */
311 enum(vals: string[] | number[]): this;
312 }
313
314 class BigInt extends SchemaType {
315 /** This schema type's name, to defend against minifiers that mangle function names. */
316 static schemaName: 'BigInt';
317
318 /** Default options for this SchemaType */
319 defaultOptions: Record<string, any>;
320 }
321
322 class Boolean extends SchemaType {
323 /** This schema type's name, to defend against minifiers that mangle function names. */
324 static schemaName: 'Boolean';
325
326 /** Configure which values get casted to `true`. */
327 static convertToTrue: Set<any>;
328
329 /** Configure which values get casted to `false`. */
330 static convertToFalse: Set<any>;
331
332 /** Default options for this SchemaType */
333 defaultOptions: Record<string, any>;
334 }
335
336 class Buffer extends SchemaType {
337 /** This schema type's name, to defend against minifiers that mangle function names. */
338 static schemaName: 'Buffer';
339
340 /**
341 * Sets the default [subtype](https://studio3t.com/whats-new/best-practices-uuid-mongodb/)
342 * for this buffer. You can find a [list of allowed subtypes here](http://api.mongodb.com/python/current/api/bson/binary.html).
343 */
344 subtype(subtype: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 128): this;
345
346 /** Default options for this SchemaType */
347 defaultOptions: Record<string, any>;
348 }
349
350 class Date extends SchemaType {
351 /** This schema type's name, to defend against minifiers that mangle function names. */
352 static schemaName: 'Date';
353
354 /** Declares a TTL index (rounded to the nearest second) for _Date_ types only. */
355 expires(when: number | string): this;
356
357 /** Sets a maximum date validator. */
358 max(value: NativeDate, message: string): this;
359
360 /** Sets a minimum date validator. */
361 min(value: NativeDate, message: string): this;
362
363 /** Default options for this SchemaType */
364 defaultOptions: Record<string, any>;
365 }
366
367 class Decimal128 extends SchemaType {
368 /** This schema type's name, to defend against minifiers that mangle function names. */
369 static schemaName: 'Decimal128';
370
371 /** Default options for this SchemaType */
372 defaultOptions: Record<string, any>;
373 }
374
375 class DocumentArray extends SchemaType implements AcceptsDiscriminator {
376 /** This schema type's name, to defend against minifiers that mangle function names. */
377 static schemaName: 'DocumentArray';
378
379 static options: { castNonArrays: boolean; };
380
381 discriminator<D>(name: string | number, schema: Schema, value?: string): Model<D>;
382 discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string): U;
383
384 /** The schema used for documents in this array */
385 schema: Schema;
386
387 /** The constructor used for subdocuments in this array */
388 caster?: typeof Types.Subdocument;
389
390 /** Default options for this SchemaType */
391 defaultOptions: Record<string, any>;
392 }
393
394 class Map extends SchemaType {
395 /** This schema type's name, to defend against minifiers that mangle function names. */
396 static schemaName: 'Map';
397
398 /** Default options for this SchemaType */
399 defaultOptions: Record<string, any>;
400 }
401
402 class Mixed extends SchemaType {
403 /** This schema type's name, to defend against minifiers that mangle function names. */
404 static schemaName: 'Mixed';
405
406 /** Default options for this SchemaType */
407 defaultOptions: Record<string, any>;
408 }
409
410 class Number extends SchemaType {
411 /** This schema type's name, to defend against minifiers that mangle function names. */
412 static schemaName: 'Number';
413
414 /** Sets a enum validator */
415 enum(vals: number[]): this;
416
417 /** Sets a maximum number validator. */
418 max(value: number, message: string): this;
419
420 /** Sets a minimum number validator. */
421 min(value: number, message: string): this;
422
423 /** Default options for this SchemaType */
424 defaultOptions: Record<string, any>;
425 }
426
427 class ObjectId extends SchemaType {
428 /** This schema type's name, to defend against minifiers that mangle function names. */
429 static schemaName: 'ObjectId';
430
431 /** Adds an auto-generated ObjectId default if turnOn is true. */
432 auto(turnOn: boolean): this;
433
434 /** Default options for this SchemaType */
435 defaultOptions: Record<string, any>;
436 }
437
438 class Subdocument extends SchemaType implements AcceptsDiscriminator {
439 /** This schema type's name, to defend against minifiers that mangle function names. */
440 static schemaName: string;
441
442 /** The document's schema */
443 schema: Schema;
444
445 /** Default options for this SchemaType */
446 defaultOptions: Record<string, any>;
447
448 discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string): U;
449 discriminator<D>(name: string | number, schema: Schema, value?: string): Model<D>;
450 }
451
452 class String extends SchemaType {
453 /** This schema type's name, to defend against minifiers that mangle function names. */
454 static schemaName: 'String';
455
456 /** Adds an enum validator */
457 enum(vals: string[] | any): this;
458
459 /** Adds a lowercase [setter](http://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-set). */
460 lowercase(shouldApply?: boolean): this;
461
462 /** Sets a regexp validator. */
463 match(value: RegExp, message: string): this;
464
465 /** Sets a maximum length validator. */
466 maxlength(value: number, message: string): this;
467
468 /** Sets a minimum length validator. */
469 minlength(value: number, message: string): this;
470
471 /** Adds a trim [setter](http://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-set). */
472 trim(shouldTrim?: boolean): this;
473
474 /** Adds an uppercase [setter](http://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-set). */
475 uppercase(shouldApply?: boolean): this;
476
477 /** Default options for this SchemaType */
478 defaultOptions: Record<string, any>;
479 }
480
481 class UUID extends SchemaType {
482 /** This schema type's name, to defend against minifiers that mangle function names. */
483 static schemaName: 'UUID';
484
485 /** Default options for this SchemaType */
486 defaultOptions: Record<string, any>;
487 }
488 }
489 }
490}
491
\No newline at end of file