-
Notifications
You must be signed in to change notification settings - Fork 26
/
model.ts
184 lines (159 loc) · 4.57 KB
/
model.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
import { Reflect } from "./utils/reflect.ts";
import { metadata } from "./constants.ts";
import { getDataType } from "./utils/models.ts";
// --------------------------------------------------------------------------------
// MODELS
// --------------------------------------------------------------------------------
/**
* Define a class as a database model.
*
* @param tableName a custom table name for this model
*/
export function Model(tableName?: string) {
return (target: Function) => {
Reflect.defineMetadata(
metadata.tableName,
tableName || target.name.toLowerCase(),
target,
);
};
}
// --------------------------------------------------------------------------------
// COLUMNS
// --------------------------------------------------------------------------------
/**
* Transform database value to JavaScript types
*/
export enum DataType {
String = "string",
Date = "date",
Number = "number",
Boolean = "boolean",
}
/**
* Information about table the table column
*/
export interface ColumnOptions {
/** JavaScript type which will be converted from the database */
type: DataType;
/** The column name on the database */
name: string;
/** The default value */
default?: any;
}
export type ColumnDescription = ColumnOptions & {
propertyKey: string;
isPrimaryKey: boolean;
};
/**
* Model field
*
* @param options field options
*/
export function Column(options?: Partial<ColumnOptions>) {
return (target: Object, propertyKey: string) => {
let columns: ColumnDescription[] = [];
if (Reflect.hasMetadata(metadata.columns, target)) {
columns = Reflect.getMetadata(metadata.columns, target);
}
const typeMetadata = Reflect.getMetadata(
"design:type",
target,
propertyKey,
);
const type = getDataType(typeMetadata);
if (!type && !options?.type) {
throw new Error(`Column '${propertyKey}' must have a type!`);
}
const description: ColumnDescription = Object.assign({}, {
propertyKey: propertyKey,
name: propertyKey,
type: type,
isPrimaryKey: false,
}, options);
columns.push(description);
Reflect.defineMetadata(metadata.columns, columns, target);
};
}
/** Options for model's primary field */
export interface PrimaryFieldOptions {
name: string;
}
/**
* Model primary key field
*
* @param options primary field options
*/
export function Primary(options?: PrimaryFieldOptions) {
return (target: Object, propertyKey: string) => {
let columns: ColumnDescription[] = [];
if (Reflect.hasMetadata(metadata.columns, target)) {
columns = Reflect.getMetadata(metadata.columns, target);
}
columns.push({
propertyKey,
name: options?.name || "id",
type: DataType.Number,
isPrimaryKey: true,
});
Reflect.defineMetadata(metadata.columns, columns, target);
};
}
// --------------------------------------------------------------------------------
// RELATIONSHIPS
// --------------------------------------------------------------------------------
export enum RelationType {
HasMany = 1,
BelongsTo = 2,
}
export interface RelationDescription {
propertyKey: string;
type: RelationType;
getModel: () => { new (): any };
targetColumn: string;
}
/**
* Define a "belongs to" relational property.
*
* @param getModel a callback function that returns the model associated with this relation.
* @param column the column name that being targetted in this relation.
*/
export function BelongsTo(
getModel: () => { new (): any },
column: string,
) {
return (target: Object, propertyKey: string) => {
addRelation(target, RelationType.BelongsTo, propertyKey, getModel, column);
};
}
/**
* Define a "has many" relational property.
*
* @param getModel a callback function that returns the model associated with this relation.
* @param column the column name that being targetted in this relation.
*/
export function HasMany(
getModel: () => { new (): any },
column: string,
) {
return (target: Object, propertyKey: string) => {
addRelation(target, RelationType.HasMany, propertyKey, getModel, column);
};
}
/**
* Add relation metadata to a targetted model.
*/
function addRelation(
target: Object,
type: RelationType,
propertyKey: string,
getModel: () => any,
targetColumn: string,
) {
let relations: RelationDescription[] = [];
if (Reflect.hasMetadata(metadata.relations, target)) {
relations = Reflect.getMetadata(metadata.relations, target);
}
relations.push({ propertyKey, type, getModel, targetColumn });
Reflect.defineMetadata(metadata.relations, relations, target);
}