@spinajs/orm 2.0.180 → 2.0.182
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/builders.d.ts +643 -643
- package/lib/cjs/builders.js +1602 -1602
- package/lib/cjs/builders.js.map +1 -1
- package/lib/cjs/converters.d.ts +34 -34
- package/lib/cjs/converters.js +104 -104
- package/lib/cjs/decorators.d.ts +152 -152
- package/lib/cjs/decorators.js +449 -449
- package/lib/cjs/dehydrators.d.ts +10 -10
- package/lib/cjs/dehydrators.js +47 -47
- package/lib/cjs/driver.d.ts +82 -82
- package/lib/cjs/driver.js +102 -102
- package/lib/cjs/driver.js.map +1 -1
- package/lib/cjs/enums.d.ts +116 -116
- package/lib/cjs/enums.js +126 -126
- package/lib/cjs/enums.js.map +1 -1
- package/lib/cjs/exceptions.d.ts +6 -6
- package/lib/cjs/exceptions.js +10 -10
- package/lib/cjs/hydrators.d.ts +19 -19
- package/lib/cjs/hydrators.js +132 -132
- package/lib/cjs/hydrators.js.map +1 -1
- package/lib/cjs/index.d.ts +17 -17
- package/lib/cjs/index.js +33 -33
- package/lib/cjs/interfaces.d.ts +921 -919
- package/lib/cjs/interfaces.d.ts.map +1 -1
- package/lib/cjs/interfaces.js +279 -279
- package/lib/cjs/interfaces.js.map +1 -1
- package/lib/cjs/middlewares.d.ts +62 -62
- package/lib/cjs/middlewares.js +258 -258
- package/lib/cjs/model.d.ts +288 -284
- package/lib/cjs/model.d.ts.map +1 -1
- package/lib/cjs/model.js +826 -810
- package/lib/cjs/model.js.map +1 -1
- package/lib/cjs/orm.d.ts +61 -61
- package/lib/cjs/orm.js +333 -333
- package/lib/cjs/orm.js.map +1 -1
- package/lib/cjs/relation-objects.d.ts +108 -108
- package/lib/cjs/relation-objects.js +221 -221
- package/lib/cjs/relations.d.ts +61 -61
- package/lib/cjs/relations.js +194 -194
- package/lib/cjs/relations.js.map +1 -1
- package/lib/cjs/statements.d.ts +143 -143
- package/lib/cjs/statements.js +309 -309
- package/lib/cjs/statements.js.map +1 -1
- package/lib/cjs/types.d.ts +32 -32
- package/lib/cjs/types.js +2 -2
- package/lib/cjs/wrappers.d.ts +5 -5
- package/lib/cjs/wrappers.js +12 -12
- package/lib/mjs/builders.d.ts +643 -643
- package/lib/mjs/builders.js +1594 -1594
- package/lib/mjs/builders.js.map +1 -1
- package/lib/mjs/converters.d.ts +34 -34
- package/lib/mjs/converters.js +96 -96
- package/lib/mjs/decorators.d.ts +152 -152
- package/lib/mjs/decorators.js +422 -422
- package/lib/mjs/dehydrators.d.ts +10 -10
- package/lib/mjs/dehydrators.js +41 -41
- package/lib/mjs/driver.d.ts +82 -82
- package/lib/mjs/driver.js +98 -98
- package/lib/mjs/driver.js.map +1 -1
- package/lib/mjs/enums.d.ts +116 -116
- package/lib/mjs/enums.js +123 -123
- package/lib/mjs/enums.js.map +1 -1
- package/lib/mjs/exceptions.d.ts +6 -6
- package/lib/mjs/exceptions.js +6 -6
- package/lib/mjs/hydrators.d.ts +19 -19
- package/lib/mjs/hydrators.js +128 -128
- package/lib/mjs/hydrators.js.map +1 -1
- package/lib/mjs/index.d.ts +17 -17
- package/lib/mjs/index.js +17 -17
- package/lib/mjs/interfaces.d.ts +921 -919
- package/lib/mjs/interfaces.d.ts.map +1 -1
- package/lib/mjs/interfaces.js +267 -267
- package/lib/mjs/interfaces.js.map +1 -1
- package/lib/mjs/middlewares.d.ts +62 -62
- package/lib/mjs/middlewares.js +249 -249
- package/lib/mjs/model.d.ts +288 -284
- package/lib/mjs/model.d.ts.map +1 -1
- package/lib/mjs/model.js +816 -800
- package/lib/mjs/model.js.map +1 -1
- package/lib/mjs/orm.d.ts +61 -61
- package/lib/mjs/orm.js +326 -326
- package/lib/mjs/orm.js.map +1 -1
- package/lib/mjs/relation-objects.d.ts +108 -108
- package/lib/mjs/relation-objects.js +211 -211
- package/lib/mjs/relations.d.ts +61 -61
- package/lib/mjs/relations.js +191 -191
- package/lib/mjs/relations.js.map +1 -1
- package/lib/mjs/statements.d.ts +143 -143
- package/lib/mjs/statements.js +301 -301
- package/lib/mjs/statements.js.map +1 -1
- package/lib/mjs/types.d.ts +32 -32
- package/lib/mjs/types.js +1 -1
- package/lib/mjs/wrappers.d.ts +5 -5
- package/lib/mjs/wrappers.js +9 -9
- package/lib/tsconfig.cjs.tsbuildinfo +1 -1
- package/lib/tsconfig.mjs.tsbuildinfo +1 -1
- package/package.json +5 -5
package/lib/cjs/decorators.js
CHANGED
|
@@ -1,450 +1,450 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Set = exports.UniversalConverter = exports.Json = exports.DateTime = exports.HasManyToMany = exports.Historical = exports.HasMany = exports.ForwardBelongsTo = exports.BelongsTo = exports.forwardRef = exports.Recursive = exports.DiscriminationMap = exports.JunctionTable = exports.Uuid = exports.Ignore = exports.Primary = exports.Archived = exports.SoftDelete = exports.UpdatedAt = exports.CreatedAt = exports.Model = exports.Connection = exports.Migration = exports.extractDecoratorDescriptor = exports.MIGRATION_DESCRIPTION_SYMBOL = exports.MODEL_DESCTRIPTION_SYMBOL = void 0;
|
|
4
|
-
/* eslint-disable prettier/prettier */
|
|
5
|
-
const converters_js_1 = require("./converters.js");
|
|
6
|
-
const di_1 = require("@spinajs/di");
|
|
7
|
-
const interfaces_js_1 = require("./interfaces.js");
|
|
8
|
-
require("reflect-metadata");
|
|
9
|
-
const model_js_1 = require("./model.js");
|
|
10
|
-
const exceptions_1 = require("@spinajs/exceptions");
|
|
11
|
-
exports.MODEL_DESCTRIPTION_SYMBOL = Symbol.for('MODEL_DESCRIPTOR');
|
|
12
|
-
exports.MIGRATION_DESCRIPTION_SYMBOL = Symbol.for('MIGRATION_DESCRIPTOR');
|
|
13
|
-
/**
|
|
14
|
-
* Helper func to create model metadata
|
|
15
|
-
*/
|
|
16
|
-
function extractDecoratorDescriptor(callback, base = false) {
|
|
17
|
-
return (target, propertyKey, indexOrDescriptor) => {
|
|
18
|
-
let metadata = null;
|
|
19
|
-
if (!base) {
|
|
20
|
-
metadata = target.constructor[exports.MODEL_DESCTRIPTION_SYMBOL];
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
metadata = target[exports.MODEL_DESCTRIPTION_SYMBOL];
|
|
24
|
-
}
|
|
25
|
-
if (!metadata) {
|
|
26
|
-
metadata = {
|
|
27
|
-
Driver: null,
|
|
28
|
-
Converters: new Map(),
|
|
29
|
-
Columns: [],
|
|
30
|
-
Connection: null,
|
|
31
|
-
PrimaryKey: '',
|
|
32
|
-
SoftDelete: {
|
|
33
|
-
DeletedAt: '',
|
|
34
|
-
},
|
|
35
|
-
Archived: {
|
|
36
|
-
ArchivedAt: '',
|
|
37
|
-
},
|
|
38
|
-
TableName: '',
|
|
39
|
-
Timestamps: {
|
|
40
|
-
CreatedAt: '',
|
|
41
|
-
UpdatedAt: '',
|
|
42
|
-
},
|
|
43
|
-
Relations: new Map(),
|
|
44
|
-
Name: target.constructor.name,
|
|
45
|
-
JunctionModelProperties: [],
|
|
46
|
-
DiscriminationMap: {
|
|
47
|
-
Field: '',
|
|
48
|
-
Models: null,
|
|
49
|
-
},
|
|
50
|
-
Schema: {},
|
|
51
|
-
};
|
|
52
|
-
if (!base) {
|
|
53
|
-
target.constructor[exports.MODEL_DESCTRIPTION_SYMBOL] = metadata;
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
target[exports.MODEL_DESCTRIPTION_SYMBOL] = metadata;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
if (callback) {
|
|
60
|
-
callback(metadata, target, propertyKey, indexOrDescriptor);
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
exports.extractDecoratorDescriptor = extractDecoratorDescriptor;
|
|
65
|
-
/**
|
|
66
|
-
* Sets migration option
|
|
67
|
-
*
|
|
68
|
-
* @param connection - connection name, must exists in configuration file
|
|
69
|
-
*/
|
|
70
|
-
function Migration(connection) {
|
|
71
|
-
return (target) => {
|
|
72
|
-
let metadata = target[exports.MIGRATION_DESCRIPTION_SYMBOL];
|
|
73
|
-
if (!metadata) {
|
|
74
|
-
metadata = {
|
|
75
|
-
Connection: '',
|
|
76
|
-
};
|
|
77
|
-
target[exports.MIGRATION_DESCRIPTION_SYMBOL] = metadata;
|
|
78
|
-
}
|
|
79
|
-
metadata.Connection = connection;
|
|
80
|
-
di_1.DI.register(target).as('__migrations__');
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
exports.Migration = Migration;
|
|
84
|
-
/**
|
|
85
|
-
* Connection model decorator, assigns connection to model
|
|
86
|
-
*
|
|
87
|
-
* @param name - connection name, must be avaible in db config
|
|
88
|
-
*/
|
|
89
|
-
function Connection(name) {
|
|
90
|
-
return extractDecoratorDescriptor((model) => {
|
|
91
|
-
model.Connection = name;
|
|
92
|
-
}, true);
|
|
93
|
-
}
|
|
94
|
-
exports.Connection = Connection;
|
|
95
|
-
/**
|
|
96
|
-
* TableName model decorator, assigns table from database to model
|
|
97
|
-
*
|
|
98
|
-
* @param name - table name in database that is referred by this model
|
|
99
|
-
*/
|
|
100
|
-
function Model(tableName) {
|
|
101
|
-
return extractDecoratorDescriptor((model, target) => {
|
|
102
|
-
di_1.DI.register(target).as('__models__');
|
|
103
|
-
model.TableName = tableName;
|
|
104
|
-
model.Name = target.name;
|
|
105
|
-
}, true);
|
|
106
|
-
}
|
|
107
|
-
exports.Model = Model;
|
|
108
|
-
/**
|
|
109
|
-
* Set create timestamps feature to model. Proper columns must be avaible in database table.
|
|
110
|
-
* It allow to track creation times & changes to model
|
|
111
|
-
*/
|
|
112
|
-
function CreatedAt() {
|
|
113
|
-
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
114
|
-
const type = Reflect.getMetadata('design:type', target, propertyKey);
|
|
115
|
-
if (type.name !== 'DateTime') {
|
|
116
|
-
throw Error(`Proprety ${propertyKey} marked as CreatedAt must be DateTime type, but is ${type.name}. Type: ${target.name}`);
|
|
117
|
-
}
|
|
118
|
-
model.Timestamps.CreatedAt = propertyKey;
|
|
119
|
-
// add converter for this field
|
|
120
|
-
model.Converters.set(propertyKey, {
|
|
121
|
-
Class: interfaces_js_1.DatetimeValueConverter,
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
exports.CreatedAt = CreatedAt;
|
|
126
|
-
/**
|
|
127
|
-
* Set update timestamps feature to model. Proper columns must be avaible in database table.
|
|
128
|
-
* It allow to track creation times & changes to model
|
|
129
|
-
*/
|
|
130
|
-
function UpdatedAt() {
|
|
131
|
-
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
132
|
-
const type = Reflect.getMetadata('design:type', target, propertyKey);
|
|
133
|
-
if (type.name !== 'DateTime') {
|
|
134
|
-
throw Error(`Proprety ${propertyKey} marked as UpdatedAt must be DateTime type, but is ${type.name}. Type: ${target.name}`);
|
|
135
|
-
}
|
|
136
|
-
model.Timestamps.UpdatedAt = propertyKey;
|
|
137
|
-
// add converter for this field
|
|
138
|
-
model.Converters.set(propertyKey, {
|
|
139
|
-
Class: interfaces_js_1.DatetimeValueConverter,
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
exports.UpdatedAt = UpdatedAt;
|
|
144
|
-
/**
|
|
145
|
-
* Sets soft delete feature to model. Soft delete dont delete model, but sets deletion date and hides from
|
|
146
|
-
* select result by default.
|
|
147
|
-
*/
|
|
148
|
-
function SoftDelete() {
|
|
149
|
-
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
150
|
-
const type = Reflect.getMetadata('design:type', target, propertyKey);
|
|
151
|
-
if (type.name !== 'DateTime') {
|
|
152
|
-
throw Error(`Proprety ${propertyKey} marked as DeletedAt must be DateTime type, but is ${type.name}. Type: ${target.name}`);
|
|
153
|
-
}
|
|
154
|
-
model.SoftDelete.DeletedAt = propertyKey;
|
|
155
|
-
// add converter for this field
|
|
156
|
-
model.Converters.set(propertyKey, {
|
|
157
|
-
Class: interfaces_js_1.DatetimeValueConverter,
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
exports.SoftDelete = SoftDelete;
|
|
162
|
-
/**
|
|
163
|
-
* Enable archive mode for model. If enabled all changes creates new instance in DB and old have set archived field
|
|
164
|
-
* and gets attached to new model. It enabled to track changes to model in DB and also preserve data in relations.
|
|
165
|
-
*
|
|
166
|
-
*/
|
|
167
|
-
function Archived() {
|
|
168
|
-
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
169
|
-
const type = Reflect.getMetadata('design:type', target, propertyKey);
|
|
170
|
-
if (type.name !== 'DateTime') {
|
|
171
|
-
throw Error(`Proprety ${propertyKey} marked as ArchivedAt must be DateTime type, but is ${type.name}. Type: ${target.name}`);
|
|
172
|
-
}
|
|
173
|
-
model.Archived.ArchivedAt = propertyKey;
|
|
174
|
-
// add converter for this field
|
|
175
|
-
model.Converters.set(propertyKey, {
|
|
176
|
-
Class: interfaces_js_1.DatetimeValueConverter,
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
exports.Archived = Archived;
|
|
181
|
-
/**
|
|
182
|
-
* Makrs field as primary key
|
|
183
|
-
*/
|
|
184
|
-
function Primary() {
|
|
185
|
-
return extractDecoratorDescriptor((model, _target, propertyKey) => {
|
|
186
|
-
model.PrimaryKey = propertyKey;
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
exports.Primary = Primary;
|
|
190
|
-
/**
|
|
191
|
-
* Marks columns as UUID. Column will be generated ad creation
|
|
192
|
-
*/
|
|
193
|
-
function Ignore() {
|
|
194
|
-
return extractDecoratorDescriptor((model, _target, propertyKey) => {
|
|
195
|
-
const columnDesc = model.Columns.find((c) => c.Name === propertyKey);
|
|
196
|
-
if (!columnDesc) {
|
|
197
|
-
// we dont want to fill all props, they will be loaded from db and mergeg with this
|
|
198
|
-
model.Columns.push({ Name: propertyKey, Ignore: true });
|
|
199
|
-
}
|
|
200
|
-
else {
|
|
201
|
-
columnDesc.Ignore = true;
|
|
202
|
-
}
|
|
203
|
-
}, true);
|
|
204
|
-
}
|
|
205
|
-
exports.Ignore = Ignore;
|
|
206
|
-
/**
|
|
207
|
-
* Marks columns as UUID. Column will be generated ad creation
|
|
208
|
-
*/
|
|
209
|
-
function Uuid() {
|
|
210
|
-
return extractDecoratorDescriptor((model, _target, propertyKey) => {
|
|
211
|
-
const columnDesc = model.Columns.find((c) => c.Name === propertyKey);
|
|
212
|
-
if (!columnDesc) {
|
|
213
|
-
// we dont want to fill all props, they will be loaded from db and mergeg with this
|
|
214
|
-
model.Columns.push({ Name: propertyKey, Uuid: true });
|
|
215
|
-
}
|
|
216
|
-
else {
|
|
217
|
-
columnDesc.Uuid = true;
|
|
218
|
-
}
|
|
219
|
-
model.Converters.set(propertyKey, {
|
|
220
|
-
Class: converters_js_1.UuidConverter,
|
|
221
|
-
});
|
|
222
|
-
}, true);
|
|
223
|
-
}
|
|
224
|
-
exports.Uuid = Uuid;
|
|
225
|
-
function JunctionTable() {
|
|
226
|
-
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
227
|
-
model.JunctionModelProperties.push({
|
|
228
|
-
Name: propertyKey,
|
|
229
|
-
Model: Reflect.getMetadata('design:type', target, propertyKey),
|
|
230
|
-
});
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
exports.JunctionTable = JunctionTable;
|
|
234
|
-
/**
|
|
235
|
-
*
|
|
236
|
-
* Marks model to have discrimination map.
|
|
237
|
-
*
|
|
238
|
-
* @param fieldName - db field name to look for
|
|
239
|
-
* @param discriminationMap - field - model mapping
|
|
240
|
-
*/
|
|
241
|
-
function DiscriminationMap(fieldName, discriminationMap) {
|
|
242
|
-
return extractDecoratorDescriptor((model, _target, _propertyKey) => {
|
|
243
|
-
model.DiscriminationMap.Field = fieldName;
|
|
244
|
-
model.DiscriminationMap.Models = new Map();
|
|
245
|
-
discriminationMap.forEach((d) => {
|
|
246
|
-
model.DiscriminationMap.Models.set(d.Key, d.Value);
|
|
247
|
-
});
|
|
248
|
-
}, true);
|
|
249
|
-
}
|
|
250
|
-
exports.DiscriminationMap = DiscriminationMap;
|
|
251
|
-
/**
|
|
252
|
-
* Marks relation as recursive. When relation is populated it loads all to the top
|
|
253
|
-
*
|
|
254
|
-
*/
|
|
255
|
-
function Recursive() {
|
|
256
|
-
return extractDecoratorDescriptor((model, _target, propertyKey) => {
|
|
257
|
-
if (!model.Relations.has(propertyKey)) {
|
|
258
|
-
throw new exceptions_1.InvalidOperation(`cannot set recursive on not existing relation ( relation ${propertyKey} on model ${model.Name} )`);
|
|
259
|
-
}
|
|
260
|
-
const relation = model.Relations.get(propertyKey);
|
|
261
|
-
relation.Recursive = true;
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
exports.Recursive = Recursive;
|
|
265
|
-
const forwardRef = (fn) => ({
|
|
266
|
-
forwardRef: fn,
|
|
267
|
-
});
|
|
268
|
-
exports.forwardRef = forwardRef;
|
|
269
|
-
/**
|
|
270
|
-
* Creates one to one relation with target model.
|
|
271
|
-
*
|
|
272
|
-
* @param foreignKey - foreign key name in db, defaults to lowercase property name with _id suffix eg. owner_id
|
|
273
|
-
* @param primaryKey - primary key in related model, defaults to primary key taken from db
|
|
274
|
-
*/
|
|
275
|
-
function BelongsTo(targetModel, foreignKey, primaryKey) {
|
|
276
|
-
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
277
|
-
model.Relations.set(propertyKey, {
|
|
278
|
-
Name: propertyKey,
|
|
279
|
-
Type: interfaces_js_1.RelationType.One,
|
|
280
|
-
SourceModel: target.constructor,
|
|
281
|
-
TargetModelType: targetModel,
|
|
282
|
-
TargetModel: null,
|
|
283
|
-
ForeignKey: foreignKey ?? `${propertyKey.toLowerCase()}_id`,
|
|
284
|
-
PrimaryKey: primaryKey ?? model.PrimaryKey,
|
|
285
|
-
Recursive: false,
|
|
286
|
-
});
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
exports.BelongsTo = BelongsTo;
|
|
290
|
-
/**
|
|
291
|
-
* Creates one to one relation with target model.
|
|
292
|
-
*
|
|
293
|
-
* @param foreignKey - foreign key name in db, defaults to lowercase property name with _id suffix eg. owner_id
|
|
294
|
-
* @param primaryKey - primary key in related model, defaults to primary key taken from db
|
|
295
|
-
*/
|
|
296
|
-
function ForwardBelongsTo(forwardRef, foreignKey, primaryKey) {
|
|
297
|
-
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
298
|
-
model.Relations.set(propertyKey, {
|
|
299
|
-
Name: propertyKey,
|
|
300
|
-
Type: interfaces_js_1.RelationType.One,
|
|
301
|
-
SourceModel: target.constructor,
|
|
302
|
-
TargetModelType: forwardRef.forwardRef,
|
|
303
|
-
TargetModel: null,
|
|
304
|
-
ForeignKey: foreignKey ?? `${propertyKey.toLowerCase()}_id`,
|
|
305
|
-
PrimaryKey: primaryKey ?? model.PrimaryKey,
|
|
306
|
-
Recursive: false,
|
|
307
|
-
});
|
|
308
|
-
});
|
|
309
|
-
}
|
|
310
|
-
exports.ForwardBelongsTo = ForwardBelongsTo;
|
|
311
|
-
/**
|
|
312
|
-
* Creates one to many relation with target model.
|
|
313
|
-
*
|
|
314
|
-
* @param targetModel - due to limitations of metadata reflection api in typescript target model mus be set explicitly
|
|
315
|
-
* @param foreignKey - foreign key name in db, defaults to lowercase property name with _id suffix eg. owner_id
|
|
316
|
-
* @param primaryKey - primary key in source table defaults to lowercase property name with _id suffix eg. owner_id
|
|
317
|
-
*
|
|
318
|
-
*/
|
|
319
|
-
function HasMany(targetModel, options) {
|
|
320
|
-
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
321
|
-
model.Relations.set(propertyKey, {
|
|
322
|
-
Name: propertyKey,
|
|
323
|
-
Type: interfaces_js_1.RelationType.Many,
|
|
324
|
-
SourceModel: target.constructor,
|
|
325
|
-
TargetModelType: targetModel,
|
|
326
|
-
TargetModel: null,
|
|
327
|
-
ForeignKey: options ? options.foreignKey ?? `${model.Name.toLowerCase()}_id` : `${model.Name.toLowerCase()}_id`,
|
|
328
|
-
PrimaryKey: options ? options.primaryKey ?? model.PrimaryKey : model.PrimaryKey,
|
|
329
|
-
Recursive: false,
|
|
330
|
-
Factory: options ? options.factory : null,
|
|
331
|
-
RelationClass: options ? options.type : null,
|
|
332
|
-
});
|
|
333
|
-
});
|
|
334
|
-
}
|
|
335
|
-
exports.HasMany = HasMany;
|
|
336
|
-
function Historical(targetModel) {
|
|
337
|
-
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
338
|
-
model.Relations.set(propertyKey, {
|
|
339
|
-
Name: propertyKey,
|
|
340
|
-
Type: interfaces_js_1.RelationType.Many,
|
|
341
|
-
SourceModel: target.constructor,
|
|
342
|
-
TargetModelType: targetModel,
|
|
343
|
-
TargetModel: null,
|
|
344
|
-
ForeignKey: model.PrimaryKey,
|
|
345
|
-
PrimaryKey: model.PrimaryKey,
|
|
346
|
-
Recursive: false,
|
|
347
|
-
});
|
|
348
|
-
});
|
|
349
|
-
}
|
|
350
|
-
exports.Historical = Historical;
|
|
351
|
-
/**
|
|
352
|
-
* Creates many to many relation with separate join table
|
|
353
|
-
*
|
|
354
|
-
* @param junctionModel - model for junction table
|
|
355
|
-
* @param targetModel - model for related data
|
|
356
|
-
* @param targetModelPKey - target model primary key name
|
|
357
|
-
* @param sourceModelPKey - source model primary key name
|
|
358
|
-
* @param junctionModelTargetPk - junction table target primary key name ( foreign key for target model )
|
|
359
|
-
* @param junctionModelSourcePk - junction table source primary key name ( foreign key for source model )
|
|
360
|
-
*/
|
|
361
|
-
function HasManyToMany(junctionModel, targetModel, targetModelPKey, sourceModelPKey, junctionModelTargetPk, junctionModelSourcePk) {
|
|
362
|
-
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
363
|
-
const targetModelDescriptor = (0, model_js_1.extractModelDescriptor)(targetModel);
|
|
364
|
-
model.Relations.set(propertyKey, {
|
|
365
|
-
Name: propertyKey,
|
|
366
|
-
Recursive: false,
|
|
367
|
-
Type: interfaces_js_1.RelationType.ManyToMany,
|
|
368
|
-
SourceModel: target.constructor,
|
|
369
|
-
TargetModelType: targetModel,
|
|
370
|
-
TargetModel: null,
|
|
371
|
-
ForeignKey: targetModelPKey ?? targetModelDescriptor.PrimaryKey,
|
|
372
|
-
PrimaryKey: sourceModelPKey ?? model.PrimaryKey,
|
|
373
|
-
JunctionModel: junctionModel,
|
|
374
|
-
JunctionModelTargetModelFKey_Name: junctionModelTargetPk ?? `${targetModelDescriptor.Name.toLowerCase()}_id`,
|
|
375
|
-
JunctionModelSourceModelFKey_Name: junctionModelSourcePk ?? `${model.Name.toLowerCase()}_id`,
|
|
376
|
-
});
|
|
377
|
-
});
|
|
378
|
-
}
|
|
379
|
-
exports.HasManyToMany = HasManyToMany;
|
|
380
|
-
/**
|
|
381
|
-
* Mark field as datetime type. It will ensure that conversion to & from DB is valid, eg. sqlite DB
|
|
382
|
-
* saves datetime as TEXT and ISO8601 strings
|
|
383
|
-
*/
|
|
384
|
-
function DateTime() {
|
|
385
|
-
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
386
|
-
const type = Reflect.getMetadata('design:type', target, propertyKey);
|
|
387
|
-
if (type.name !== 'DateTime') {
|
|
388
|
-
throw Error(`Proprety ${propertyKey} must be DateTime type`);
|
|
389
|
-
}
|
|
390
|
-
if (model.Converters.has(propertyKey)) {
|
|
391
|
-
throw new exceptions_1.InvalidArgument(`property ${propertyKey} already have data converter attached`);
|
|
392
|
-
}
|
|
393
|
-
model.Converters.set(propertyKey, {
|
|
394
|
-
Class: interfaces_js_1.DatetimeValueConverter,
|
|
395
|
-
});
|
|
396
|
-
});
|
|
397
|
-
}
|
|
398
|
-
exports.DateTime = DateTime;
|
|
399
|
-
/**
|
|
400
|
-
* Converts data in db to json object. Column type in DB should be STRING.
|
|
401
|
-
* DO not use this decorator for use of native DB JSON implementation.
|
|
402
|
-
* ORM will detect automatically if field is native JSON DB type.
|
|
403
|
-
*/
|
|
404
|
-
function Json() {
|
|
405
|
-
return extractDecoratorDescriptor((model, _, propertyKey) => {
|
|
406
|
-
// add converter for this field
|
|
407
|
-
model.Converters.set(propertyKey, {
|
|
408
|
-
Class: converters_js_1.JsonValueConverter,
|
|
409
|
-
});
|
|
410
|
-
});
|
|
411
|
-
}
|
|
412
|
-
exports.Json = Json;
|
|
413
|
-
/**
|
|
414
|
-
*
|
|
415
|
-
* Universal converter that guess whitch type to return. Usefull in tables that holds as text different values
|
|
416
|
-
* eg. metadata table
|
|
417
|
-
*
|
|
418
|
-
* @param typeColumn - type column that defines final type of value
|
|
419
|
-
*/
|
|
420
|
-
function UniversalConverter(typeColumn) {
|
|
421
|
-
return extractDecoratorDescriptor((model, _, propertyKey) => {
|
|
422
|
-
// add converter for this field
|
|
423
|
-
model.Converters.set(propertyKey, {
|
|
424
|
-
Class: converters_js_1.JsonValueConverter,
|
|
425
|
-
Options: {
|
|
426
|
-
TypeColumn: typeColumn,
|
|
427
|
-
},
|
|
428
|
-
});
|
|
429
|
-
});
|
|
430
|
-
}
|
|
431
|
-
exports.UniversalConverter = UniversalConverter;
|
|
432
|
-
/**
|
|
433
|
-
* Mark field as SET type. It will ensure that conversion to & from DB is valid, eg. to emulate field type SET in sqlite
|
|
434
|
-
*/
|
|
435
|
-
function Set() {
|
|
436
|
-
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
437
|
-
const type = Reflect.getMetadata('design:type', target, propertyKey);
|
|
438
|
-
if (type.name !== 'Array') {
|
|
439
|
-
throw Error(`Proprety ${propertyKey} must be an array type`);
|
|
440
|
-
}
|
|
441
|
-
if (model.Converters.has(propertyKey)) {
|
|
442
|
-
throw new exceptions_1.InvalidArgument(`property ${propertyKey} already have data converter attached`);
|
|
443
|
-
}
|
|
444
|
-
model.Converters.set(propertyKey, {
|
|
445
|
-
Class: interfaces_js_1.SetValueConverter,
|
|
446
|
-
});
|
|
447
|
-
});
|
|
448
|
-
}
|
|
449
|
-
exports.Set = Set;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Set = exports.UniversalConverter = exports.Json = exports.DateTime = exports.HasManyToMany = exports.Historical = exports.HasMany = exports.ForwardBelongsTo = exports.BelongsTo = exports.forwardRef = exports.Recursive = exports.DiscriminationMap = exports.JunctionTable = exports.Uuid = exports.Ignore = exports.Primary = exports.Archived = exports.SoftDelete = exports.UpdatedAt = exports.CreatedAt = exports.Model = exports.Connection = exports.Migration = exports.extractDecoratorDescriptor = exports.MIGRATION_DESCRIPTION_SYMBOL = exports.MODEL_DESCTRIPTION_SYMBOL = void 0;
|
|
4
|
+
/* eslint-disable prettier/prettier */
|
|
5
|
+
const converters_js_1 = require("./converters.js");
|
|
6
|
+
const di_1 = require("@spinajs/di");
|
|
7
|
+
const interfaces_js_1 = require("./interfaces.js");
|
|
8
|
+
require("reflect-metadata");
|
|
9
|
+
const model_js_1 = require("./model.js");
|
|
10
|
+
const exceptions_1 = require("@spinajs/exceptions");
|
|
11
|
+
exports.MODEL_DESCTRIPTION_SYMBOL = Symbol.for('MODEL_DESCRIPTOR');
|
|
12
|
+
exports.MIGRATION_DESCRIPTION_SYMBOL = Symbol.for('MIGRATION_DESCRIPTOR');
|
|
13
|
+
/**
|
|
14
|
+
* Helper func to create model metadata
|
|
15
|
+
*/
|
|
16
|
+
function extractDecoratorDescriptor(callback, base = false) {
|
|
17
|
+
return (target, propertyKey, indexOrDescriptor) => {
|
|
18
|
+
let metadata = null;
|
|
19
|
+
if (!base) {
|
|
20
|
+
metadata = target.constructor[exports.MODEL_DESCTRIPTION_SYMBOL];
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
metadata = target[exports.MODEL_DESCTRIPTION_SYMBOL];
|
|
24
|
+
}
|
|
25
|
+
if (!metadata) {
|
|
26
|
+
metadata = {
|
|
27
|
+
Driver: null,
|
|
28
|
+
Converters: new Map(),
|
|
29
|
+
Columns: [],
|
|
30
|
+
Connection: null,
|
|
31
|
+
PrimaryKey: '',
|
|
32
|
+
SoftDelete: {
|
|
33
|
+
DeletedAt: '',
|
|
34
|
+
},
|
|
35
|
+
Archived: {
|
|
36
|
+
ArchivedAt: '',
|
|
37
|
+
},
|
|
38
|
+
TableName: '',
|
|
39
|
+
Timestamps: {
|
|
40
|
+
CreatedAt: '',
|
|
41
|
+
UpdatedAt: '',
|
|
42
|
+
},
|
|
43
|
+
Relations: new Map(),
|
|
44
|
+
Name: target.constructor.name,
|
|
45
|
+
JunctionModelProperties: [],
|
|
46
|
+
DiscriminationMap: {
|
|
47
|
+
Field: '',
|
|
48
|
+
Models: null,
|
|
49
|
+
},
|
|
50
|
+
Schema: {},
|
|
51
|
+
};
|
|
52
|
+
if (!base) {
|
|
53
|
+
target.constructor[exports.MODEL_DESCTRIPTION_SYMBOL] = metadata;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
target[exports.MODEL_DESCTRIPTION_SYMBOL] = metadata;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (callback) {
|
|
60
|
+
callback(metadata, target, propertyKey, indexOrDescriptor);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
exports.extractDecoratorDescriptor = extractDecoratorDescriptor;
|
|
65
|
+
/**
|
|
66
|
+
* Sets migration option
|
|
67
|
+
*
|
|
68
|
+
* @param connection - connection name, must exists in configuration file
|
|
69
|
+
*/
|
|
70
|
+
function Migration(connection) {
|
|
71
|
+
return (target) => {
|
|
72
|
+
let metadata = target[exports.MIGRATION_DESCRIPTION_SYMBOL];
|
|
73
|
+
if (!metadata) {
|
|
74
|
+
metadata = {
|
|
75
|
+
Connection: '',
|
|
76
|
+
};
|
|
77
|
+
target[exports.MIGRATION_DESCRIPTION_SYMBOL] = metadata;
|
|
78
|
+
}
|
|
79
|
+
metadata.Connection = connection;
|
|
80
|
+
di_1.DI.register(target).as('__migrations__');
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
exports.Migration = Migration;
|
|
84
|
+
/**
|
|
85
|
+
* Connection model decorator, assigns connection to model
|
|
86
|
+
*
|
|
87
|
+
* @param name - connection name, must be avaible in db config
|
|
88
|
+
*/
|
|
89
|
+
function Connection(name) {
|
|
90
|
+
return extractDecoratorDescriptor((model) => {
|
|
91
|
+
model.Connection = name;
|
|
92
|
+
}, true);
|
|
93
|
+
}
|
|
94
|
+
exports.Connection = Connection;
|
|
95
|
+
/**
|
|
96
|
+
* TableName model decorator, assigns table from database to model
|
|
97
|
+
*
|
|
98
|
+
* @param name - table name in database that is referred by this model
|
|
99
|
+
*/
|
|
100
|
+
function Model(tableName) {
|
|
101
|
+
return extractDecoratorDescriptor((model, target) => {
|
|
102
|
+
di_1.DI.register(target).as('__models__');
|
|
103
|
+
model.TableName = tableName;
|
|
104
|
+
model.Name = target.name;
|
|
105
|
+
}, true);
|
|
106
|
+
}
|
|
107
|
+
exports.Model = Model;
|
|
108
|
+
/**
|
|
109
|
+
* Set create timestamps feature to model. Proper columns must be avaible in database table.
|
|
110
|
+
* It allow to track creation times & changes to model
|
|
111
|
+
*/
|
|
112
|
+
function CreatedAt() {
|
|
113
|
+
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
114
|
+
const type = Reflect.getMetadata('design:type', target, propertyKey);
|
|
115
|
+
if (type.name !== 'DateTime') {
|
|
116
|
+
throw Error(`Proprety ${propertyKey} marked as CreatedAt must be DateTime type, but is ${type.name}. Type: ${target.name}`);
|
|
117
|
+
}
|
|
118
|
+
model.Timestamps.CreatedAt = propertyKey;
|
|
119
|
+
// add converter for this field
|
|
120
|
+
model.Converters.set(propertyKey, {
|
|
121
|
+
Class: interfaces_js_1.DatetimeValueConverter,
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
exports.CreatedAt = CreatedAt;
|
|
126
|
+
/**
|
|
127
|
+
* Set update timestamps feature to model. Proper columns must be avaible in database table.
|
|
128
|
+
* It allow to track creation times & changes to model
|
|
129
|
+
*/
|
|
130
|
+
function UpdatedAt() {
|
|
131
|
+
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
132
|
+
const type = Reflect.getMetadata('design:type', target, propertyKey);
|
|
133
|
+
if (type.name !== 'DateTime') {
|
|
134
|
+
throw Error(`Proprety ${propertyKey} marked as UpdatedAt must be DateTime type, but is ${type.name}. Type: ${target.name}`);
|
|
135
|
+
}
|
|
136
|
+
model.Timestamps.UpdatedAt = propertyKey;
|
|
137
|
+
// add converter for this field
|
|
138
|
+
model.Converters.set(propertyKey, {
|
|
139
|
+
Class: interfaces_js_1.DatetimeValueConverter,
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
exports.UpdatedAt = UpdatedAt;
|
|
144
|
+
/**
|
|
145
|
+
* Sets soft delete feature to model. Soft delete dont delete model, but sets deletion date and hides from
|
|
146
|
+
* select result by default.
|
|
147
|
+
*/
|
|
148
|
+
function SoftDelete() {
|
|
149
|
+
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
150
|
+
const type = Reflect.getMetadata('design:type', target, propertyKey);
|
|
151
|
+
if (type.name !== 'DateTime') {
|
|
152
|
+
throw Error(`Proprety ${propertyKey} marked as DeletedAt must be DateTime type, but is ${type.name}. Type: ${target.name}`);
|
|
153
|
+
}
|
|
154
|
+
model.SoftDelete.DeletedAt = propertyKey;
|
|
155
|
+
// add converter for this field
|
|
156
|
+
model.Converters.set(propertyKey, {
|
|
157
|
+
Class: interfaces_js_1.DatetimeValueConverter,
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
exports.SoftDelete = SoftDelete;
|
|
162
|
+
/**
|
|
163
|
+
* Enable archive mode for model. If enabled all changes creates new instance in DB and old have set archived field
|
|
164
|
+
* and gets attached to new model. It enabled to track changes to model in DB and also preserve data in relations.
|
|
165
|
+
*
|
|
166
|
+
*/
|
|
167
|
+
function Archived() {
|
|
168
|
+
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
169
|
+
const type = Reflect.getMetadata('design:type', target, propertyKey);
|
|
170
|
+
if (type.name !== 'DateTime') {
|
|
171
|
+
throw Error(`Proprety ${propertyKey} marked as ArchivedAt must be DateTime type, but is ${type.name}. Type: ${target.name}`);
|
|
172
|
+
}
|
|
173
|
+
model.Archived.ArchivedAt = propertyKey;
|
|
174
|
+
// add converter for this field
|
|
175
|
+
model.Converters.set(propertyKey, {
|
|
176
|
+
Class: interfaces_js_1.DatetimeValueConverter,
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
exports.Archived = Archived;
|
|
181
|
+
/**
|
|
182
|
+
* Makrs field as primary key
|
|
183
|
+
*/
|
|
184
|
+
function Primary() {
|
|
185
|
+
return extractDecoratorDescriptor((model, _target, propertyKey) => {
|
|
186
|
+
model.PrimaryKey = propertyKey;
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
exports.Primary = Primary;
|
|
190
|
+
/**
|
|
191
|
+
* Marks columns as UUID. Column will be generated ad creation
|
|
192
|
+
*/
|
|
193
|
+
function Ignore() {
|
|
194
|
+
return extractDecoratorDescriptor((model, _target, propertyKey) => {
|
|
195
|
+
const columnDesc = model.Columns.find((c) => c.Name === propertyKey);
|
|
196
|
+
if (!columnDesc) {
|
|
197
|
+
// we dont want to fill all props, they will be loaded from db and mergeg with this
|
|
198
|
+
model.Columns.push({ Name: propertyKey, Ignore: true });
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
columnDesc.Ignore = true;
|
|
202
|
+
}
|
|
203
|
+
}, true);
|
|
204
|
+
}
|
|
205
|
+
exports.Ignore = Ignore;
|
|
206
|
+
/**
|
|
207
|
+
* Marks columns as UUID. Column will be generated ad creation
|
|
208
|
+
*/
|
|
209
|
+
function Uuid() {
|
|
210
|
+
return extractDecoratorDescriptor((model, _target, propertyKey) => {
|
|
211
|
+
const columnDesc = model.Columns.find((c) => c.Name === propertyKey);
|
|
212
|
+
if (!columnDesc) {
|
|
213
|
+
// we dont want to fill all props, they will be loaded from db and mergeg with this
|
|
214
|
+
model.Columns.push({ Name: propertyKey, Uuid: true });
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
columnDesc.Uuid = true;
|
|
218
|
+
}
|
|
219
|
+
model.Converters.set(propertyKey, {
|
|
220
|
+
Class: converters_js_1.UuidConverter,
|
|
221
|
+
});
|
|
222
|
+
}, true);
|
|
223
|
+
}
|
|
224
|
+
exports.Uuid = Uuid;
|
|
225
|
+
function JunctionTable() {
|
|
226
|
+
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
227
|
+
model.JunctionModelProperties.push({
|
|
228
|
+
Name: propertyKey,
|
|
229
|
+
Model: Reflect.getMetadata('design:type', target, propertyKey),
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
exports.JunctionTable = JunctionTable;
|
|
234
|
+
/**
|
|
235
|
+
*
|
|
236
|
+
* Marks model to have discrimination map.
|
|
237
|
+
*
|
|
238
|
+
* @param fieldName - db field name to look for
|
|
239
|
+
* @param discriminationMap - field - model mapping
|
|
240
|
+
*/
|
|
241
|
+
function DiscriminationMap(fieldName, discriminationMap) {
|
|
242
|
+
return extractDecoratorDescriptor((model, _target, _propertyKey) => {
|
|
243
|
+
model.DiscriminationMap.Field = fieldName;
|
|
244
|
+
model.DiscriminationMap.Models = new Map();
|
|
245
|
+
discriminationMap.forEach((d) => {
|
|
246
|
+
model.DiscriminationMap.Models.set(d.Key, d.Value);
|
|
247
|
+
});
|
|
248
|
+
}, true);
|
|
249
|
+
}
|
|
250
|
+
exports.DiscriminationMap = DiscriminationMap;
|
|
251
|
+
/**
|
|
252
|
+
* Marks relation as recursive. When relation is populated it loads all to the top
|
|
253
|
+
*
|
|
254
|
+
*/
|
|
255
|
+
function Recursive() {
|
|
256
|
+
return extractDecoratorDescriptor((model, _target, propertyKey) => {
|
|
257
|
+
if (!model.Relations.has(propertyKey)) {
|
|
258
|
+
throw new exceptions_1.InvalidOperation(`cannot set recursive on not existing relation ( relation ${propertyKey} on model ${model.Name} )`);
|
|
259
|
+
}
|
|
260
|
+
const relation = model.Relations.get(propertyKey);
|
|
261
|
+
relation.Recursive = true;
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
exports.Recursive = Recursive;
|
|
265
|
+
const forwardRef = (fn) => ({
|
|
266
|
+
forwardRef: fn,
|
|
267
|
+
});
|
|
268
|
+
exports.forwardRef = forwardRef;
|
|
269
|
+
/**
|
|
270
|
+
* Creates one to one relation with target model.
|
|
271
|
+
*
|
|
272
|
+
* @param foreignKey - foreign key name in db, defaults to lowercase property name with _id suffix eg. owner_id
|
|
273
|
+
* @param primaryKey - primary key in related model, defaults to primary key taken from db
|
|
274
|
+
*/
|
|
275
|
+
function BelongsTo(targetModel, foreignKey, primaryKey) {
|
|
276
|
+
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
277
|
+
model.Relations.set(propertyKey, {
|
|
278
|
+
Name: propertyKey,
|
|
279
|
+
Type: interfaces_js_1.RelationType.One,
|
|
280
|
+
SourceModel: target.constructor,
|
|
281
|
+
TargetModelType: targetModel,
|
|
282
|
+
TargetModel: null,
|
|
283
|
+
ForeignKey: foreignKey ?? `${propertyKey.toLowerCase()}_id`,
|
|
284
|
+
PrimaryKey: primaryKey ?? model.PrimaryKey,
|
|
285
|
+
Recursive: false,
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
exports.BelongsTo = BelongsTo;
|
|
290
|
+
/**
|
|
291
|
+
* Creates one to one relation with target model.
|
|
292
|
+
*
|
|
293
|
+
* @param foreignKey - foreign key name in db, defaults to lowercase property name with _id suffix eg. owner_id
|
|
294
|
+
* @param primaryKey - primary key in related model, defaults to primary key taken from db
|
|
295
|
+
*/
|
|
296
|
+
function ForwardBelongsTo(forwardRef, foreignKey, primaryKey) {
|
|
297
|
+
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
298
|
+
model.Relations.set(propertyKey, {
|
|
299
|
+
Name: propertyKey,
|
|
300
|
+
Type: interfaces_js_1.RelationType.One,
|
|
301
|
+
SourceModel: target.constructor,
|
|
302
|
+
TargetModelType: forwardRef.forwardRef,
|
|
303
|
+
TargetModel: null,
|
|
304
|
+
ForeignKey: foreignKey ?? `${propertyKey.toLowerCase()}_id`,
|
|
305
|
+
PrimaryKey: primaryKey ?? model.PrimaryKey,
|
|
306
|
+
Recursive: false,
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
exports.ForwardBelongsTo = ForwardBelongsTo;
|
|
311
|
+
/**
|
|
312
|
+
* Creates one to many relation with target model.
|
|
313
|
+
*
|
|
314
|
+
* @param targetModel - due to limitations of metadata reflection api in typescript target model mus be set explicitly
|
|
315
|
+
* @param foreignKey - foreign key name in db, defaults to lowercase property name with _id suffix eg. owner_id
|
|
316
|
+
* @param primaryKey - primary key in source table defaults to lowercase property name with _id suffix eg. owner_id
|
|
317
|
+
*
|
|
318
|
+
*/
|
|
319
|
+
function HasMany(targetModel, options) {
|
|
320
|
+
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
321
|
+
model.Relations.set(propertyKey, {
|
|
322
|
+
Name: propertyKey,
|
|
323
|
+
Type: interfaces_js_1.RelationType.Many,
|
|
324
|
+
SourceModel: target.constructor,
|
|
325
|
+
TargetModelType: targetModel,
|
|
326
|
+
TargetModel: null,
|
|
327
|
+
ForeignKey: options ? options.foreignKey ?? `${model.Name.toLowerCase()}_id` : `${model.Name.toLowerCase()}_id`,
|
|
328
|
+
PrimaryKey: options ? options.primaryKey ?? model.PrimaryKey : model.PrimaryKey,
|
|
329
|
+
Recursive: false,
|
|
330
|
+
Factory: options ? options.factory : null,
|
|
331
|
+
RelationClass: options ? options.type : null,
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
exports.HasMany = HasMany;
|
|
336
|
+
function Historical(targetModel) {
|
|
337
|
+
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
338
|
+
model.Relations.set(propertyKey, {
|
|
339
|
+
Name: propertyKey,
|
|
340
|
+
Type: interfaces_js_1.RelationType.Many,
|
|
341
|
+
SourceModel: target.constructor,
|
|
342
|
+
TargetModelType: targetModel,
|
|
343
|
+
TargetModel: null,
|
|
344
|
+
ForeignKey: model.PrimaryKey,
|
|
345
|
+
PrimaryKey: model.PrimaryKey,
|
|
346
|
+
Recursive: false,
|
|
347
|
+
});
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
exports.Historical = Historical;
|
|
351
|
+
/**
|
|
352
|
+
* Creates many to many relation with separate join table
|
|
353
|
+
*
|
|
354
|
+
* @param junctionModel - model for junction table
|
|
355
|
+
* @param targetModel - model for related data
|
|
356
|
+
* @param targetModelPKey - target model primary key name
|
|
357
|
+
* @param sourceModelPKey - source model primary key name
|
|
358
|
+
* @param junctionModelTargetPk - junction table target primary key name ( foreign key for target model )
|
|
359
|
+
* @param junctionModelSourcePk - junction table source primary key name ( foreign key for source model )
|
|
360
|
+
*/
|
|
361
|
+
function HasManyToMany(junctionModel, targetModel, targetModelPKey, sourceModelPKey, junctionModelTargetPk, junctionModelSourcePk) {
|
|
362
|
+
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
363
|
+
const targetModelDescriptor = (0, model_js_1.extractModelDescriptor)(targetModel);
|
|
364
|
+
model.Relations.set(propertyKey, {
|
|
365
|
+
Name: propertyKey,
|
|
366
|
+
Recursive: false,
|
|
367
|
+
Type: interfaces_js_1.RelationType.ManyToMany,
|
|
368
|
+
SourceModel: target.constructor,
|
|
369
|
+
TargetModelType: targetModel,
|
|
370
|
+
TargetModel: null,
|
|
371
|
+
ForeignKey: targetModelPKey ?? targetModelDescriptor.PrimaryKey,
|
|
372
|
+
PrimaryKey: sourceModelPKey ?? model.PrimaryKey,
|
|
373
|
+
JunctionModel: junctionModel,
|
|
374
|
+
JunctionModelTargetModelFKey_Name: junctionModelTargetPk ?? `${targetModelDescriptor.Name.toLowerCase()}_id`,
|
|
375
|
+
JunctionModelSourceModelFKey_Name: junctionModelSourcePk ?? `${model.Name.toLowerCase()}_id`,
|
|
376
|
+
});
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
exports.HasManyToMany = HasManyToMany;
|
|
380
|
+
/**
|
|
381
|
+
* Mark field as datetime type. It will ensure that conversion to & from DB is valid, eg. sqlite DB
|
|
382
|
+
* saves datetime as TEXT and ISO8601 strings
|
|
383
|
+
*/
|
|
384
|
+
function DateTime() {
|
|
385
|
+
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
386
|
+
const type = Reflect.getMetadata('design:type', target, propertyKey);
|
|
387
|
+
if (type.name !== 'DateTime') {
|
|
388
|
+
throw Error(`Proprety ${propertyKey} must be DateTime type`);
|
|
389
|
+
}
|
|
390
|
+
if (model.Converters.has(propertyKey)) {
|
|
391
|
+
throw new exceptions_1.InvalidArgument(`property ${propertyKey} already have data converter attached`);
|
|
392
|
+
}
|
|
393
|
+
model.Converters.set(propertyKey, {
|
|
394
|
+
Class: interfaces_js_1.DatetimeValueConverter,
|
|
395
|
+
});
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
exports.DateTime = DateTime;
|
|
399
|
+
/**
|
|
400
|
+
* Converts data in db to json object. Column type in DB should be STRING.
|
|
401
|
+
* DO not use this decorator for use of native DB JSON implementation.
|
|
402
|
+
* ORM will detect automatically if field is native JSON DB type.
|
|
403
|
+
*/
|
|
404
|
+
function Json() {
|
|
405
|
+
return extractDecoratorDescriptor((model, _, propertyKey) => {
|
|
406
|
+
// add converter for this field
|
|
407
|
+
model.Converters.set(propertyKey, {
|
|
408
|
+
Class: converters_js_1.JsonValueConverter,
|
|
409
|
+
});
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
exports.Json = Json;
|
|
413
|
+
/**
|
|
414
|
+
*
|
|
415
|
+
* Universal converter that guess whitch type to return. Usefull in tables that holds as text different values
|
|
416
|
+
* eg. metadata table
|
|
417
|
+
*
|
|
418
|
+
* @param typeColumn - type column that defines final type of value
|
|
419
|
+
*/
|
|
420
|
+
function UniversalConverter(typeColumn) {
|
|
421
|
+
return extractDecoratorDescriptor((model, _, propertyKey) => {
|
|
422
|
+
// add converter for this field
|
|
423
|
+
model.Converters.set(propertyKey, {
|
|
424
|
+
Class: converters_js_1.JsonValueConverter,
|
|
425
|
+
Options: {
|
|
426
|
+
TypeColumn: typeColumn,
|
|
427
|
+
},
|
|
428
|
+
});
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
exports.UniversalConverter = UniversalConverter;
|
|
432
|
+
/**
|
|
433
|
+
* Mark field as SET type. It will ensure that conversion to & from DB is valid, eg. to emulate field type SET in sqlite
|
|
434
|
+
*/
|
|
435
|
+
function Set() {
|
|
436
|
+
return extractDecoratorDescriptor((model, target, propertyKey) => {
|
|
437
|
+
const type = Reflect.getMetadata('design:type', target, propertyKey);
|
|
438
|
+
if (type.name !== 'Array') {
|
|
439
|
+
throw Error(`Proprety ${propertyKey} must be an array type`);
|
|
440
|
+
}
|
|
441
|
+
if (model.Converters.has(propertyKey)) {
|
|
442
|
+
throw new exceptions_1.InvalidArgument(`property ${propertyKey} already have data converter attached`);
|
|
443
|
+
}
|
|
444
|
+
model.Converters.set(propertyKey, {
|
|
445
|
+
Class: interfaces_js_1.SetValueConverter,
|
|
446
|
+
});
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
exports.Set = Set;
|
|
450
450
|
//# sourceMappingURL=decorators.js.map
|