-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
table.ts
65 lines (53 loc) · 1.97 KB
/
table.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
import type { Model, ModelOptions, ModelStatic } from '../../model.js';
import type { RegisteredModelOptions } from '../shared/model.js';
import { registerModelOptions } from '../shared/model.js';
/**
* The `@Table` decorator is used to configure a model. It is used on a model class, and takes an object as parameter.<br />
* Using this decorator is completely optional, you only need to use it if you want to configure one of the options of your model.
*
* @example
* ```ts
* @Table({
* tableName: 'users',
* timestamps: false,
* })
* class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {}
* ```
*
* @param options
*/
export function Table<M extends Model = Model>(options: ModelOptions<M>): ClassDecorator;
export function Table(target: ModelStatic): void;
export function Table(arg: any): undefined | ClassDecorator {
if (typeof arg === 'function') {
annotate(arg);
return undefined;
}
const options: ModelOptions = { ...arg };
// @ts-expect-error -- making sure the option is not provided.
if (options.abstract) {
throw new Error(
'`abstract` is not a valid option for @Table. Did you mean to use @Table.Abstract?',
);
}
return (target: any) => annotate(target, options);
}
function AbstractTable<M extends Model = Model>(
options: Omit<ModelOptions<M>, 'tableName' | 'name'>,
): ClassDecorator;
function AbstractTable(target: ModelStatic): void;
function AbstractTable(arg: any): undefined | ClassDecorator {
if (typeof arg === 'function') {
annotate(arg, { abstract: true });
return undefined;
}
const options: ModelOptions = { ...arg, abstract: true };
if (options.tableName || options.name) {
throw new Error('Options "tableName" and "name" cannot be set on abstract models.');
}
return (target: any) => annotate(target, options);
}
Table.Abstract = AbstractTable;
function annotate(target: ModelStatic, options: RegisteredModelOptions = {}): void {
registerModelOptions(target, options);
}