-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathtypegoose.ts
184 lines (159 loc) · 5.06 KB
/
typegoose.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* imports */
import * as mongoose from 'mongoose';
import { shim } from 'object.fromentries';
import 'reflect-metadata';
if (!Object.fromEntries) {
shim();
}
import { constructors, hooks, methods, models, plugins, schemas, virtuals } from './data';
import { IModelOptions } from './optionsProp';
import { DocumentType, EmptyVoidFn, NoParamConstructor, Ref, ReturnModelType } from './types';
/* exports */
export * from './method';
export * from './prop';
export * from './hooks';
export * from './plugin';
export * from '.';
export * from './typeguards';
export * from './optionsProp';
export { DocumentType, Ref, ReturnModelType };
export { getClassForDocument } from './utils';
/**
* Main Class
*/
export abstract class Typegoose { } // this is kept for future use
/**
* Generates a Mongoose schema out of class props, iterating through all parents
* @param t The not initialized Class
* @returns Returns the Build Schema
*/
export function buildSchema<T, U extends NoParamConstructor<T>>(t: U) {
const name = t.name;
// get schema of current model
let sch = _buildSchema(t, name);
/** Parent Constructor */
let parentCtor = Object.getPrototypeOf(t.prototype).constructor;
// iterate trough all parents
while (parentCtor && parentCtor.name !== 'Typegoose' && parentCtor.name !== 'Object') {
// extend schema
sch = _buildSchema(t, parentCtor.name, sch);
// next parent
parentCtor = Object.getPrototypeOf(parentCtor.prototype).constructor;
}
return sch;
}
/**
* Get a Model for a Class
* Executes .setModelForClass if it cant find it already
* @param cl The uninitialized Class
* @returns The Model
* @public
* @example
* ```ts
* class Name {}
*
* const NameModel = getModelForClass(Name);
* ```
*/
export function getModelForClass<T, U extends NoParamConstructor<T>>(cl: U) {
const name = cl.name;
if (!models.get(name)) {
setModelForClass(cl);
}
return models.get(name) as ReturnModelType<U, T>;
}
/**
* Builds the Schema & The Model
* Note: you should use {@link getModelForClass}
* @param cl The uninitialized Class
* @returns The Model
* @public
* @example
* ```ts
* class Name {}
*
* const NameModel = setModelForClass(Name);
* ```
*/
export function setModelForClass<T, U extends NoParamConstructor<T>>(cl: U) {
const name = cl.name;
const options: IModelOptions = Reflect.getMetadata('typegoose:options', cl) || {};
const sch = buildSchema(cl);
let model = mongoose.model.bind(mongoose);
if (options.existingConnection) {
model = options.existingConnection.model.bind(options.existingConnection);
} else if (options.existingMongoose) {
model = options.existingMongoose.model.bind(options.existingMongoose);
}
models.set(name, model(name, sch));
constructors.set(name, cl);
return models.get(name) as ReturnModelType<U, T>;
}
/**
* Private schema builder out of class props
* -> If you discover this, dont use this function, use Typegoose.buildSchema!
* @param cl The not initialized Class
* @param name The Name to save the Schema Under (Mostly Constructor.name)
* @param sch Already Existing Schema?
* @returns Returns the Build Schema
* @private
*/
function _buildSchema<T, U extends NoParamConstructor<T>>(cl: U, name: string, sch?: mongoose.Schema) {
/** Simplify the usage */
const Schema = mongoose.Schema;
const { schemaOptions }: IModelOptions = Reflect.getMetadata('typegoose:options', cl) || {};
if (!sch) {
sch = new Schema(schemas.get(name), schemaOptions);
} else {
sch.add(schemas.get(name));
}
/** Simplify the usage */
const staticMethods = methods.staticMethods.get(name);
if (staticMethods) {
sch.statics = Object.assign(Object.fromEntries(staticMethods), sch.statics || {});
} else {
sch.statics = sch.statics || {};
}
/** Simplify the usage */
const instanceMethods = methods.instanceMethods.get(name);
if (instanceMethods) {
sch.methods = Object.assign(Object.fromEntries(instanceMethods), sch.methods || {});
} else {
sch.methods = sch.methods || {};
}
const hook = hooks.get(name);
if (hook) {
hook.pre.forEach((func, method) => {
sch.pre(method as string, func as EmptyVoidFn);
// ^ look at https://github.com/DefinitelyTyped/DefinitelyTyped/issues/37333
});
hook.post.forEach((v, k) => sch.post(k, v));
}
if (plugins.get(name)) {
for (const plugin of plugins.get(name)) {
sch.plugin(plugin.mongoosePlugin, plugin.options);
}
}
/** Simplify the usage */
const getterSetters = virtuals.get(name);
if (getterSetters) {
for (const [key, virtual] of getterSetters) {
if (virtual.options && virtual.options.overwrite) {
sch.virtual(key, virtual.options);
} else {
if (virtual.get) {
sch.virtual(key, virtual.options).get(virtual.get);
}
if (virtual.set) {
sch.virtual(key, virtual.options).set(virtual.set);
}
}
}
}
/** Get Metadata for indices */
const indices = Reflect.getMetadata('typegoose:indices', cl) || [];
for (const index of indices) {
sch.index(index.fields, index.options);
}
return sch;
}