-
Notifications
You must be signed in to change notification settings - Fork 8
/
decorators.ts
124 lines (112 loc) · 3.27 KB
/
decorators.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
import * as cormo from 'cormo';
export function Model(options: { connection?: cormo.Connection, name?: string, description?: string } = {}) {
const c = cormo.Model({ connection: options.connection, name: options.name });
return (ctor: typeof cormo.BaseModel) => {
c(ctor);
(ctor as any)._graphql = {
description: options.description,
};
};
}
interface IColumnBasicOptions {
required?: boolean;
graphql_required?: boolean;
unique?: boolean;
name?: string;
description?: string;
default_value?: string | number | (() => string | number);
}
export function Column(options: { enum: any } & IColumnBasicOptions
| { type: cormo.types.ColumnType | cormo.types.ColumnType[] } & IColumnBasicOptions): PropertyDecorator;
export function Column(options: {
enum?: any, type?: cormo.types.ColumnType
| cormo.types.ColumnType[],
} & IColumnBasicOptions): PropertyDecorator {
let cormo_type: cormo.types.ColumnType | cormo.types.ColumnType[];
if (options.enum) {
cormo_type = cormo.types.Integer;
} else {
cormo_type = options.type!;
}
const c = cormo.Column({
_graphql: {
description: options.description,
},
default_value: options.default_value,
name: options.name,
required: options.required,
type: cormo_type,
unique: options.unique,
});
return (target: object, propertyKey: string | symbol) => {
c(target, propertyKey);
};
}
interface IObjectColumnOptions {
type: any;
required?: boolean;
description?: string;
}
export function ObjectColumn(options: IObjectColumnOptions): PropertyDecorator {
const c = cormo.ObjectColumn(options.type);
return (target: object, propertyKey: string | symbol) => {
c(target, propertyKey);
};
}
interface IHasManyBasicOptions {
type?: string;
foreign_key?: string;
integrity?: 'ignore' | 'nullify' | 'restrict' | 'delete';
description?: string;
}
export function HasMany(options: IHasManyBasicOptions) {
const c_options = {
foreign_key: options.foreign_key,
integrity: options.integrity,
type: options.type,
};
const c = cormo.HasMany(c_options);
return (target: cormo.BaseModel, propertyKey: string) => {
c(target, propertyKey);
};
}
interface IHasOneBasicOptions {
type?: string;
foreign_key?: string;
integrity?: 'ignore' | 'nullify' | 'restrict' | 'delete';
description?: string;
}
export function HasOne(options: IHasOneBasicOptions) {
const c_options = {
foreign_key: options.foreign_key,
integrity: options.integrity,
type: options.type,
};
const c = cormo.HasOne(c_options);
return (target: cormo.BaseModel, propertyKey: string) => {
c(target, propertyKey);
};
}
interface IBelongsToBasicOptions {
type?: string;
required?: boolean;
foreign_key?: string;
description?: string;
}
export function BelongsTo(options: IBelongsToBasicOptions) {
const c_options = {
foreign_key: options.foreign_key,
required: options.required,
type: options.type,
};
const c = cormo.BelongsTo(c_options);
return (target: cormo.BaseModel, propertyKey: string) => {
c(target, propertyKey);
};
}
export function Index(columns: { [column: string]: 1 | -1 }, options?: { name?: string, unique?: boolean }) {
const c = cormo.Index(columns, options);
return (ctor: typeof cormo.BaseModel) => {
c(ctor);
};
}