UNPKG

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