-
-
Notifications
You must be signed in to change notification settings - Fork 281
/
schema.ts
53 lines (51 loc) · 1.49 KB
/
schema.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
import {Property} from "@tsed/common";
import {applyDecorators, getDecoratorType, StoreMerge} from "@tsed/core";
import {SchemaTypeOpts} from "mongoose";
import {MONGOOSE_SCHEMA} from "../constants";
import {MongooseSchemaOptions} from "../interfaces";
import {createSchema} from "../utils/createSchema";
/**
* Define a class as a Mongoose Schema ready to be used to compose other schemes and models.
*
* ### Example
*
* ```typescript
* @Schema()
* export class EventSchema {
* @Property()
* field: string;
* }
* ```
*
* ### Options
*
* - `schemaOptions` (mongoose.SchemaOptions): Option to configure the schema behavior.
*
* @param {MongooseSchemaOptions | undefined} options
* @returns {(target: any) => void}
* @decorator
* @mongoose
* @property
* @class
*/
export function Schema(options?: MongooseSchemaOptions): (target: any) => void;
/**
* Attach a schema on property class.
*
* @param {SchemaTypeOpts<any>} definition
* @returns {Function}
* @decorator
* @mongoose
*/
export function Schema(definition: SchemaTypeOpts<any>): Function;
export function Schema(options: MongooseSchemaOptions | SchemaTypeOpts<any> = {}) {
return (...parameters: any[]) => {
switch (getDecoratorType(parameters)) {
case "property":
return applyDecorators(Property(), StoreMerge(MONGOOSE_SCHEMA, options))(...parameters);
case "class":
StoreMerge(MONGOOSE_SCHEMA, createSchema(parameters[0], options as MongooseSchemaOptions))(...parameters);
break;
}
};
}