This repository has been archived by the owner on Jun 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
/
utils.ts
88 lines (78 loc) · 1.87 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/** @format */
import * as mongoose from 'mongoose';
import { constructors, schema } from './data';
/**
* Returns true, if it includes the Type
* @param Type The Type
* @returns true, if it includes it
*/
export function isPrimitive(Type: any): boolean {
return ['String', 'Number', 'Boolean', 'Date', 'Decimal128', 'ObjectID'].includes(Type.name);
}
/**
* Returns true, if it is an Object
* @param Type The Type
* @returns true, if it is an Object
*/
export function isObject(Type: any): boolean {
let prototype = Type.prototype;
let name = Type.name;
while (name) {
if (name === 'Object') {
return true;
}
prototype = Object.getPrototypeOf(prototype);
name = prototype ? prototype.constructor.name : null;
}
return false;
}
/**
* Returns true, if it is an Number
* @param Type The Type
* @returns true, if it is an Number
*/
export function isNumber(Type: any): boolean {
return Type.name === 'Number';
}
/**
* Returns true, if it is an String
* @param Type The Type
* @returns true, if it is an String
*/
export function isString(Type: any): boolean {
return Type.name === 'String';
}
/**
* Initialize as Object
* @param name <no info>
* @param key <no info>
*/
export function initAsObject(name, key): void {
if (!schema[name]) {
schema[name] = {};
}
if (!schema[name][key]) {
schema[name][key] = {};
}
}
/**
* Initialize as Array
* @param name <no info>
* @param key <no info>
*/
export function initAsArray(name: any, key: any): void {
if (!schema[name]) {
schema[name] = {};
}
if (!schema[name][key]) {
schema[name][key] = [{}];
}
}
/**
* Get the Class for a given Document
* @param document The Document
*/
export function getClassForDocument(document: mongoose.Document): any {
const modelName = (document.constructor as mongoose.Model<typeof document>).modelName;
return constructors[modelName];
}