1 | import type * as mongoose from 'mongoose';
|
2 | import type { Severity, PropType } from './internal/constants';
|
3 | /**
|
4 | * Get the Type of an instance of a Document with Class properties
|
5 | * @example
|
6 | * ```ts
|
7 | * class ClassName {
|
8 | * @prop()
|
9 | * public someProperty: string;
|
10 | * }
|
11 | * const NameModel = getModelForClass(ClassName);
|
12 | *
|
13 | * const doc: DocumentType<ClassName> = await NameModel.create({});
|
14 | * ```
|
15 | */
|
16 | export type DocumentType<T, QueryHelpers = BeAnObject> = mongoose.Document<unknown, QueryHelpers, T> & mongoose.Require_id<T> & IObjectWithTypegooseFunction;
|
17 | /**
|
18 | * Get the Type of an instance of a SubDocument with Class properties
|
19 | */
|
20 | export type SubDocumentType<T, QueryHelpers = BeAnObject> = DocumentType<T, QueryHelpers> & mongoose.Types.Subdocument;
|
21 | /**
|
22 | * Get the Type of an instance of a SubDocument that exists within an array, with Class properties
|
23 | */
|
24 | export type ArraySubDocumentType<T, QueryHelpers = BeAnObject> = DocumentType<T, QueryHelpers> & mongoose.Types.ArraySubdocument;
|
25 | /**
|
26 | * Used Internally for ModelTypes
|
27 | */
|
28 | export type ModelType<T, QueryHelpers = BeAnObject> = mongoose.Model<DocumentType<T, QueryHelpers>, QueryHelpers>;
|
29 | /**
|
30 | * Any-param Constructor
|
31 | */
|
32 | export type AnyParamConstructor<T> = new (...args: any) => T;
|
33 | /**
|
34 | * The Type for Models used in typegoose, mostly returned by "getModelForClass" and "addModelToTypegoose"
|
35 | * @example
|
36 | * const Model: ReturnModelType<typeof YourClass, YourClassQueryHelper> = mongoose.model("YourClass", YourClassSchema);
|
37 | */
|
38 | export type ReturnModelType<U extends AnyParamConstructor<any>, QueryHelpers = BeAnObject> = ModelType<InstanceType<U>, QueryHelpers> & U;
|
39 | /** Generic "Function" type, because typescript does not like using "Function" directly in strict mode */
|
40 | export type Func = (...args: any[]) => any;
|
41 | /**
|
42 | * The Type of a function to generate a custom model name.
|
43 | */
|
44 | export type CustomNameFunction = (options: IModelOptions) => string;
|
45 | /**
|
46 | * Defer a reference with a function (or as other projects call it "Forward declaration")
|
47 | * @param type This is just to comply with the common pattern of `type => ActualType`
|
48 | */
|
49 | export type DeferredFunc<T = any> = (...args: unknown[]) => T;
|
50 | /**
|
51 | * Dynamic Functions, since mongoose 4.13
|
52 | * @param doc The Document current document
|
53 | */
|
54 | export type DynamicStringFunc<T extends AnyParamConstructor<any>> = (doc: DocumentType<T>) => string;
|
55 | /** Type to keep the "discriminators" options consistent in types */
|
56 | export type NestedDiscriminatorsFunction = DeferredFunc<(AnyParamConstructor<any> | DiscriminatorObject)[]>;
|
57 | /**
|
58 | * This Interface for most properties uses "mongoose.SchemaTypeOptions<any>['']", but for some special (or typegoose custom) options, it is not used
|
59 | *
|
60 | * Example: `index` is directly from mongoose, where as `type` is from typegoose
|
61 | */
|
62 | export interface BasePropOptions {
|
63 | /**
|
64 | * include this value?
|
65 | * @default true (Implicitly)
|
66 | */
|
67 | select?: mongoose.SchemaTypeOptions<any>['select'];
|
68 | /**
|
69 | * is this value required?
|
70 | * @default false (Implicitly)
|
71 | */
|
72 | required?: mongoose.SchemaTypeOptions<any>['required'];
|
73 | /** Only accept Values from the Enum(|Array) */
|
74 | enum?: mongoose.SchemaTypeOptions<any>['enum'];
|
75 | /**
|
76 | * Add "null" to the enum array
|
77 | * Note: Custom Typegoose Option
|
78 | */
|
79 | addNullToEnum?: boolean;
|
80 | /**
|
81 | * Set Custom "warnMixed" Severity for a specific property
|
82 | * Overwrites Severity set in "modelOptions" for a specific property
|
83 | * Note: Custom Typegoose Option
|
84 | */
|
85 | allowMixed?: Severity;
|
86 | /** Give the Property a default Value */
|
87 | default?: mongoose.SchemaTypeOptions<any>['default'];
|
88 | /** Give a Validator RegExp or Function */
|
89 | validate?: mongoose.SchemaTypeOptions<any>['validate'];
|
90 | /**
|
91 | * Should this property have a "unique" index?
|
92 | * @link https://docs.mongodb.com/manual/indexes/#unique-indexes
|
93 | */
|
94 | unique?: mongoose.SchemaTypeOptions<any>['unique'];
|
95 | /**
|
96 | * Should this property have an index?
|
97 | * Note: don't use this if you want to do a compound index
|
98 | * @link https://docs.mongodb.com/manual/indexes
|
99 | */
|
100 | index?: mongoose.SchemaTypeOptions<any>['index'];
|
101 | /**
|
102 | * Should this property have a "sparse" index?
|
103 | * @link https://docs.mongodb.com/manual/indexes/#sparse-indexes
|
104 | */
|
105 | sparse?: mongoose.SchemaTypeOptions<any>['sparse'];
|
106 | /**
|
107 | * Should this property have an "expires" index?
|
108 | * @link https://docs.mongodb.com/manual/tutorial/expire-data
|
109 | */
|
110 | expires?: mongoose.SchemaTypeOptions<any>['expires'];
|
111 | /**
|
112 | * Should this property have a "text" index?
|
113 | * @link https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-text
|
114 | */
|
115 | text?: mongoose.SchemaTypeOptions<any>['text'];
|
116 | /** Should subdocuments get their own id?
|
117 | * @default true (Implicitly)
|
118 | */
|
119 | _id?: mongoose.SchemaTypeOptions<any>['_id'];
|
120 | /**
|
121 | * Set a Setter (Non-Virtual) to pre-process your value
|
122 | * (when using get/set both are required)
|
123 | * Please note that the option `type` is required, if get/set saves a different value than what is defined
|
124 | * @param value The Value that needs to get modified
|
125 | * @returns The Value, but modified OR anything
|
126 | * @example
|
127 | * ```ts
|
128 | * function setHello(val: string): string {
|
129 | * return val.toLowerCase()
|
130 | * }
|
131 | * function getHello(val: string): string {
|
132 | * return val.toUpperCase();
|
133 | * }
|
134 | * class Dummy {
|
135 | * @prop({ set: setHello, get: getHello }) // many options can be used, like required
|
136 | * public hello: string;
|
137 | * }
|
138 | * ```
|
139 | */
|
140 | set?: mongoose.SchemaTypeOptions<any>['set'];
|
141 | /**
|
142 | * Set a Getter (Non-Virtual) to Post-process your value
|
143 | * (when using get/set both are required)
|
144 | * Please note that the option `type` is required, if get/set saves a different value than what is defined
|
145 | * @param value The Value that needs to get modified
|
146 | * @returns The Value, but modified OR anything
|
147 | * @example
|
148 | * ```ts
|
149 | * function setHello(val: string): string {
|
150 | * return val.toLowerCase()
|
151 | * }
|
152 | * function getHello(val: string): string {
|
153 | * return val.toUpperCase();
|
154 | * }
|
155 | * class Dummy {
|
156 | * @prop({ set: setHello, get: getHello }) // many options can be used, like required
|
157 | * public hello: string;
|
158 | * }
|
159 | * ```
|
160 | */
|
161 | get?: mongoose.SchemaTypeOptions<any>['get'];
|
162 | /**
|
163 | * This may be needed if get/set is used
|
164 | * (this sets the type how it is saved to the DB)
|
165 | */
|
166 | type?: DeferredFunc<AnyParamConstructor<any>> | DeferredFunc<unknown> | unknown;
|
167 | /**
|
168 | * Make a property read-only
|
169 | * @example
|
170 | * ```ts
|
171 | * class SomeClass {
|
172 | * @prop({ immutable: true })
|
173 | * public someprop: Readonly<string>;
|
174 | * }
|
175 | * ```
|
176 | */
|
177 | immutable?: mongoose.SchemaTypeOptions<any>['immutable'];
|
178 | /**
|
179 | * Give the Property an alias in the output
|
180 | *
|
181 | * Note: you should include the alias as a variable in the class, but not with a prop decorator
|
182 | * @example
|
183 | * ```ts
|
184 | * class Dummy {
|
185 | * @prop({ alias: "helloWorld" })
|
186 | * public hello: string; // normal, with @prop
|
187 | * public helloWorld: string; // is just for type Completion, will not be included in the DB
|
188 | * }
|
189 | * ```
|
190 | */
|
191 | alias?: mongoose.SchemaTypeOptions<any>['alias'];
|
192 | /**
|
193 | * This option as only an effect when the plugin `mongoose-autopopulate` is used
|
194 | */
|
195 | autopopulate?: boolean | Function | KeyStringAny;
|
196 | /** Reference another Document (you should use Ref<T> as Prop type) */
|
197 | ref?: DeferredFunc<string | AnyParamConstructor<any> | DynamicStringFunc<any>> | string | AnyParamConstructor<any>;
|
198 | /** Take the Path and try to resolve it to a Model */
|
199 | refPath?: string;
|
200 | /**
|
201 | * Set the Nested Discriminators
|
202 | *
|
203 | * Note: "_id: false" as a prop option doesn't work here
|
204 | *
|
205 | * Note: Custom Typegoose Option
|
206 | */
|
207 | discriminators?: NestedDiscriminatorsFunction;
|
208 | /**
|
209 | * Use option {@link BasePropOptions.type}
|
210 | * @see https://typegoose.github.io/typegoose/docs/api/decorators/prop#map-options
|
211 | * @see https://typegoose.github.io/typegoose/docs/api/decorators/prop#proptype
|
212 | */
|
213 | of?: never;
|
214 | /**
|
215 | * If true, uses Mongoose's default `_id` settings. Only allowed for ObjectIds
|
216 | *
|
217 | * Note: Copied from mongoose's "index.d.ts"#SchemaTypeOptions
|
218 | */
|
219 | auto?: mongoose.SchemaTypeOptions<any>['auto'];
|
220 | /**
|
221 | * The default [subtype](http://bsonspec.org/spec.html) associated with this buffer when it is stored in MongoDB. Only allowed for buffer paths
|
222 | *
|
223 | * Note: Copied from mongoose's "index.d.ts"#SchemaTypeOptions
|
224 | */
|
225 | subtype?: mongoose.SchemaTypeOptions<any>['subtype'];
|
226 | /**
|
227 | * If `true`, Mongoose will skip gathering indexes on subpaths. Only allowed for subdocuments and subdocument arrays.
|
228 | *
|
229 | * Note: Copied from mongoose's "index.d.ts"#SchemaTypeOptions
|
230 | */
|
231 | excludeIndexes?: mongoose.SchemaTypeOptions<any>['excludeIndexes'];
|
232 | /**
|
233 | * Define a transform function for this individual schema type.
|
234 | * Only called when calling `toJSON()` or `toObject()`.
|
235 | *
|
236 | * Note: Copied from mongoose's "index.d.ts"#SchemaTypeOptions
|
237 | */
|
238 | transform?: mongoose.SchemaTypeOptions<any>['transform'];
|
239 | [extra: string]: any;
|
240 | }
|
241 | export interface InnerOuterOptions {
|
242 | /**
|
243 | * Use this to define inner-options
|
244 | * Use this if the auto-mapping is not correct or for plugin options
|
245 | *
|
246 | * Please open a new issue if some option is mismatched or not existing / mapped
|
247 | */
|
248 | innerOptions?: KeyStringAny;
|
249 | /**
|
250 | * Use this to define outer-options
|
251 | * Use this if the auto-mapping is not correct or for plugin options
|
252 | *
|
253 | * Please open a new issue if some option is mismatched or not existing / mapped
|
254 | */
|
255 | outerOptions?: KeyStringAny;
|
256 | }
|
257 | /**
|
258 | * Internal type for `utils.mapOptions`
|
259 | * @internal
|
260 | */
|
261 | export interface MappedInnerOuterOptions {
|
262 | /**
|
263 | * Mapped options for the inner of the Type
|
264 | */
|
265 | inner: NonNullable<KeyStringAny>;
|
266 | /**
|
267 | * Mapped options for the outer of the type
|
268 | */
|
269 | outer: NonNullable<KeyStringAny>;
|
270 | }
|
271 | /** Options for Array's */
|
272 | export interface ArrayPropOptions extends BasePropOptions, InnerOuterOptions {
|
273 | /**
|
274 | * How many dimensions this Array should have
|
275 | * (needs to be higher than 0)
|
276 | *
|
277 | * Note: Custom Typegoose Option
|
278 | * @default 1
|
279 | */
|
280 | dim?: number;
|
281 | /**
|
282 | * Set if Non-Array values will be cast to an array
|
283 | *
|
284 | * NOTE: This option currently only really affects "DocumentArray" and not normal arrays, https://github.com/Automattic/mongoose/issues/10398
|
285 | * @see https://mongoosejs.com/docs/api/schemaarray.html#schemaarray_SchemaArray.options
|
286 | * @example
|
287 | * ```ts
|
288 | * new Model({ array: "string" });
|
289 | * // will be cast to equal
|
290 | * new Model({ array: ["string"] });
|
291 | * ```
|
292 | * @default true
|
293 | */
|
294 | castNonArrays?: boolean;
|
295 | }
|
296 | /** Options For Map's */
|
297 | export interface MapPropOptions extends BasePropOptions, InnerOuterOptions {
|
298 | }
|
299 | export interface ValidateNumberOptions {
|
300 | /** Only allow numbers that are higher than this */
|
301 | min?: mongoose.SchemaTypeOptions<any>['min'];
|
302 | /** Only allow numbers lower than this */
|
303 | max?: mongoose.SchemaTypeOptions<any>['max'];
|
304 | /** Only allow Values from the enum */
|
305 | enum?: number[];
|
306 | }
|
307 | export interface ValidateStringOptions {
|
308 | /** Only allow values that match this RegExp */
|
309 | match?: mongoose.SchemaTypeOptions<any>['match'];
|
310 | /** Only allow Values from the enum */
|
311 | enum?: string[];
|
312 | /** Only allow values that have at least this length */
|
313 | minlength?: mongoose.SchemaTypeOptions<any>['minlength'];
|
314 | /** Only allow values that have at max this length */
|
315 | maxlength?: mongoose.SchemaTypeOptions<any>['maxlength'];
|
316 | }
|
317 | export interface TransformStringOptions {
|
318 | /** Should it be lowercased before save? */
|
319 | lowercase?: mongoose.SchemaTypeOptions<any>['lowercase'];
|
320 | /** Should it be uppercased before save? */
|
321 | uppercase?: mongoose.SchemaTypeOptions<any>['uppercase'];
|
322 | /** Should it be trimmed before save? */
|
323 | trim?: mongoose.SchemaTypeOptions<any>['trim'];
|
324 | }
|
325 | export interface VirtualOptions {
|
326 | /** Reference another Document (Ref<T> should be used as property type) */
|
327 | ref: NonNullable<BasePropOptions['ref']>;
|
328 | /** Which property(on the current-Class) to match `foreignField` against */
|
329 | localField: mongoose.VirtualTypeOptions['localField'];
|
330 | /** Which property(on the ref-Class) to match `localField` against */
|
331 | foreignField: mongoose.VirtualTypeOptions['foreignField'];
|
332 | /** Return as One Document(true) or as Array(false) */
|
333 | justOne?: mongoose.VirtualTypeOptions['justOne'];
|
334 | /** Return the number of Documents found instead of the actual Documents */
|
335 | count?: mongoose.VirtualTypeOptions['count'];
|
336 | /** Extra Query Options */
|
337 | options?: mongoose.VirtualTypeOptions['options'];
|
338 | /** Match Options */
|
339 | match?: mongoose.VirtualTypeOptions['match'];
|
340 | /**
|
341 | * If you set this to `true`, Mongoose will call any custom getters you defined on this virtual.
|
342 | *
|
343 | * Note: Copied from mongoose's "index.d.ts"#VirtualTypeOptions
|
344 | */
|
345 | getters?: mongoose.VirtualTypeOptions['getters'];
|
346 | /**
|
347 | * Add a default `limit` to the `populate()` query.
|
348 | *
|
349 | * Note: Copied from mongoose's "index.d.ts"#VirtualTypeOptions
|
350 | */
|
351 | limit?: mongoose.VirtualTypeOptions['limit'];
|
352 | /**
|
353 | * Add a default `skip` to the `populate()` query.
|
354 | *
|
355 | * Note: Copied from mongoose's "index.d.ts"#VirtualTypeOptions
|
356 | */
|
357 | skip?: mongoose.VirtualTypeOptions['skip'];
|
358 | /**
|
359 | * For legacy reasons, `limit` with `populate()` may give incorrect results because it only
|
360 | * executes a single query for every document being populated. If you set `perDocumentLimit`,
|
361 | * Mongoose will ensure correct `limit` per document by executing a separate query for each
|
362 | * document to `populate()`. For example, `.find().populate({ path: 'test', perDocumentLimit: 2 })`
|
363 | * will execute 2 additional queries if `.find()` returns 2 documents.
|
364 | *
|
365 | * Note: Copied from mongoose's "index.d.ts"#VirtualTypeOptions
|
366 | */
|
367 | perDocumentLimit?: mongoose.VirtualTypeOptions['perDocumentLimit'];
|
368 | [extra: string]: any;
|
369 | }
|
370 | export type PropOptionsForNumber = BasePropOptions & ValidateNumberOptions;
|
371 | export type PropOptionsForString = BasePropOptions & TransformStringOptions & ValidateStringOptions;
|
372 | export type RefType = mongoose.RefType;
|
373 | /**
|
374 | * Reference another Model
|
375 | */
|
376 | export type Ref<PopulatedType, RawId extends mongoose.RefType = PopulatedType extends {
|
377 | _id?: mongoose.RefType;
|
378 | } ? NonNullable<PopulatedType['_id']> : mongoose.Types.ObjectId> = mongoose.PopulatedDoc<DocumentType<PopulatedType>, RawId>;
|
379 | export interface DiscriminatorObject {
|
380 | /** The Class to use */
|
381 | type: AnyParamConstructor<any>;
|
382 | /**
|
383 | * The Name to differentiate between other classes
|
384 | * Mongoose JSDOC: [value] the string stored in the `discriminatorKey` property. If not specified, Mongoose uses the `name` parameter.
|
385 | * @default {string} The output of "getName"
|
386 | */
|
387 | value?: string;
|
388 | }
|
389 | export interface IModelOptions {
|
390 | /** An Existing Mongoose Connection */
|
391 | existingMongoose?: mongoose.Mongoose;
|
392 | /** Supports all Mongoose's Schema Options */
|
393 | schemaOptions?: mongoose.SchemaOptions;
|
394 | /** An Existing Connection */
|
395 | existingConnection?: mongoose.Connection;
|
396 | /** Typegoose Custom Options */
|
397 | options?: ICustomOptions;
|
398 | }
|
399 | /** Interface for just all naming options */
|
400 | export interface INamingOptions {
|
401 | /** Same as in {@link ICustomOptions} */
|
402 | customName?: ICustomOptions['customName'];
|
403 | /** Same as in {@link ICustomOptions} */
|
404 | automaticName?: ICustomOptions['automaticName'];
|
405 | /** Same as in {@link mongoose.SchemaOptions} */
|
406 | schemaCollection?: mongoose.SchemaOptions['collection'];
|
407 | }
|
408 | /** Typegoose options, mostly for "modelOptions({ options: ICustomOptions })" */
|
409 | export interface ICustomOptions {
|
410 | /**
|
411 | * Set the modelName of the class.
|
412 | * If it is a function, the function will be executed. The function will override
|
413 | * "automaticName". If "automaticName" is true and "customName" is a string, it
|
414 | * sets a *suffix* instead of the whole name.
|
415 | * @default schemaOptions.collection
|
416 | */
|
417 | customName?: string | CustomNameFunction;
|
418 | /**
|
419 | * Enable Automatic Name generation of a model
|
420 | * Example:
|
421 | * class with name of "SomeClass"
|
422 | * and option "collection" of "SC"
|
423 | *
|
424 | * will generate the name of "SomeClass_SC"
|
425 | * @default false
|
426 | */
|
427 | automaticName?: boolean;
|
428 | /** Allow "mongoose.Schema.Types.Mixed"? */
|
429 | allowMixed?: Severity;
|
430 | /**
|
431 | * Enable Overwriting of the plugins on the "to-be" discriminator schema with the base schema's
|
432 | * Note: this does not actually "merge plugins", it will overwrite the "to-be" discriminator's plugins with the base schema's
|
433 | * If {@link ICustomOptions.enableMergePlugins} and {@link ICustomOptions.enableMergeHooks} are both "false", then the global plugins will be automatically applied by typegoose, see https://github.com/Automattic/mongoose/issues/12696
|
434 | * @default false
|
435 | */
|
436 | enableMergePlugins?: boolean;
|
437 | /**
|
438 | * Enable Merging of Hooks
|
439 | * Note: only hooks that can be matched against each-other can be de-duplicated
|
440 | * If {@link ICustomOptions.enableMergePlugins} and {@link ICustomOptions.enableMergeHooks} are both "false", then the global plugins will be automatically applied by typegoose, see https://github.com/Automattic/mongoose/issues/12696
|
441 | * @default false
|
442 | */
|
443 | enableMergeHooks?: boolean;
|
444 | /**
|
445 | * Disable all lower indexes than this class (works like `sch.clone().clearIndexes()`)
|
446 | * @default false
|
447 | */
|
448 | disableLowerIndexes?: boolean;
|
449 | /**
|
450 | * Set the Nested Discriminators on the *base* of the Discriminators
|
451 | *
|
452 | * This option can be used over the prop-option to not have to re-define discriminators if used in multiple classes
|
453 | */
|
454 | discriminators?: NestedDiscriminatorsFunction;
|
455 | /**
|
456 | * Disable Caching for this Class if defined via `@modelOptions`, or disable caching for the `getModelForClass` / `buildSchema` / `getDiscriminatorModelForClass`
|
457 | * Does NOT overwrite global disabled caching
|
458 | * "undefined" and "false" have the same meaning
|
459 | * @default false
|
460 | */
|
461 | disableCaching?: boolean;
|
462 | }
|
463 | /** Extra options for "_buildSchema" in "schema.ts" */
|
464 | export interface IBuildSchemaOptions {
|
465 | /**
|
466 | * Add indexes from this class?
|
467 | * will be "false" when "ICustomOptions.disableLowerIndexes" is "true" for some upper class
|
468 | * @default true
|
469 | */
|
470 | buildIndexes?: boolean;
|
471 | }
|
472 | /** Type for the Values stored in the Reflection for Properties */
|
473 | export interface DecoratedPropertyMetadata {
|
474 | /** Prop Options */
|
475 | options: KeyStringAny;
|
476 | /** The Target Reflection target for getting metadata from keys */
|
477 | target: AnyParamConstructor<any>;
|
478 | /** Property name */
|
479 | key: string | symbol;
|
480 | /** What is it for a prop type? */
|
481 | propType?: PropType;
|
482 | }
|
483 | export type DecoratedPropertyMetadataMap = Map<string | symbol, DecoratedPropertyMetadata>;
|
484 | /**
|
485 | * Alias of "mongoose.IndexOptions" for convenience
|
486 | */
|
487 | export type IndexOptions = mongoose.IndexOptions;
|
488 | /**
|
489 | * Type for the Values stored in the Reflection for Indexes
|
490 | * @example
|
491 | * ```ts
|
492 | * const indices: IIndexArray[] = Reflect.getMetadata(DecoratorKeys.Index, target) || []);
|
493 | * ```
|
494 | */
|
495 | export interface IIndexArray {
|
496 | fields: KeyStringAny;
|
497 | options?: IndexOptions;
|
498 | }
|
499 | /**
|
500 | * Type for the Values stored in the Reflection for Plugins
|
501 | * @example
|
502 | * ```ts
|
503 | * const plugins: IPluginsArray[] = Array.from(Reflect.getMetadata(DecoratorKeys.Plugins, target) ?? []);
|
504 | * ```
|
505 | */
|
506 | export interface IPluginsArray {
|
507 | /** The Plugin Function to add */
|
508 | mongoosePlugin: Func;
|
509 | /** The Plugin's options, which could be anything because mongoose does not enforce it to be a object */
|
510 | options: any | undefined;
|
511 | }
|
512 | /**
|
513 | * Type for the Values stored in the Reflection for Virtual Populates
|
514 | * @example
|
515 | * ```ts
|
516 | * const virtuals: VirtualPopulateMap = new Map(Reflect.getMetadata(DecoratorKeys.VirtualPopulate, target.constructor) ?? []);
|
517 | * ```
|
518 | */
|
519 | export type VirtualPopulateMap = Map<string, VirtualOptions & Record<string, unknown>>;
|
520 | /**
|
521 | * Gets the signature (parameters with their types, and the return type) of a function type.
|
522 | *
|
523 | * @description Should be used when defining an interface for a class that uses query methods.
|
524 | *
|
525 | * @example
|
526 | * ```ts
|
527 | * function sendMessage(recipient: string, sender: string, priority: number, retryIfFails: boolean) {
|
528 | * // some logic...
|
529 | * return true;
|
530 | * }
|
531 | *
|
532 | * // Both of the following types will be identical.
|
533 | * type SendMessageType = AsQueryMethod<typeof sendMessage>;
|
534 | * type SendMessageManualType = (recipient: string, sender: string, priority: number, retryIfFails: boolean) => boolean;
|
535 | * ```
|
536 | */
|
537 | export type AsQueryMethod<T extends (...args: any) => any> = (...args: Parameters<T>) => ReturnType<T>;
|
538 | /**
|
539 | * Helper type to easily set the `this` type in a QueryHelper function
|
540 | *
|
541 | * @example
|
542 | * function findById(this: QueryHelperThis<YourClass, YourClassQueryHelpers>, id: string) {
|
543 | * return this.findOne({ _id: id });
|
544 | * }
|
545 | */
|
546 | export type QueryHelperThis<T extends AnyParamConstructor<any>, QueryHelpers, S = DocumentType<T, QueryHelpers>> = mongoose.QueryWithHelpers<S | null, S, QueryHelpers>;
|
547 | /**
|
548 | * Type for the Values stored in the Reflection for Query Methods
|
549 | * @example
|
550 | * ```ts
|
551 | * const queryMethods: QueryMethodMap = new Map(Reflect.getMetadata(DecoratorKeys.QueryMethod, target) ?? []);
|
552 | * ```
|
553 | */
|
554 | export type QueryMethodMap = Map<string, Func>;
|
555 | /**
|
556 | * Type for the Values stored in the Reflection for Nested Discriminators
|
557 | * @example
|
558 | * ```ts
|
559 | * const disMap: NestedDiscriminatorsMap = new Map(Reflect.getMetadata(DecoratorKeys.NestedDiscriminators, target) ?? []);
|
560 | * ```
|
561 | */
|
562 | export type NestedDiscriminatorsMap = Map<string, DiscriminatorObject[]>;
|
563 | /** A Helper type to combine both mongoose Hook Option types */
|
564 | export type HookOptionsEither = mongoose.SchemaPreOptions | mongoose.SchemaPostOptions;
|
565 | /**
|
566 | * Type for the Values stored in the Reflection for Hooks
|
567 | * @example
|
568 | * ```ts
|
569 | * const postHooks: IHooksArray[] = Array.from(Reflect.getMetadata(DecoratorKeys.HooksPost, target) ?? []);
|
570 | * ```
|
571 | */
|
572 | export interface IHooksArray {
|
573 | /** The Function to add as a hooks */
|
574 | func: Func;
|
575 | /** The Method to where this hook gets triggered */
|
576 | methods: (string | RegExp)[];
|
577 | /**
|
578 | * Options for Hooks
|
579 | * @see https://mongoosejs.com/docs/middleware.html#naming
|
580 | */
|
581 | options?: HookOptionsEither;
|
582 | }
|
583 | export interface IGlobalOptions {
|
584 | /** Typegoose Options */
|
585 | options?: ICustomOptions;
|
586 | /** Schema Options that should get applied to all models */
|
587 | schemaOptions?: mongoose.SchemaOptions;
|
588 | /**
|
589 | * Global Options for general Typegoose
|
590 | */
|
591 | globalOptions?: ITypegooseOptions;
|
592 | }
|
593 | export interface ITypegooseOptions {
|
594 | /**
|
595 | * Option to disable caching globally
|
596 | * completely disables the "constructors" and "models" maps
|
597 | * "false" and "undefined" have the same result of enabling caching
|
598 | * @default false
|
599 | */
|
600 | disableGlobalCaching?: boolean;
|
601 | }
|
602 | /** Interface describing a Object that has a "typegooseName" Function */
|
603 | export interface IObjectWithTypegooseFunction {
|
604 | typegooseName(): string;
|
605 | }
|
606 | /** For the types that error that seemingly don't have a prototype */
|
607 | export interface IPrototype {
|
608 | prototype?: any;
|
609 | }
|
610 | /** A Helper Interface for defining a "key" index of "string" and "value" of "any" */
|
611 | export type KeyStringAny = Record<string, any>;
|
612 | /**
|
613 | * The Return Type of "utils.getType"
|
614 | */
|
615 | export interface GetTypeReturn {
|
616 | type: unknown;
|
617 | dim: number;
|
618 | }
|
619 | /**
|
620 | * This type is for lint error "ban-types" where "{}" would be used
|
621 | * This type is separate from "{@link KeyStringAny}" because it has a different meaning
|
622 | */
|
623 | export type BeAnObject = Record<string, any>;
|
624 | /** Options used for "processProp" */
|
625 | export interface ProcessPropOptions extends DecoratedPropertyMetadata {
|
626 | /** The target Class's static version */
|
627 | cl: AnyParamConstructor<any>;
|
628 | }
|