@rws-framework/db 2.4.5 → 3.0.0
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/.bin/add-v.sh +9 -9
- package/.bin/emerge.sh +10 -10
- package/.eslintrc.json +53 -53
- package/README.md +404 -404
- package/dist/decorators/IdType.d.ts +8 -0
- package/dist/decorators/IdType.js +10 -0
- package/dist/decorators/InverseRelation.d.ts +2 -2
- package/dist/decorators/InverseRelation.js +5 -1
- package/dist/decorators/InverseTimeSeries.d.ts +0 -0
- package/dist/decorators/InverseTimeSeries.js +0 -0
- package/dist/decorators/RWSCollection.d.ts +7 -0
- package/dist/decorators/RWSCollection.js +6 -0
- package/dist/decorators/Relation.d.ts +8 -6
- package/dist/decorators/Relation.js +8 -1
- package/dist/decorators/TrackType.d.ts +2 -14
- package/dist/decorators/TrackType.js +7 -0
- package/dist/decorators/TypeFunctions.d.ts +44 -0
- package/dist/decorators/TypeFunctions.js +174 -0
- package/dist/decorators/index.d.ts +4 -2
- package/dist/decorators/index.js +3 -1
- package/dist/helper/DbHelper.d.ts +76 -5
- package/dist/helper/DbHelper.js +93 -154
- package/dist/helper/FieldsHelper.d.ts +0 -0
- package/dist/helper/FieldsHelper.js +0 -0
- package/dist/helper/db/index.d.ts +9 -0
- package/dist/helper/db/index.js +18 -0
- package/dist/helper/db/relation-manager.d.ts +45 -0
- package/dist/helper/db/relation-manager.js +105 -0
- package/dist/helper/db/schema-generator.d.ts +37 -0
- package/dist/helper/db/schema-generator.js +243 -0
- package/dist/helper/db/type-converter.d.ts +22 -0
- package/dist/helper/db/type-converter.js +106 -0
- package/dist/helper/db/utils.d.ts +24 -0
- package/dist/helper/db/utils.js +99 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -1
- package/dist/models/TimeSeriesModel.d.ts +7 -7
- package/dist/models/TimeSeriesModel.js +33 -33
- package/dist/models/_model.d.ts +2 -2
- package/dist/models/_model.js +0 -0
- package/dist/models/core/RWSModel.d.ts +4 -1
- package/dist/models/core/RWSModel.js +6 -4
- package/dist/models/core/TimeSeriesModel.d.ts +0 -0
- package/dist/models/core/TimeSeriesModel.js +0 -0
- package/dist/models/index.d.ts +2 -2
- package/dist/models/index.js +0 -0
- package/dist/models/interfaces/IDbOpts.d.ts +17 -0
- package/dist/models/interfaces/IDbOpts.js +2 -0
- package/dist/models/interfaces/IIdOpts.d.ts +0 -0
- package/dist/models/interfaces/IIdOpts.js +1 -0
- package/dist/models/interfaces/IIdTypeOpts.d.ts +3 -0
- package/dist/models/interfaces/IIdTypeOpts.js +2 -0
- package/dist/models/interfaces/IModel.d.ts +0 -0
- package/dist/models/interfaces/IModel.js +0 -0
- package/dist/models/interfaces/IRWSModelServices.d.ts +0 -0
- package/dist/models/interfaces/IRWSModelServices.js +0 -0
- package/dist/models/interfaces/ITrackerOpts.d.ts +12 -0
- package/dist/models/interfaces/ITrackerOpts.js +2 -0
- package/dist/models/interfaces/OpModelType.d.ts +3 -0
- package/dist/models/interfaces/OpModelType.js +0 -0
- package/dist/models/types/RelationTypes.d.ts +0 -0
- package/dist/models/types/RelationTypes.js +0 -0
- package/dist/models/utils/ModelUtils.d.ts +0 -0
- package/dist/models/utils/ModelUtils.js +0 -0
- package/dist/models/utils/PaginationUtils.d.ts +0 -0
- package/dist/models/utils/PaginationUtils.js +0 -0
- package/dist/models/utils/RelationUtils.d.ts +1 -1
- package/dist/models/utils/RelationUtils.js +2 -2
- package/dist/models/utils/TimeSeriesUtils.d.ts +0 -0
- package/dist/models/utils/TimeSeriesUtils.js +0 -0
- package/dist/services/DBService.d.ts +0 -0
- package/dist/services/DBService.js +0 -0
- package/dist/types/DbConfigHandler.d.ts +1 -1
- package/dist/types/DbConfigHandler.js +0 -0
- package/dist/types/FindParams.d.ts +0 -0
- package/dist/types/FindParams.js +0 -0
- package/dist/types/IRWSModel.d.ts +1 -1
- package/dist/types/IRWSModel.js +0 -0
- package/dist/types/ITimeSeries.d.ts +0 -0
- package/dist/types/ITimeSeries.js +0 -0
- package/exec/console.js +110 -110
- package/exec/db.rws.webpack.config.js +168 -168
- package/exec/src/cli.ts +73 -75
- package/exec/tsconfig.json +32 -32
- package/exec/webpackFilters.js +17 -17
- package/package.json +36 -36
- package/src/decorators/IdType.ts +17 -0
- package/src/decorators/InverseRelation.ts +41 -37
- package/src/decorators/InverseTimeSeries.ts +21 -21
- package/src/decorators/RWSCollection.ts +45 -27
- package/src/decorators/Relation.ts +61 -48
- package/src/decorators/TrackType.ts +65 -69
- package/src/decorators/index.ts +16 -8
- package/src/empty.js +0 -0
- package/src/helper/DbHelper.ts +133 -223
- package/src/helper/FieldsHelper.ts +34 -34
- package/src/helper/db/index.ts +10 -0
- package/src/helper/db/relation-manager.ts +119 -0
- package/src/helper/db/schema-generator.ts +302 -0
- package/src/helper/db/type-converter.ts +119 -0
- package/src/helper/db/utils.ts +120 -0
- package/src/index.ts +47 -38
- package/src/models/_model.ts +29 -29
- package/src/models/core/RWSModel.ts +523 -520
- package/src/models/core/TimeSeriesModel.ts +19 -19
- package/src/models/index.ts +20 -20
- package/src/models/interfaces/IDbOpts.ts +17 -0
- package/src/models/interfaces/IIdTypeOpts.ts +4 -0
- package/src/models/interfaces/IModel.ts +12 -12
- package/src/models/interfaces/IRWSModelServices.ts +7 -7
- package/src/models/interfaces/ITrackerOpts.ts +13 -0
- package/src/models/interfaces/OpModelType.ts +52 -49
- package/src/models/types/RelationTypes.ts +25 -25
- package/src/models/utils/ModelUtils.ts +65 -65
- package/src/models/utils/PaginationUtils.ts +42 -42
- package/src/models/utils/RelationUtils.ts +76 -76
- package/src/models/utils/TimeSeriesUtils.ts +38 -38
- package/src/services/DBService.ts +277 -277
- package/src/types/DbConfigHandler.ts +17 -17
- package/src/types/FindParams.ts +13 -13
- package/src/types/IRWSModel.ts +2 -2
- package/src/types/ITimeSeries.ts +5 -5
- package/tsconfig.json +22 -22
|
@@ -1,520 +1,523 @@
|
|
|
1
|
-
import { IModel } from '../interfaces/IModel';
|
|
2
|
-
import { IRWSModelServices } from '../interfaces/IRWSModelServices';
|
|
3
|
-
import { OpModelType } from '../interfaces/OpModelType';
|
|
4
|
-
import { TrackType,
|
|
5
|
-
import { FieldsHelper } from '../../helper/FieldsHelper';
|
|
6
|
-
import { FindByType, IPaginationParams } from '../../types/FindParams';
|
|
7
|
-
import { RelationUtils } from '../utils/RelationUtils';
|
|
8
|
-
|
|
9
|
-
import { TimeSeriesUtils } from '../utils/TimeSeriesUtils';
|
|
10
|
-
import { ModelUtils } from '../utils/ModelUtils';
|
|
11
|
-
// import timeSeriesModel from './TimeSeriesModel';
|
|
12
|
-
import { DBService } from '../../services/DBService';
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
static
|
|
22
|
-
static
|
|
23
|
-
static
|
|
24
|
-
static
|
|
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
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const
|
|
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
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
const
|
|
229
|
-
const
|
|
230
|
-
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
!
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
this.
|
|
307
|
-
|
|
308
|
-
this.
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
const
|
|
392
|
-
const
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
const
|
|
418
|
-
|
|
419
|
-
const
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
const
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
const
|
|
440
|
-
const
|
|
441
|
-
|
|
442
|
-
const
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
1
|
+
import { IModel } from '../interfaces/IModel';
|
|
2
|
+
import { IRWSModelServices } from '../interfaces/IRWSModelServices';
|
|
3
|
+
import { OpModelType } from '../interfaces/OpModelType';
|
|
4
|
+
import { TrackType, ITrackerMetaOpts } from '../../decorators';
|
|
5
|
+
import { FieldsHelper } from '../../helper/FieldsHelper';
|
|
6
|
+
import { FindByType, IPaginationParams } from '../../types/FindParams';
|
|
7
|
+
import { RelationUtils } from '../utils/RelationUtils';
|
|
8
|
+
|
|
9
|
+
import { TimeSeriesUtils } from '../utils/TimeSeriesUtils';
|
|
10
|
+
import { ModelUtils } from '../utils/ModelUtils';
|
|
11
|
+
// import timeSeriesModel from './TimeSeriesModel';
|
|
12
|
+
import { DBService } from '../../services/DBService';
|
|
13
|
+
import { ISuperTagData } from '../../decorators/RWSCollection';
|
|
14
|
+
|
|
15
|
+
class RWSModel<T> implements IModel {
|
|
16
|
+
static services: IRWSModelServices = {};
|
|
17
|
+
|
|
18
|
+
[key: string]: any;
|
|
19
|
+
@TrackType(String)
|
|
20
|
+
id: string | number;
|
|
21
|
+
static _collection: string = null;
|
|
22
|
+
static _RELATIONS = {};
|
|
23
|
+
static _NO_ID = false;
|
|
24
|
+
static _SUPER_TAGS: ISuperTagData[] = [];
|
|
25
|
+
static _BANNED_KEYS = ['_collection'];
|
|
26
|
+
static allModels: OpModelType<any>[] = [];
|
|
27
|
+
static _CUT_KEYS: string[] = [];
|
|
28
|
+
|
|
29
|
+
constructor(data: any = null) {
|
|
30
|
+
if(!this.getCollection()){
|
|
31
|
+
throw new Error('Model must have a collection defined');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
this.dbService = RWSModel.services.dbService;
|
|
35
|
+
this.configService = RWSModel.services.configService;
|
|
36
|
+
|
|
37
|
+
if(!data){
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if(!this.hasTimeSeries()){
|
|
42
|
+
this._fill(data);
|
|
43
|
+
}else{
|
|
44
|
+
throw new Error('Time Series not supported in synchronous constructor. Use `await Model.create(data)` static method to instantiate this model.');
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
checkForInclusionWithThrow(): void {
|
|
49
|
+
const constructor = this.constructor as OpModelType<any>;
|
|
50
|
+
if(!constructor.checkForInclusion(constructor.name)){
|
|
51
|
+
throw new Error('Model undefined: ' + constructor.name);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static checkForInclusionWithThrow(this: OpModelType<any>, checkModelType: string): void {
|
|
56
|
+
if(!this.checkForInclusion(this.name)){
|
|
57
|
+
throw new Error('Model undefined: ' + this.name);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
checkForInclusion(): boolean {
|
|
62
|
+
const constructor = this.constructor as OpModelType<any>;
|
|
63
|
+
return constructor.checkForInclusion(constructor.name);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
static checkForInclusion(this: OpModelType<any>, checkModelType: string): boolean {
|
|
67
|
+
return this.loadModels().find((definedModel: OpModelType<any>) => {
|
|
68
|
+
return definedModel.name === checkModelType;
|
|
69
|
+
}) !== undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
protected _fill(data: any): RWSModel<T> {
|
|
73
|
+
for (const key in data) {
|
|
74
|
+
if (data.hasOwnProperty(key)) {
|
|
75
|
+
|
|
76
|
+
const meta = Reflect.getMetadata(`InverseTimeSeries:${key}`, (this as any).constructor.prototype);
|
|
77
|
+
|
|
78
|
+
if(meta){
|
|
79
|
+
data[key] = {
|
|
80
|
+
create: data[key]
|
|
81
|
+
};
|
|
82
|
+
}else{
|
|
83
|
+
this[key] = data[key];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
protected hasRelation(key: string): boolean {
|
|
92
|
+
return RelationUtils.hasRelation(this, key);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
protected bindRelation(key: string, relatedModel: RWSModel<any>): { connect: { id: string | number } } {
|
|
96
|
+
return RelationUtils.bindRelation(relatedModel);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public async _asyncFill(data: any, fullDataMode = false, allowRelations = true): Promise<T> {
|
|
100
|
+
const collections_to_models: {[key: string]: any} = {};
|
|
101
|
+
const timeSeriesIds = TimeSeriesUtils.getTimeSeriesModelFields(this);
|
|
102
|
+
|
|
103
|
+
const classFields = FieldsHelper.getAllClassFields(this.constructor);
|
|
104
|
+
|
|
105
|
+
// Get both relation metadata types asynchronously
|
|
106
|
+
const [relOneData, relManyData] = await Promise.all([
|
|
107
|
+
this.getRelationOneMeta(classFields),
|
|
108
|
+
this.getRelationManyMeta(classFields)
|
|
109
|
+
]);
|
|
110
|
+
|
|
111
|
+
this.loadModels().forEach((model) => {
|
|
112
|
+
collections_to_models[model.getCollection()] = model;
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const seriesHydrationfields: string[] = [];
|
|
116
|
+
|
|
117
|
+
if (allowRelations) {
|
|
118
|
+
// Handle many-to-many relations
|
|
119
|
+
for (const key in relManyData) {
|
|
120
|
+
if(!fullDataMode && (this as any).constructor._CUT_KEYS.includes(key)){
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const relMeta = relManyData[key];
|
|
125
|
+
|
|
126
|
+
const relationEnabled = !RelationUtils.checkRelDisabled(this, relMeta.key);
|
|
127
|
+
if (relationEnabled) {
|
|
128
|
+
this[relMeta.key] = await relMeta.inversionModel.findBy({
|
|
129
|
+
conditions: {
|
|
130
|
+
[relMeta.foreignKey]: data.id
|
|
131
|
+
},
|
|
132
|
+
allowRelations: false
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Handle one-to-one relations
|
|
138
|
+
for (const key in relOneData) {
|
|
139
|
+
if(!fullDataMode && (this as any).constructor._CUT_KEYS.includes(key)){
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const relMeta = relOneData[key];
|
|
144
|
+
const relationEnabled = !RelationUtils.checkRelDisabled(this, relMeta.key);
|
|
145
|
+
|
|
146
|
+
if(!data[relMeta.hydrationField] && relMeta.required){
|
|
147
|
+
throw new Error(`Relation field "${relMeta.hydrationField}" is required in model ${this.constructor.name}.`)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (relationEnabled && data[relMeta.hydrationField]) {
|
|
151
|
+
this[relMeta.key] = await relMeta.model.find(data[relMeta.hydrationField], { allowRelations: false });
|
|
152
|
+
}
|
|
153
|
+
else if(relationEnabled && !data[relMeta.hydrationField] && data[relMeta.key]){
|
|
154
|
+
const newRelModel: RWSModel<any> = await relMeta.model.create(data[relMeta.key]);
|
|
155
|
+
this[relMeta.key] = await newRelModel.save();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const cutKeys = ((this.constructor as any)._CUT_KEYS as string[]);
|
|
159
|
+
|
|
160
|
+
if(!cutKeys.includes(relMeta.hydrationField)){
|
|
161
|
+
cutKeys.push(relMeta.hydrationField)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Process regular fields and time series
|
|
167
|
+
for (const key in data) {
|
|
168
|
+
if (data.hasOwnProperty(key)) {
|
|
169
|
+
if(!fullDataMode && (this as any).constructor._CUT_KEYS.includes(key)){
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (Object.keys(relOneData).includes(key)) {
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (seriesHydrationfields.includes(key)) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const timeSeriesMetaData = timeSeriesIds[key];
|
|
182
|
+
|
|
183
|
+
if (timeSeriesMetaData) {
|
|
184
|
+
this[key] = data[key];
|
|
185
|
+
const seriesModel = collections_to_models[timeSeriesMetaData.collection];
|
|
186
|
+
|
|
187
|
+
const dataModels = await seriesModel.findBy({
|
|
188
|
+
id: { in: data[key] }
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
seriesHydrationfields.push(timeSeriesMetaData.hydrationField);
|
|
192
|
+
|
|
193
|
+
this[timeSeriesMetaData.hydrationField] = dataModels;
|
|
194
|
+
} else {
|
|
195
|
+
this[key] = data[key];
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return this as any as T;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private getModelScalarFields(model: RWSModel<T>): string[] {
|
|
204
|
+
return ModelUtils.getModelScalarFields(model);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private async getRelationOneMeta(classFields: string[]) {
|
|
208
|
+
return RelationUtils.getRelationOneMeta(this, classFields);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
static async getRelationOneMeta(model: any, classFields: string[]) {
|
|
212
|
+
return RelationUtils.getRelationOneMeta(model, classFields);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private async getRelationManyMeta(classFields: string[]) {
|
|
216
|
+
return RelationUtils.getRelationManyMeta(this, classFields);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
static async getRelationManyMeta(model: any, classFields: string[]) {
|
|
220
|
+
return RelationUtils.getRelationManyMeta(model, classFields);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
public static async paginate<T extends RWSModel<T>>(
|
|
224
|
+
this: OpModelType<T>,
|
|
225
|
+
paginateParams: IPaginationParams,
|
|
226
|
+
findParams?: FindByType
|
|
227
|
+
): Promise<T[]> {
|
|
228
|
+
const conditions = findParams?.conditions ?? {};
|
|
229
|
+
const ordering = findParams?.ordering ?? null;
|
|
230
|
+
const fields = findParams?.fields ?? null;
|
|
231
|
+
const allowRelations = findParams?.allowRelations ?? true;
|
|
232
|
+
const fullData = findParams?.fullData ?? false;
|
|
233
|
+
|
|
234
|
+
const collection = Reflect.get(this, '_collection');
|
|
235
|
+
this.checkForInclusionWithThrow(this.name);
|
|
236
|
+
try {
|
|
237
|
+
const dbData = await this.services.dbService.findBy(collection, conditions, fields, ordering, paginateParams);
|
|
238
|
+
if (dbData.length) {
|
|
239
|
+
const instanced: T[] = [];
|
|
240
|
+
|
|
241
|
+
for (const data of dbData) {
|
|
242
|
+
const inst: T = new (this as { new(): T })();
|
|
243
|
+
instanced.push((await inst._asyncFill(data, fullData,allowRelations)) as T);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return instanced;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return [];
|
|
250
|
+
} catch (rwsError: Error | any) {
|
|
251
|
+
console.error(rwsError);
|
|
252
|
+
|
|
253
|
+
throw rwsError;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
public async toMongo(): Promise<any> {
|
|
258
|
+
const data: any = {};
|
|
259
|
+
const timeSeriesIds = TimeSeriesUtils.getTimeSeriesModelFields(this);
|
|
260
|
+
const timeSeriesHydrationFields: string[] = [];
|
|
261
|
+
|
|
262
|
+
for (const key in (this as any)) {
|
|
263
|
+
if (this.hasRelation(key)) {
|
|
264
|
+
data[key] = this.bindRelation(key, this[key]);
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (!(await this.isDbVariable(key))) {
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const passedFieldCondition: boolean = this.hasOwnProperty(key) &&
|
|
273
|
+
!((this as any).constructor._BANNED_KEYS
|
|
274
|
+
|| RWSModel._BANNED_KEYS
|
|
275
|
+
).includes(key) &&
|
|
276
|
+
!timeSeriesHydrationFields.includes(key)
|
|
277
|
+
;
|
|
278
|
+
|
|
279
|
+
if (passedFieldCondition) {
|
|
280
|
+
data[key] = this[key];
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (timeSeriesIds[key]) {
|
|
284
|
+
data[key] = this[key];
|
|
285
|
+
timeSeriesHydrationFields.push(timeSeriesIds[key].hydrationField);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return data;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
getCollection(): string | null {
|
|
293
|
+
return (this as any).constructor._collection || this._collection;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
static getCollection(): string | null {
|
|
297
|
+
return (this as any).constructor._collection || this._collection;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
async save(): Promise<this> {
|
|
301
|
+
const data = await this.toMongo();
|
|
302
|
+
let updatedModelData = data;
|
|
303
|
+
if (this.id) {
|
|
304
|
+
this.preUpdate();
|
|
305
|
+
|
|
306
|
+
updatedModelData = await this.dbService.update(data, this.getCollection());
|
|
307
|
+
|
|
308
|
+
await this._asyncFill(updatedModelData);
|
|
309
|
+
this.postUpdate();
|
|
310
|
+
} else {
|
|
311
|
+
this.preCreate();
|
|
312
|
+
|
|
313
|
+
const isTimeSeries = false;//this instanceof timeSeriesModel;
|
|
314
|
+
|
|
315
|
+
updatedModelData = await this.dbService.insert(data, this.getCollection(), isTimeSeries);
|
|
316
|
+
|
|
317
|
+
await this._asyncFill(updatedModelData);
|
|
318
|
+
|
|
319
|
+
this.postCreate();
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
return this;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
static async getModelAnnotations<T extends unknown>(constructor: new () => T): Promise<Record<string, {annotationType: string, metadata: any}>> {
|
|
326
|
+
return ModelUtils.getModelAnnotations(constructor);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
public preUpdate(): void {
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
public postUpdate(): void {
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
public preCreate(): void {
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
public postCreate(): void {
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
public static isSubclass<T extends RWSModel<T>, C extends new () => T>(constructor: C, baseClass: new () => T): boolean {
|
|
346
|
+
return ModelUtils.isSubclass(constructor, baseClass);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
hasTimeSeries(): boolean {
|
|
350
|
+
return TimeSeriesUtils.checkTimeSeries((this as any).constructor);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
static checkTimeSeries(constructor: any): boolean {
|
|
354
|
+
return TimeSeriesUtils.checkTimeSeries(constructor);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
async isDbVariable(variable: string): Promise<boolean> {
|
|
358
|
+
return ModelUtils.checkDbVariable((this as any).constructor, variable);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
static async checkDbVariable(constructor: any, variable: string): Promise<boolean> {
|
|
362
|
+
return ModelUtils.checkDbVariable(constructor, variable);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
sanitizeDBData(data: any): any {
|
|
366
|
+
const dataKeys = Object.keys(data);
|
|
367
|
+
const sanitizedData: {[key: string]: any} = {};
|
|
368
|
+
|
|
369
|
+
for (const key of dataKeys){
|
|
370
|
+
if(this.isDbVariable(key)){
|
|
371
|
+
sanitizedData[key] = data[key];
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
return sanitizedData;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
public static async watchCollection<T extends RWSModel<T>>(
|
|
379
|
+
this: OpModelType<T>,
|
|
380
|
+
preRun: () => void
|
|
381
|
+
){
|
|
382
|
+
const collection = Reflect.get(this, '_collection');
|
|
383
|
+
this.checkForInclusionWithThrow(this.name);
|
|
384
|
+
return await this.services.dbService.watchCollection(collection, preRun);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
public static async findOneBy<T extends RWSModel<T>>(
|
|
388
|
+
this: OpModelType<T>,
|
|
389
|
+
findParams?: FindByType
|
|
390
|
+
): Promise<T | null> {
|
|
391
|
+
const conditions = findParams?.conditions ?? {};
|
|
392
|
+
const ordering = findParams?.ordering ?? null;
|
|
393
|
+
const fields = findParams?.fields ?? null;
|
|
394
|
+
const allowRelations = findParams?.allowRelations ?? true;
|
|
395
|
+
const fullData = findParams?.fullData ?? false;
|
|
396
|
+
|
|
397
|
+
this.checkForInclusionWithThrow('');
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
const collection = Reflect.get(this, '_collection');
|
|
401
|
+
const dbData = await this.services.dbService.findOneBy(collection, conditions, fields, ordering, allowRelations);
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
if (dbData) {
|
|
405
|
+
const inst: T = new (this as { new(): T })();
|
|
406
|
+
return await inst._asyncFill(dbData, fullData, allowRelations);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
return null;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
public static async find<T extends RWSModel<T>>(
|
|
413
|
+
this: OpModelType<T>,
|
|
414
|
+
id: string,
|
|
415
|
+
findParams: Omit<FindByType, 'conditions'> = null
|
|
416
|
+
): Promise<T | null> {
|
|
417
|
+
const ordering = findParams?.ordering ?? null;
|
|
418
|
+
const fields = findParams?.fields ?? null;
|
|
419
|
+
const allowRelations = findParams?.allowRelations ?? true;
|
|
420
|
+
const fullData = findParams?.fullData ?? false;
|
|
421
|
+
|
|
422
|
+
const collection = Reflect.get(this, '_collection');
|
|
423
|
+
this.checkForInclusionWithThrow(this.name);
|
|
424
|
+
|
|
425
|
+
const dbData = await this.services.dbService.findOneBy(collection, { id }, fields, ordering, allowRelations);
|
|
426
|
+
|
|
427
|
+
if (dbData) {
|
|
428
|
+
const inst: T = new (this as { new(): T })();
|
|
429
|
+
return await inst._asyncFill(dbData, fullData, allowRelations);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
return null;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
public static async findBy<T extends RWSModel<T>>(
|
|
436
|
+
this: OpModelType<T>,
|
|
437
|
+
findParams?: FindByType
|
|
438
|
+
): Promise<T[]> {
|
|
439
|
+
const conditions = findParams?.conditions ?? {};
|
|
440
|
+
const ordering = findParams?.ordering ?? null;
|
|
441
|
+
const fields = findParams?.fields ?? null;
|
|
442
|
+
const allowRelations = findParams?.allowRelations ?? true;
|
|
443
|
+
const fullData = findParams?.fullData ?? false;
|
|
444
|
+
|
|
445
|
+
const collection = Reflect.get(this, '_collection');
|
|
446
|
+
this.checkForInclusionWithThrow(this.name);
|
|
447
|
+
try {
|
|
448
|
+
const dbData = await this.services.dbService.findBy(collection, conditions, fields, ordering);
|
|
449
|
+
if (dbData.length) {
|
|
450
|
+
const instanced: T[] = [];
|
|
451
|
+
|
|
452
|
+
for (const data of dbData) {
|
|
453
|
+
const inst: T = new (this as { new(): T })();
|
|
454
|
+
instanced.push((await inst._asyncFill(data, fullData,allowRelations)) as T);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
return instanced;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
return [];
|
|
461
|
+
} catch (rwsError: Error | any) {
|
|
462
|
+
console.error(rwsError);
|
|
463
|
+
|
|
464
|
+
throw rwsError;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
public static async delete<T extends RWSModel<T>>(
|
|
469
|
+
this: OpModelType<T>,
|
|
470
|
+
conditions: any
|
|
471
|
+
): Promise<void> {
|
|
472
|
+
const collection = Reflect.get(this, '_collection');
|
|
473
|
+
this.checkForInclusionWithThrow(this.name);
|
|
474
|
+
return await this.services.dbService.delete(collection, conditions);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
public async delete<T extends RWSModel<T>>(): Promise<void> {
|
|
478
|
+
const collection = Reflect.get(this, '_collection');
|
|
479
|
+
this.checkForInclusionWithThrow();
|
|
480
|
+
return await this.dbService.delete(collection, {
|
|
481
|
+
id: this.id
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
static async create<T extends RWSModel<T>>(this: new () => T, data: any): Promise<T> {
|
|
486
|
+
const newModel = new this();
|
|
487
|
+
|
|
488
|
+
const sanitizedData = newModel.sanitizeDBData(data);
|
|
489
|
+
|
|
490
|
+
await newModel._asyncFill(sanitizedData);
|
|
491
|
+
|
|
492
|
+
return newModel;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
static loadModels(): OpModelType<any>[] {
|
|
496
|
+
return this.allModels || [];
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
loadModels(): OpModelType<any>[] {
|
|
500
|
+
return RWSModel.loadModels();
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
private checkRelDisabled(key: string): boolean {
|
|
504
|
+
return RelationUtils.checkRelDisabled(this, key);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
public static setServices(services: IRWSModelServices){
|
|
508
|
+
this.allModels = services.configService.get('db_models');
|
|
509
|
+
this.services = services;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
public getDb(): DBService
|
|
513
|
+
{
|
|
514
|
+
return this.services.dbService;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
public static getDb(): DBService
|
|
518
|
+
{
|
|
519
|
+
return this.services.dbService;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
export { RWSModel };
|