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