/
model.ts
163 lines (133 loc) · 5.47 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
import type { ModelAttributeColumnOptions, ModelAttributes, ModelOptions, ModelStatic } from '../../model.js';
import type { Sequelize } from '../../sequelize.js';
interface RegisteredOptions {
model: ModelOptions;
attributes: { [key: string]: Partial<ModelAttributeColumnOptions> };
}
const registeredOptions = new WeakMap<ModelStatic, RegisteredOptions>();
/**
* Registers model options for future registering of the Model using Model.init
* Subsequent calls for the same model & attributeName will be merged, with the newer call taking precedence.
* 'sequelize' option is not accepted here. Pass it through `Model.init` when registering the model.
*
* @param model
* @param options
*/
export function registerModelOptions(
model: ModelStatic,
options: ModelOptions,
): void {
if (!registeredOptions.has(model)) {
registeredOptions.set(model, { model: options, attributes: {} });
return;
}
// merge-able: scopes, indexes, setterMethods, getterMethods
const existingModelOptions = registeredOptions.get(model)!.model;
for (const [optionName, optionValue] of Object.entries(options)) {
if (!(optionName in existingModelOptions)) {
// @ts-expect-error -- runtime type checking is enforced by model
existingModelOptions[optionName] = optionValue;
continue;
}
// These are objects. We merge their properties, unless the same key is used in both values.
if (optionName === 'scopes' || optionName === 'setterMethods' || optionName === 'getterMethods') {
for (const [subOptionName, subOptionValue] of Object.entries(optionValue)) {
if (subOptionName in existingModelOptions[optionName]!) {
throw new Error(`Multiple decorators are attempting to register option ${optionName}[${JSON.stringify(subOptionName)}] on model ${model.name}.`);
}
// @ts-expect-error -- runtime type checking is enforced by model
existingModelOptions[optionName][subOptionName] = subOptionValue;
}
continue;
}
// This is an array. Simple array merge.
if (optionName === 'indexes') {
existingModelOptions.indexes = [...existingModelOptions.indexes!, ...optionValue];
continue;
}
// @ts-expect-error
if (optionValue === existingModelOptions[optionName]) {
continue;
}
throw new Error(`Multiple decorators are attempting to set different values for the option ${optionName} on model ${model.name}.`);
}
}
/**
* Registers attribute options for future registering of the Model using Model.init
* Subsequent calls for the same model & attributeName will be merged, with the newer call taking precedence.
*
* @param model
* @param attributeName
* @param options
*/
export function registerModelAttributeOptions(
model: ModelStatic,
attributeName: string,
options: Partial<ModelAttributeColumnOptions>,
): void {
if (!registeredOptions.has(model)) {
registeredOptions.set(model, {
model: {},
attributes: {
[attributeName]: options,
},
});
return;
}
const existingAttributesOptions = registeredOptions.get(model)!.attributes;
if (!(attributeName in existingAttributesOptions)) {
existingAttributesOptions[attributeName] = options;
return;
}
const existingOptions = existingAttributesOptions[attributeName]!;
for (const [optionName, optionValue] of Object.entries(options)) {
if (!(optionName in existingOptions)) {
// @ts-expect-error -- runtime type checking is enforced by model
existingOptions[optionName] = optionValue;
continue;
}
// These are objects. We merge their properties, unless the same key is used in both values.
if (optionName === 'validate') {
// @ts-expect-error
for (const [subOptionName, subOptionValue] of Object.entries(optionValue)) {
if (subOptionName in existingOptions[optionName]!) {
throw new Error(`Multiple decorators are attempting to register option ${optionName}[${JSON.stringify(subOptionName)}] of attribute ${attributeName} on model ${model.name}.`);
}
// @ts-expect-error -- runtime type checking is enforced by model
existingOptions[optionName][subOptionName] = subOptionValue;
}
continue;
}
if (optionName === 'unique') {
if (!existingOptions.unique) {
existingOptions.unique = [];
} else if (!Array.isArray(existingOptions.unique)) {
existingOptions.unique = [existingOptions.unique];
}
if (Array.isArray(optionValue)) {
existingOptions.unique = [...existingOptions.unique, ...optionValue];
} else {
// @ts-expect-error -- runtime type checking is enforced by model
existingOptions.unique = [...existingOptions.unique, optionValue];
}
continue;
}
// @ts-expect-error
if (optionValue === existingOptions[optionName]) {
continue;
}
throw new Error(`Multiple decorators are attempting to set different values for the option ${optionName} of attribute ${attributeName} on model ${model.name}.`);
}
}
export function initDecoratedModel(model: ModelStatic, sequelize: Sequelize): void {
const { model: modelOptions, attributes: attributeOptions } = registeredOptions.get(model) ?? {};
// model.init will ensure all required attributeOptions have been specified.
// @ts-expect-error secret method
model._internalInit(attributeOptions as ModelAttributes, {
...modelOptions,
sequelize,
});
}
export function isDecoratedModel(model: ModelStatic): boolean {
return registeredOptions.has(model);
}