UNPKG

12.9 kBTypeScriptView Raw
1/// <reference types="mongoose/types/aggregate" />
2/// <reference types="mongoose/types/callback" />
3/// <reference types="mongoose/types/collection" />
4/// <reference types="mongoose/types/connection" />
5/// <reference types="mongoose/types/cursor" />
6/// <reference types="mongoose/types/document" />
7/// <reference types="mongoose/types/error" />
8/// <reference types="mongoose/types/expressions" />
9/// <reference types="mongoose/types/helpers" />
10/// <reference types="mongoose/types/middlewares" />
11/// <reference types="mongoose/types/indexes" />
12/// <reference types="mongoose/types/models" />
13/// <reference types="mongoose/types/mongooseoptions" />
14/// <reference types="mongoose/types/pipelinestage" />
15/// <reference types="mongoose/types/populate" />
16/// <reference types="mongoose/types/query" />
17/// <reference types="mongoose/types/schemaoptions" />
18/// <reference types="mongoose/types/schematypes" />
19/// <reference types="mongoose/types/session" />
20/// <reference types="mongoose/types/types" />
21/// <reference types="mongoose/types/utility" />
22/// <reference types="mongoose/types/validation" />
23/// <reference types="mongoose/types/virtuals" />
24/// <reference types="mongoose/types/inferschematype" />
25/// <reference types="mongoose/types/inferrawdoctype" />
26import * as mongoose from 'mongoose';
27import type { AnyParamConstructor, DeferredFunc, Func, GetTypeReturn, IModelOptions, INamingOptions, IObjectWithTypegooseFunction, IPrototype, KeyStringAny, MappedInnerOuterOptions, PropOptionsForNumber, PropOptionsForString, VirtualOptions } from '../types';
28import { DecoratorKeys } from './constants';
29/**
30 * Returns true, if the type is included in mongoose.Schema.Types
31 * @param Type The Type to test
32 * @returns true, if it includes it
33 */
34export declare function isPrimitive(Type: any): boolean;
35/**
36 * Returns true, if the type is included in mongoose.Schema.Types except the aliases
37 * @param Type The Type to test
38 * @returns true, if it includes it
39 */
40export declare function isAnRefType(Type: any): boolean;
41/**
42 * Returns true, if it is an Object
43 * Looks down the prototype chain, unless "once" is set to "true"
44 * @param Type The Type to test
45 * @param once Set to not loop down the prototype chain, default "false"
46 * @returns true, if it is an Object
47 */
48export declare function isObject(Type: any, once?: boolean): boolean;
49/**
50 * Returns true, if it is an Number
51 * @param Type The Type to test
52 * @returns true, if it is an Number
53 */
54export declare function isNumber(Type: any): Type is number;
55/**
56 * Returns true, if it is an String
57 * @param Type The Type to test
58 * @returns true, if it is an String
59 */
60export declare function isString(Type: any): Type is string;
61/**
62 * Get or init the Cached Schema
63 * @param target The Target to get / init the cached schema
64 * @returns The Schema to use
65 */
66export declare function getCachedSchema(target: AnyParamConstructor<any>): Record<string, mongoose.SchemaDefinition<unknown>>;
67/**
68 * Get the Class for a number of inputs
69 * @param input The Input to fetch the class from
70 */
71export declare function getClass(input: mongoose.Document | IObjectWithTypegooseFunction | {
72 typegooseName: string;
73} | string | any): NewableFunction | undefined;
74/**
75 * Returns all options found in "options" that are String-validate related
76 * @param options The raw Options that may contain the wanted options
77 */
78export declare function isWithStringValidate(options: PropOptionsForString): string[];
79/**
80 * Returns all options found in "options" that are String-transform related
81 * @param options The raw Options
82 */
83export declare function isWithStringTransform(options: PropOptionsForString): string[];
84/**
85 * Returns all options found in "options" that are Number-Validate related
86 * @param options The raw Options
87 */
88export declare function isWithNumberValidate(options: PropOptionsForNumber): string[];
89/**
90 * Returns all options found in "options" that are Enum Related
91 * @param options The raw Options
92 */
93export declare function isWithEnumValidate(options: PropOptionsForNumber | PropOptionsForString): string[];
94/**
95 * Check if the "options" contain any Virtual-Populate related options (excluding "ref" by it self)
96 * @param options The raw Options
97 */
98export declare function isWithVirtualPOP(options: Partial<VirtualOptions>): boolean;
99export declare const allVirtualoptions: string[];
100/**
101 * Check if all Required options for Virtual-Populate are included in "options"
102 * @param options The raw Options
103 */
104export declare function includesAllVirtualPOP(options: Partial<VirtualOptions>): options is VirtualOptions;
105/**
106 * Merge "value" with existing Metadata and save it to the class
107 * Difference with "mergeMetadata" is that this one DOES save it to the class
108 * Overwrites any existing Metadata that is new in "value"
109 * @param key Metadata key to read from and assign the new value to
110 * @param value Options to merge with
111 * @param cl The Class to read and assign the new metadata to
112 * @internal
113 */
114export declare function assignMetadata(key: DecoratorKeys, value: unknown, cl: AnyParamConstructor<any>): any;
115/**
116 * Merge "value" with existing Metadata
117 * Difference with "assignMetadata" is that this one DOES NOT save it to the class
118 * Overwrites any existing Metadata that is new in "value"
119 * @param key Metadata key to read existing metadata from
120 * @param value Option to merge with
121 * @param cl The Class to read the metadata from
122 * @param getOwn Use "getOwnMetadata" isntead of "getMetadata"
123 * @returns Returns the merged output, where "value" overwrites existing Metadata values
124 * @internal
125 */
126export declare function mergeMetadata<T = any>(key: DecoratorKeys, value: unknown, cl: AnyParamConstructor<any>, getOwn?: boolean): T;
127/**
128 * Merge only schemaOptions from ModelOptions of the class
129 * @param value The value to use
130 * @param cl The Class to get the values from
131 */
132export declare function mergeSchemaOptions<U extends AnyParamConstructor<any>>(value: mongoose.SchemaOptions | undefined, cl: U): mongoose.SchemaOptions<unknown, {}, {}, {}, {}, mongoose.Document<unknown, {}, unknown> & {
133 _id: mongoose.Types.ObjectId;
134} & {
135 __v: number;
136}> | undefined;
137/**
138 * Tries to return the right target
139 * if target.constructor.name is "Function", return "target", otherwise "target.constructor"
140 * @param target The target to determine
141 */
142export declare function getRightTarget(target: any): any;
143/**
144 * Get the Class's final name
145 * (combines all available options to generate a name)
146 * @param cl The Class to get the name for
147 * @param overwriteNaming Overwrite naming options used for generating the name
148 */
149export declare function getName<U extends AnyParamConstructor<any>>(cl: U, overwriteNaming?: INamingOptions): string;
150/**
151 * Check if "Type" is a class and if it is already in "schemas"
152 * @param Type The Type to check
153 */
154export declare function isNotDefined(Type: any): boolean;
155/**
156 * Map Options to "inner" & "outer"
157 * -> inner: means inner of "type: [{here})"
158 * -> outer: means outer of "type: [{}], here"
159 *
160 * Specific to Arrays
161 * @param rawOptions The raw options
162 * @param Type The Type of the array
163 * @param target The Target class
164 * @param pkey Key of the Property
165 * @param loggerType Type to use for logging
166 * @param extraInner Extra Options to Mad explicitly to "inner"
167 */
168export declare function mapArrayOptions(rawOptions: any, Type: AnyParamConstructor<any> | mongoose.Schema, target: any, pkey: string, loggerType?: AnyParamConstructor<any>, extraInner?: KeyStringAny): mongoose.SchemaTypeOptions<any>;
169/**
170 * Map Options to "inner" & "outer"
171 * @param rawOptions The raw options
172 * @param Type The Type of the array
173 * @param target The Target class
174 * @param pkey Key of the Property
175 * @param loggerType Type to use for logging
176 */
177export declare function mapOptions(rawOptions: any, Type: AnyParamConstructor<any> | (mongoose.Schema & IPrototype), target: any, pkey: string, loggerType?: AnyParamConstructor<any>): MappedInnerOuterOptions;
178/**
179 * Check if the current Type is meant to be a Array
180 * @param rawOptions The raw options
181 */
182export declare function isTypeMeantToBeArray(rawOptions: any): boolean;
183/**
184 * Warn, Error or Allow if an mixed type is set
185 * -> this function exists for de-duplication
186 * @param target Target Class
187 * @param key Property key
188 */
189export declare function warnMixed(target: any, key: string): void | never;
190/**
191 * Check if "val" is "null" to "undefined"
192 * This Function exists because since node 4.0.0 the internal util.is* functions got deprecated
193 * @param val Any value to test if null or undefined
194 */
195export declare function isNullOrUndefined(val: unknown): val is null | undefined;
196/**
197 * Assign Global ModelOptions if not already existing
198 * @param target Target Class
199 * @returns "true" when it assigned options
200 */
201export declare function assignGlobalModelOptions(target: any): boolean;
202/**
203 * Consistently get the "ModelOptions", merged with (the following is the order in which options are applied):
204 * 1. globalOptions if unset
205 * 2. decorator ModelOptions
206 * 3. input "rawOptions"
207 *
208 * Note: applies global options to the decorator options if unset, but does not set the final options
209 * @param rawOptions Options to merge(-overwrite) all previous options
210 * @param cl The Class to get / set the ModelOptions on
211 * @param getOwn use "getOwnMetadata" instead of "getMetadata"
212 * @returns A ModelOptions object
213 */
214export declare function getMergedModelOptions(rawOptions: IModelOptions | undefined, cl: AnyParamConstructor<any>, getOwn?: boolean): IModelOptions;
215/**
216 * Loop over "dimensions" and create an array from that
217 * @param rawOptions baseProp's rawOptions
218 * @param extra What is actually in the deepest array
219 * @param name name of the target for better error logging
220 * @param key key of target-key for better error logging
221 */
222export declare function createArrayFromDimensions(rawOptions: any, extra: any, name: string, key: string): any[];
223/**
224 * Assert a condition, if "false" throw error
225 * Note: it is not named "assert" to differentiate between node and jest types
226 *
227 * Note: "error" can be a function to not execute the constructor when not needed
228 * @param cond The Condition to check
229 * @param error A Custom Error to throw or a function that returns a Error
230 */
231export declare function assertion(cond: any, error?: Error | DeferredFunc<Error>): asserts cond;
232/**
233 * Assert if "val" is an function (constructor for classes)
234 * @param val Value to test
235 */
236export declare function assertionIsClass(val: any): asserts val is Func;
237/**
238 * Get Type, if input is an arrow-function, execute it and return the result
239 * @param typeOrFunc Function or Type
240 * @param returnLastFoundArray Return the last found array (used for something like PropOptions.discriminators)
241 */
242export declare function getType(typeOrFunc: Func | any, returnLastFoundArray?: boolean): GetTypeReturn;
243/**
244 * Is the provided input an class with an constructor?
245 * @param obj The Value to test
246 */
247export declare function isConstructor(obj: any): obj is AnyParamConstructor<any>;
248/**
249 * Logs an warning if "included > 0" that the options of not the current type are included
250 * @param name Name of the Class
251 * @param key Name of the Currently Processed key
252 * @param type Name of the Expected Type
253 * @param extra Extra string to be included
254 * @param included Included Options to be listed
255 */
256export declare function warnNotCorrectTypeOptions(name: string, key: string, type: string, extra: string, included: string[]): void;
257/**
258 * Logs a warning for Discriminator setting a different "existing*" property than the base
259 * @param fromName Name of the Base Model
260 * @param clName Name of the Discriminator's class
261 * @param property The property defined that does not match
262 */
263export declare function warnNotMatchingExisting(fromName: string, clName: string, property: string): void;
264/**
265 * Try to convert input "value" to a String, without it failing
266 * @param value The Value to convert to String
267 * @returns A String, either "value.toString" or a placeholder
268 */
269export declare function toStringNoFail(value: unknown): string;
270/**
271 * Map options from {@link IModelOptions} to {@link INamingOptions}
272 * @param options The options to map
273 * @returns Always a object, contains mapped options from {@link IModelOptions}
274 */
275export declare function mapModelOptionsToNaming(options: IModelOptions | undefined): INamingOptions;
276/**
277 * Helper function to check if caching is enabled globally
278 * @returns "true" if caching is enabled or "false" if disabled
279 */
280export declare function isGlobalCachingEnabled(): boolean;
281/**
282 * Helper function to check if caching is enabled globally AND by options
283 * @param opt The caching option (from IModelOptions)
284 * @returns "true" if caching is enabled or "false" if disabled
285 */
286export declare function isCachingEnabled(opt: boolean | undefined): boolean;