UNPKG

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