UNPKG

9.97 kBTypeScriptView Raw
1import * as mongoose from 'mongoose';
2import type { AnyParamConstructor, DeferredFunc, Func, GetTypeReturn, IModelOptions, IObjectWithTypegooseFunction, IObjectWithTypegooseName, IPrototype, KeyStringAny, MappedInnerOuterOptions, PropOptionsForNumber, PropOptionsForString, VirtualOptions } from '../types';
3import { DecoratorKeys, PropType } 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 * Generate the inital values for the property to be extended upon
38 * @param name Name of the current Model/Class
39 * @param key Key of the property
40 * @param proptype Type of the Property
41 */
42export declare function initProperty(name: string, key: string, proptype: PropType): {
43 [path: string]: mongoose.SchemaDefinitionProperty<undefined>;
44};
45/**
46 * Get the Class for a given Document
47 * @param document The Document to fetch the class from
48 */
49export declare function getClassForDocument(document: mongoose.Document): NewableFunction | undefined;
50/**
51 * Get the Class for a given Schema
52 * @param input The Input to fetch the class from
53 */
54export declare function getClass(input: (mongoose.Document & IObjectWithTypegooseFunction) | (mongoose.Schema.Types.Subdocument & IObjectWithTypegooseFunction) | string | IObjectWithTypegooseName | any): NewableFunction | undefined;
55/**
56 * Returns all options found in "options" that are String-validate related
57 * @param options The raw Options that may contain the wanted options
58 */
59export declare function isWithStringValidate(options: PropOptionsForString): string[];
60/**
61 * Returns all options found in "options" that are String-transform related
62 * @param options The raw Options
63 */
64export declare function isWithStringTransform(options: PropOptionsForString): string[];
65/**
66 * Returns all options found in "options" that are Number-Validate related
67 * @param options The raw Options
68 */
69export declare function isWithNumberValidate(options: PropOptionsForNumber): string[];
70/**
71 * Returns all options found in "options" that are Enum Related
72 * @param options The raw Options
73 */
74export declare function isWithEnumValidate(options: PropOptionsForNumber | PropOptionsForString): string[];
75/**
76 * Check if the "options" contain any Virtual-Populate related options (excluding "ref" by it self)
77 * @param options The raw Options
78 */
79export declare function isWithVirtualPOP(options: Partial<VirtualOptions>): boolean;
80export declare const allVirtualoptions: string[];
81/**
82 * Check if all Required options for Virtual-Populate are included in "options"
83 * @param options The raw Options
84 */
85export declare function includesAllVirtualPOP(options: Partial<VirtualOptions>): options is VirtualOptions;
86/**
87 * Merge "value" with existing Metadata and save it to the class
88 * Difference with "mergeMetadata" is that this one DOES save it to the class
89 * Overwrites any existing Metadata that is new in "value"
90 * @param key Metadata key to read from and assign the new value to
91 * @param value Options to merge with
92 * @param cl The Class to read and assign the new metadata to
93 * @internal
94 */
95export declare function assignMetadata(key: DecoratorKeys, value: unknown, cl: AnyParamConstructor<any>): any;
96/**
97 * Merge "value" with existing Metadata
98 * Difference with "assignMetadata" is that this one DOES NOT save it to the class
99 * Overwrites any existing Metadata that is new in "value"
100 * @param key Metadata key to read existing metadata from
101 * @param value Option to merge with
102 * @param cl The Class to read the metadata from
103 * @returns Returns the merged output, where "value" overwrites existing Metadata values
104 * @internal
105 */
106export declare function mergeMetadata<T = any>(key: DecoratorKeys, value: unknown, cl: AnyParamConstructor<any>): T;
107/**
108 * Merge only schemaOptions from ModelOptions of the class
109 * @param value The value to use
110 * @param cl The Class to get the values from
111 */
112export declare function mergeSchemaOptions<U extends AnyParamConstructor<any>>(value: mongoose.SchemaOptions | undefined, cl: U): mongoose.SchemaOptions | undefined;
113/**
114 * Tries to return the right target
115 * if target.constructor.name is "Function", return "target", otherwise "target.constructor"
116 * @param target The target to determine
117 */
118export declare function getRightTarget(target: any): any;
119/**
120 * Get the Class's final name
121 * (combines all available options to generate a name)
122 * @param cl The Class to get the name for
123 * @param overwriteOptions Overwrite ModelOptions to generate a name from (Only name related options are merged)
124 */
125export declare function getName<U extends AnyParamConstructor<any>>(cl: U, overwriteOptions?: IModelOptions): string;
126/**
127 * Check if "Type" is a class and if it is already in "schemas"
128 * @param Type The Type to check
129 */
130export declare function isNotDefined(Type: any): boolean;
131/**
132 * Map Options to "inner" & "outer"
133 * -> inner: means inner of "type: [{here})"
134 * -> outer: means outer of "type: [{}], here"
135 *
136 * Specific to Arrays
137 * @param rawOptions The raw options
138 * @param Type The Type of the array
139 * @param target The Target class
140 * @param pkey Key of the Property
141 * @param loggerType Type to use for logging
142 * @param extraInner Extra Options to Mad explicitly to "inner"
143 */
144export declare function mapArrayOptions(rawOptions: any, Type: AnyParamConstructor<any> | mongoose.Schema, target: any, pkey: string, loggerType?: AnyParamConstructor<any>, extraInner?: KeyStringAny): mongoose.SchemaTypeOptions<any>;
145/**
146 * Map Options to "inner" & "outer"
147 * @param rawOptions The raw options
148 * @param Type The Type of the array
149 * @param target The Target class
150 * @param pkey Key of the Property
151 * @param loggerType Type to use for logging
152 */
153export declare function mapOptions(rawOptions: any, Type: AnyParamConstructor<any> | (mongoose.Schema & IPrototype), target: any, pkey: string, loggerType?: AnyParamConstructor<any>): MappedInnerOuterOptions;
154/**
155 * Check if the current Type is meant to be a Array
156 * @param rawOptions The raw options
157 */
158export declare function isTypeMeantToBeArray(rawOptions: any): boolean;
159/**
160 * Warn, Error or Allow if an mixed type is set
161 * -> this function exists for de-duplication
162 * @param target Target Class
163 * @param key Property key
164 */
165export declare function warnMixed(target: any, key: string): void | never;
166/**
167 * Check if "val" is "null" to "undefined"
168 * This Function exists because since node 4.0.0 the internal util.is* functions got deprecated
169 * @param val Any value to test if null or undefined
170 */
171export declare function isNullOrUndefined(val: unknown): val is null | undefined;
172/**
173 * Assign Global ModelOptions if not already existing
174 * @param target Target Class
175 */
176export declare function assignGlobalModelOptions(target: any): void;
177/**
178 * Loop over "dimensions" and create an array from that
179 * @param rawOptions baseProp's rawOptions
180 * @param extra What is actually in the deepest array
181 * @param name name of the target for better error logging
182 * @param key key of target-key for better error logging
183 */
184export declare function createArrayFromDimensions(rawOptions: any, extra: any, name: string, key: string): any[];
185/**
186 * Assert a condition, if "false" throw error
187 * Note: it is not named "assert" to differentiate between node and jest types
188 *
189 * Note: "error" can be a function to not execute the constructor when not needed
190 * @param cond The Condition to check
191 * @param error A Custom Error to throw or a function that returns a Error
192 */
193export declare function assertion(cond: any, error?: Error | DeferredFunc<Error>): asserts cond;
194/**
195 * Assert if "val" is an function (constructor for classes)
196 * @param val Value to test
197 */
198export declare function assertionIsClass(val: any): asserts val is Func;
199/**
200 * Get Type, if input is an arrow-function, execute it and return the result
201 * @param typeOrFunc Function or Type
202 * @param returnLastFoundArray Return the last found array (used for something like PropOptions.discriminators)
203 */
204export declare function getType(typeOrFunc: Func | any, returnLastFoundArray?: boolean): GetTypeReturn;
205/**
206 * Is the provided input an class with an constructor?
207 * @param obj The Value to test
208 */
209export declare function isConstructor(obj: any): obj is AnyParamConstructor<any>;
210/**
211 * Logs an warning if "included > 0" that the options of not the current type are included
212 * @param name Name of the Class
213 * @param key Name of the Currently Processed key
214 * @param type Name of the Expected Type
215 * @param extra Extra string to be included
216 * @param included Included Options to be listed
217 */
218export declare function warnNotCorrectTypeOptions(name: string, key: string, type: string, extra: string, included: string[]): void;
219/**
220 * Try to convert input "value" to a String, without it failing
221 * @param value The Value to convert to String
222 * @returns A String, either "value.toString" or a placeholder
223 */
224export declare function toStringNoFail(value: unknown): string;