-
Notifications
You must be signed in to change notification settings - Fork 286
/
Copy pathcolumn.ts
66 lines (57 loc) · 1.86 KB
/
column.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
import { ModelAttributeColumnOptions, DataType } from 'sequelize';
import { addAttribute } from './attribute-service';
import { isDataType } from '../../sequelize/data-type/data-type-service';
import { getSequelizeTypeByDesignType } from '../shared/model-service';
export function Column(dataType: DataType): Function;
export function Column(options: Partial<ModelAttributeColumnOptions>): Function;
export function Column(
target: any,
propertyName: string,
propertyDescriptor?: PropertyDescriptor
): void;
export function Column(...args: any[]): Function | void {
// In case of no specified options, we infer the
// sequelize data type by the type of the property
if (args.length >= 2) {
const target = args[0];
const propertyName = args[1];
const propertyDescriptor = args[2];
annotate(target, propertyName, propertyDescriptor);
return;
}
return (target: any, propertyName: string, propertyDescriptor?: PropertyDescriptor) => {
annotate(
target,
propertyName,
propertyDescriptor ?? Object.getOwnPropertyDescriptor(target, propertyName),
args[0]
);
};
}
function annotate(
target: any,
propertyName: string,
propertyDescriptor?: PropertyDescriptor,
optionsOrDataType: Partial<ModelAttributeColumnOptions> | DataType = {}
): void {
let options: Partial<ModelAttributeColumnOptions>;
if (isDataType(optionsOrDataType)) {
options = {
type: optionsOrDataType,
};
} else {
options = { ...(optionsOrDataType as ModelAttributeColumnOptions) };
if (!options.type) {
options.type = getSequelizeTypeByDesignType(target, propertyName);
}
}
if (propertyDescriptor) {
if (propertyDescriptor.get) {
options.get = propertyDescriptor.get;
}
if (propertyDescriptor.set) {
options.set = propertyDescriptor.set;
}
}
addAttribute(target, propertyName, options);
}