@rws-framework/db 2.1.10 → 2.2.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 (48) hide show
  1. package/dist/decorators/TrackType.d.ts +1 -1
  2. package/dist/models/_model.d.ts +5 -116
  3. package/dist/models/_model.js +7 -499
  4. package/dist/models/core/RWSModel.d.ts +65 -0
  5. package/dist/models/core/RWSModel.js +375 -0
  6. package/dist/models/core/TimeSeriesModel.d.ts +7 -0
  7. package/dist/models/core/TimeSeriesModel.js +34 -0
  8. package/dist/models/index.d.ts +8 -0
  9. package/dist/models/index.js +12 -0
  10. package/dist/models/interfaces/IModel.d.ts +10 -0
  11. package/dist/models/interfaces/IModel.js +2 -0
  12. package/dist/models/interfaces/IRWSModelServices.d.ts +6 -0
  13. package/dist/models/interfaces/IRWSModelServices.js +2 -0
  14. package/dist/models/interfaces/OpModelType.d.ts +32 -0
  15. package/dist/models/interfaces/OpModelType.js +2 -0
  16. package/dist/models/types/RelationTypes.d.ts +24 -0
  17. package/dist/models/types/RelationTypes.js +2 -0
  18. package/dist/models/utils/ModelUtils.d.ts +10 -0
  19. package/dist/models/utils/ModelUtils.js +56 -0
  20. package/dist/models/utils/PaginationUtils.d.ts +5 -0
  21. package/dist/models/utils/PaginationUtils.js +32 -0
  22. package/dist/models/utils/RelationUtils copy.d.ts +13 -0
  23. package/dist/models/utils/RelationUtils copy.js +65 -0
  24. package/dist/models/utils/RelationUtils.d.ts +14 -0
  25. package/dist/models/utils/RelationUtils.js +65 -0
  26. package/dist/models/utils/TimeSeriesUtils.d.ts +11 -0
  27. package/dist/models/utils/TimeSeriesUtils.js +35 -0
  28. package/dist/services/DBService.d.ts +3 -2
  29. package/dist/services/DBService.js +5 -1
  30. package/dist/types/DbConfigHandler.d.ts +1 -1
  31. package/dist/types/FindParams.d.ts +5 -0
  32. package/package.json +1 -1
  33. package/src/decorators/TrackType.ts +2 -2
  34. package/src/models/_model.ts +26 -677
  35. package/src/models/core/RWSModel.ts +482 -0
  36. package/src/models/core/TimeSeriesModel.ts +19 -0
  37. package/src/models/index.ts +20 -0
  38. package/src/models/interfaces/IModel.ts +11 -0
  39. package/src/models/interfaces/IRWSModelServices.ts +7 -0
  40. package/src/models/interfaces/OpModelType.ts +49 -0
  41. package/src/models/types/RelationTypes.ts +25 -0
  42. package/src/models/utils/ModelUtils.ts +65 -0
  43. package/src/models/utils/PaginationUtils.ts +42 -0
  44. package/src/models/utils/RelationUtils.ts +76 -0
  45. package/src/models/utils/TimeSeriesUtils.ts +38 -0
  46. package/src/services/DBService.ts +14 -3
  47. package/src/types/DbConfigHandler.ts +2 -2
  48. package/src/types/FindParams.ts +10 -4
@@ -1,678 +1,27 @@
1
- import {DBService, IDBClientCreate} from '../services/DBService';
2
- import { IRWSModel } from '../types/IRWSModel';
3
- import { IDbConfigHandler } from '../types/DbConfigHandler';
4
-
5
- import {TrackType, IMetaOpts } from '../decorators';
6
- import { FieldsHelper } from '../helper/FieldsHelper';
7
- import { FindByType } from '../types/FindParams';
8
-
9
- interface IModel{
10
- [key: string]: any;
11
- id: string | null;
12
- save: () => void;
13
- getCollection: () => string | null;
14
- configService?: IDbConfigHandler;
15
- dbService?: DBService;
16
- }
17
-
18
- interface IRWSModelServices {
19
- configService?: IDbConfigHandler
20
- dbService?: DBService
21
- }
22
-
23
- type RelationBindType = {
24
- connect: { id: string }
1
+ /**
2
+ * @deprecated This file is deprecated. Import from 'models' directory instead.
3
+ */
4
+
5
+ import {
6
+ IModel,
7
+ IRWSModelServices,
8
+ OpModelType,
9
+ RelationBindType,
10
+ RelOneMetaType,
11
+ RelManyMetaType,
12
+ RWSModel,
13
+ TrackType,
14
+ IMetaOpts
15
+ } from './index';
16
+
17
+ export {
18
+ IModel,
19
+ IRWSModelServices,
20
+ OpModelType,
21
+ RelationBindType,
22
+ RelOneMetaType,
23
+ RelManyMetaType,
24
+ RWSModel,
25
+ TrackType,
26
+ IMetaOpts
25
27
  };
26
-
27
- type RelOneMetaType<T extends IRWSModel> = {[key: string]: {required: boolean, key?: string, model: OpModelType<T>, hydrationField: string, foreignKey: string}};
28
- type RelManyMetaType<T extends IRWSModel> = {[key: string]: {key: string, inversionModel: OpModelType<T>, foreignKey: string}};
29
-
30
- export interface OpModelType<ChildClass> {
31
- new(data?: any | null): ChildClass;
32
- services: IRWSModelServices;
33
- name: string
34
- _collection: string;
35
- _RELATIONS: {[key: string]: boolean}
36
- _CUT_KEYS: string[]
37
- allModels: OpModelType<any>[];
38
- loadModels: () => OpModelType<any>[];
39
- checkForInclusionWithThrow: (className: string) => void;
40
- checkForInclusion: (className: string) => boolean;
41
- findOneBy<T extends RWSModel<T>>(
42
- this: OpModelType<T>,
43
- findParams: FindByType
44
- ): Promise<T | null>;
45
- find<T extends RWSModel<T>>(
46
- this: OpModelType<T>,
47
- id: string,
48
- findParams?: Omit<FindByType, 'conditions'>
49
- ): Promise<T | null>;
50
- findBy<T extends RWSModel<T>>(
51
- this: OpModelType<T>,
52
- findParams: FindByType
53
- ): Promise<T[]>;
54
- delete<ChildClass extends RWSModel<ChildClass>>(
55
- this: OpModelType<ChildClass>,
56
- conditions: any
57
- ): Promise<void>
58
- create<T extends RWSModel<T>>(this: OpModelType<T>, data: T): Promise<T>;
59
- getRelationOneMeta(model: any, classFields: string[]): Promise<RelOneMetaType<IRWSModel>>;
60
- getRelationManyMeta(model: any, classFields: string[]): Promise<RelManyMetaType<IRWSModel>>;
61
- getCollection(): string;
62
- setServices(services: IRWSModelServices): void;
63
- }
64
-
65
- class RWSModel<ChildClass> implements IModel{
66
- static services: IRWSModelServices = {};
67
-
68
- [key: string]: any;
69
- @TrackType(String)
70
- id: string;
71
- static _collection: string = null;
72
- static _RELATIONS = {};
73
- static _BANNED_KEYS = ['_collection'];
74
- static allModels: OpModelType<any>[] = [];
75
- static _CUT_KEYS: string[] = [];
76
-
77
- constructor(data: any) {
78
- if(!this.getCollection()){
79
- throw new Error('Model must have a collection defined');
80
-
81
- }
82
-
83
- this.dbService = RWSModel.services.dbService;
84
- this.configService = RWSModel.services.configService;
85
-
86
- if(!data){
87
- return;
88
- }
89
-
90
- if(!this.hasTimeSeries()){
91
- this._fill(data);
92
- }else{
93
- throw new Error('Time Series not supported in synchronous constructor. Use `await Model.create(data)` static method to instantiate this model.');
94
- }
95
- }
96
-
97
- checkForInclusionWithThrow(): void
98
- {
99
- this.checkForInclusionWithThrow();
100
- }
101
-
102
- static checkForInclusionWithThrow(this: OpModelType<any>, checkModelType: string): void
103
- {
104
- if(!this.checkForInclusion(this.name)){
105
- throw new Error('Model undefined: ' + this.name);
106
- }
107
- }
108
-
109
- checkForInclusion(): boolean
110
- {
111
- return this.checkForInclusion();
112
- }
113
-
114
- static checkForInclusion(this: OpModelType<any>, checkModelType: string): boolean
115
- {
116
- return this.loadModels().find((definedModel: OpModelType<any>) => {
117
- return definedModel.name === checkModelType;
118
- }) !== undefined;
119
- }
120
-
121
- protected _fill(data: any): RWSModel<ChildClass>{
122
- for (const key in data) {
123
- if (data.hasOwnProperty(key)) {
124
-
125
- const meta = Reflect.getMetadata(`InverseTimeSeries:${key}`, (this as any).constructor.prototype);
126
-
127
- if(meta){
128
- data[key] = {
129
- create: data[key]
130
- };
131
- }else{
132
- this[key] = data[key];
133
- }
134
- }
135
- }
136
-
137
- return this;
138
- }
139
-
140
- protected hasRelation(key: string): boolean
141
- {
142
- return !!this[key] && this[key] instanceof RWSModel;
143
- }
144
-
145
- protected bindRelation(key: string, relatedModel: RWSModel<any>): RelationBindType
146
- {
147
- return {
148
- connect: {
149
- id: relatedModel.id
150
- }
151
- };
152
- }
153
-
154
- public async _asyncFill(data: any, fullDataMode = false, allowRelations = true): Promise<ChildClass> {
155
- const collections_to_models: {[key: string]: any} = {};
156
- const timeSeriesIds = this.getTimeSeriesModelFields();
157
-
158
- const classFields = FieldsHelper.getAllClassFields(this.constructor);
159
-
160
- // Get both relation metadata types asynchronously
161
- const [relOneData, relManyData] = await Promise.all([
162
- this.getRelationOneMeta(classFields),
163
- this.getRelationManyMeta(classFields)
164
- ]);
165
-
166
- this.loadModels().forEach((model) => {
167
- collections_to_models[model.getCollection()] = model;
168
- });
169
-
170
- const seriesHydrationfields: string[] = [];
171
-
172
- if (allowRelations) {
173
- // Handle many-to-many relations
174
- for (const key in relManyData) {
175
- if(!fullDataMode && (this as any).constructor._CUT_KEYS.includes(key)){
176
- continue;
177
- }
178
-
179
- const relMeta = relManyData[key];
180
-
181
- const relationEnabled = this.checkRelEnabled(relMeta.key);
182
- if (relationEnabled) {
183
- this[relMeta.key] = await relMeta.inversionModel.findBy({
184
- conditions: {
185
- [relMeta.foreignKey]: data.id
186
- },
187
- allowRelations: false
188
- });
189
- }
190
- }
191
-
192
- // Handle one-to-one relations
193
- for (const key in relOneData) {
194
- if(!fullDataMode && (this as any).constructor._CUT_KEYS.includes(key)){
195
- continue;
196
- }
197
-
198
- const relMeta = relOneData[key];
199
- const relationEnabled = this.checkRelEnabled(relMeta.key);
200
-
201
- if(!data[relMeta.hydrationField] && relMeta.required){
202
- throw new Error(`Relation field "${relMeta.hydrationField}" is required in model ${this.constructor.name}.`)
203
- }
204
-
205
- if (relationEnabled && data[relMeta.hydrationField]) {
206
- this[relMeta.key] = await relMeta.model.find(data[relMeta.hydrationField], { allowRelations: false });
207
- }
208
- else if(relationEnabled && !data[relMeta.hydrationField] && data[relMeta.key]){
209
- const newRelModel: RWSModel<any> = await relMeta.model.create(data[relMeta.key]);
210
- this[relMeta.key] = await newRelModel.save();
211
- }
212
-
213
- const cutKeys = ((this.constructor as any)._CUT_KEYS as string[]);
214
-
215
- if(!cutKeys.includes(relMeta.hydrationField)){
216
- cutKeys.push(relMeta.hydrationField)
217
- }
218
- }
219
- }
220
-
221
- // Process regular fields and time series
222
- for (const key in data) {
223
- if (data.hasOwnProperty(key)) {
224
- if(!fullDataMode && (this as any).constructor._CUT_KEYS.includes(key)){
225
- continue;
226
- }
227
-
228
- if (Object.keys(relOneData).includes(key)) {
229
- continue;
230
- }
231
-
232
- if (seriesHydrationfields.includes(key)) {
233
- continue;
234
- }
235
-
236
- const timeSeriesMetaData = timeSeriesIds[key];
237
-
238
- if (timeSeriesMetaData) {
239
- this[key] = data[key];
240
- const seriesModel = collections_to_models[timeSeriesMetaData.collection];
241
-
242
- const dataModels = await seriesModel.findBy({
243
- id: { in: data[key] }
244
- });
245
-
246
- seriesHydrationfields.push(timeSeriesMetaData.hydrationField);
247
-
248
- this[timeSeriesMetaData.hydrationField] = dataModels;
249
- } else {
250
- this[key] = data[key];
251
- }
252
- }
253
- }
254
-
255
- return this as any as ChildClass;
256
- }
257
-
258
- private getModelScalarFields(model: OpModelType<any>): string[]
259
- {
260
- return FieldsHelper.getAllClassFields(model)
261
- .filter(item => item.indexOf('TrackType') === 0)
262
- .map(item => item.split(':').at(-1))
263
- }
264
-
265
- private getTimeSeriesModelFields()
266
- {
267
- const timeSeriesIds: {[key: string]: {collection: string, hydrationField: string, ids: string[]}} = {};
268
-
269
- for (const key in this as any) {
270
- if (this.hasOwnProperty(key)) {
271
-
272
- const meta = Reflect.getMetadata(`InverseTimeSeries:${key}`, (this as any));
273
- if(meta){
274
- if(!timeSeriesIds[key]){
275
- timeSeriesIds[key] = {
276
- collection: meta.timeSeriesModel,
277
- hydrationField: meta.hydrationField,
278
- ids: this[key]
279
- };
280
- }
281
- }
282
- }
283
- }
284
-
285
- return timeSeriesIds;
286
- }
287
-
288
- private async getRelationOneMeta(classFields: string[]): Promise<RelOneMetaType<RWSModel<any>>> {
289
- return RWSModel.getRelationOneMeta(this, classFields);
290
- }
291
-
292
- static async getRelationOneMeta(model: any, classFields: string[]): Promise<RelOneMetaType<RWSModel<any>>>
293
- {
294
- const relIds: RelOneMetaType<RWSModel<any>> = {};
295
- const relationFields = classFields
296
- .filter((item: string) => item.indexOf('Relation') === 0 && !item.includes('Inverse'))
297
- .map((item: string) => item.split(':').at(-1));
298
-
299
- for (const key of relationFields) {
300
- const metadataKey = `Relation:${key}`;
301
- const metadata = Reflect.getMetadata(metadataKey, model);
302
-
303
- if (metadata && metadata.promise) {
304
- const resolvedMetadata = await metadata.promise;
305
- if (!relIds[key]) {
306
- relIds[key] = {
307
- key: resolvedMetadata.key,
308
- required: resolvedMetadata.required,
309
- model: resolvedMetadata.relatedTo,
310
- hydrationField: resolvedMetadata.relationField,
311
- foreignKey: resolvedMetadata.relatedToField
312
- };
313
- }
314
- }
315
- }
316
-
317
- return relIds;
318
- }
319
-
320
- private async getRelationManyMeta(classFields: string[]): Promise<RelManyMetaType<RWSModel<any>>> {
321
- return RWSModel.getRelationManyMeta(this, classFields);
322
- }
323
-
324
- static async getRelationManyMeta(model: any, classFields: string[]): Promise<RelManyMetaType<RWSModel<any>>>
325
- {
326
- const relIds: RelManyMetaType<RWSModel<any>> = {};
327
-
328
- const inverseFields = classFields
329
- .filter((item: string) => item.indexOf('InverseRelation') === 0)
330
- .map((item: string) => item.split(':').at(-1));
331
-
332
- for (const key of inverseFields) {
333
- const metadataKey = `InverseRelation:${key}`;
334
- const metadata = Reflect.getMetadata(metadataKey, model);
335
-
336
- if (metadata && metadata.promise) {
337
- const resolvedMetadata = await metadata.promise;
338
- if (!relIds[key]) {
339
- relIds[key] = {
340
- key: resolvedMetadata.key,
341
- inversionModel: resolvedMetadata.inversionModel,
342
- foreignKey: resolvedMetadata.foreignKey
343
- };
344
- }
345
- }
346
- }
347
-
348
- return relIds;
349
- }
350
-
351
- public async toMongo(): Promise<any> {
352
- const data: any = {};
353
- const timeSeriesIds = this.getTimeSeriesModelFields();
354
- const timeSeriesHydrationFields: string[] = [];
355
-
356
- for (const key in (this as any)) {
357
- if (this.hasRelation(key)) {
358
- data[key] = this.bindRelation(key, this[key]);
359
- continue;
360
- }
361
-
362
- if (!(await this.isDbVariable(key))) {
363
- continue;
364
- }
365
-
366
- const passedFieldCondition: boolean = this.hasOwnProperty(key) &&
367
- !((this as any).constructor._BANNED_KEYS
368
- || RWSModel._BANNED_KEYS
369
- ).includes(key) &&
370
- !timeSeriesHydrationFields.includes(key)
371
- ;
372
-
373
- if (passedFieldCondition) {
374
- data[key] = this[key];
375
- }
376
-
377
- if (timeSeriesIds[key]) {
378
- data[key] = this[key];
379
- timeSeriesHydrationFields.push(timeSeriesIds[key].hydrationField);
380
- }
381
- }
382
-
383
- return data;
384
- }
385
-
386
- getCollection(): string | null {
387
- return (this as any).constructor._collection || this._collection;
388
- }
389
-
390
- static getCollection(): string | null {
391
- return (this as any).constructor._collection || this._collection;
392
- }
393
-
394
-
395
- async save(): Promise<this> {
396
- const data = await this.toMongo();
397
- let updatedModelData = data;
398
- if (this.id) {
399
- this.preUpdate();
400
-
401
- updatedModelData = await this.dbService.update(data, this.getCollection());
402
-
403
- await this._asyncFill(updatedModelData);
404
- this.postUpdate();
405
- } else {
406
- this.preCreate();
407
-
408
- const timeSeriesModel = await import('./TimeSeriesModel');
409
- const isTimeSeries = this instanceof timeSeriesModel.default;
410
-
411
- updatedModelData = await this.dbService.insert(data, this.getCollection(), isTimeSeries);
412
-
413
- await this._asyncFill(updatedModelData);
414
-
415
- this.postCreate();
416
- }
417
-
418
- return this;
419
- }
420
-
421
- static async getModelAnnotations<T extends unknown>(constructor: new () => T): Promise<Record<string, {annotationType: string, metadata: any}>> {
422
- const annotationsData: Record<string, {annotationType: string, metadata: any}> = {};
423
-
424
- const metadataKeys = Reflect.getMetadataKeys(constructor.prototype);
425
-
426
- // Process all metadata keys and collect promises
427
- const metadataPromises = metadataKeys.map(async (fullKey: string) => {
428
- const [annotationType, propertyKey] = fullKey.split(':');
429
- const metadata = Reflect.getMetadata(fullKey, constructor.prototype);
430
-
431
- if (metadata) {
432
- // If this is a relation metadata with a promise
433
- if (metadata.promise && (annotationType === 'Relation' || annotationType === 'InverseRelation')) {
434
- const resolvedMetadata = await metadata.promise;
435
- annotationsData[propertyKey] = {
436
- annotationType,
437
- metadata: resolvedMetadata
438
- };
439
- } else {
440
- // Handle non-relation metadata as before
441
- const key = metadata.key || propertyKey;
442
- annotationsData[key] = {
443
- annotationType,
444
- metadata
445
- };
446
- }
447
- }
448
- });
449
-
450
- // Wait for all metadata to be processed
451
- await Promise.all(metadataPromises);
452
-
453
- return annotationsData;
454
- }
455
-
456
- public preUpdate(): void
457
- {
458
- return;
459
- }
460
-
461
- public postUpdate(): void
462
- {
463
- return;
464
- }
465
-
466
- public preCreate(): void
467
- {
468
- return;
469
- }
470
-
471
- public postCreate(): void
472
- {
473
- return;
474
- }
475
-
476
- public static isSubclass<T extends RWSModel<T>, C extends new () => T>(constructor: C, baseClass: new () => T): boolean {
477
- return baseClass.prototype.isPrototypeOf(constructor.prototype);
478
- }
479
-
480
- hasTimeSeries(): boolean
481
- {
482
- return RWSModel.checkTimeSeries((this as any).constructor);
483
- }
484
-
485
- static checkTimeSeries(constructor: any): boolean
486
- {
487
- const data = constructor.prototype as any;
488
-
489
- for (const key in data) {
490
-
491
- if (data.hasOwnProperty(key)) {
492
-
493
- if(Reflect.getMetadata(`InverseTimeSeries:${key}`, constructor.prototype)){
494
- return true;
495
- }
496
- }
497
- }
498
-
499
- return false;
500
- }
501
-
502
- async isDbVariable(variable: string): Promise<boolean>
503
- {
504
- return RWSModel.checkDbVariable((this as any).constructor, variable);
505
- }
506
-
507
- static async checkDbVariable(constructor: any, variable: string): Promise<boolean> {
508
- if(variable === 'id'){
509
- return true;
510
- }
511
-
512
- const dbAnnotations = await RWSModel.getModelAnnotations(constructor);
513
- type AnnotationType = { annotationType: string, key: string };
514
-
515
- const dbProperties: string[] = Object.keys(dbAnnotations)
516
- .map((key: string): AnnotationType => {return {...dbAnnotations[key], key};})
517
- .filter((element: AnnotationType) => element.annotationType === 'TrackType' )
518
- .map((element: AnnotationType) => element.key);
519
-
520
- return dbProperties.includes(variable);
521
- }
522
-
523
- sanitizeDBData(data: any): any
524
- {
525
- const dataKeys = Object.keys(data);
526
- const sanitizedData: {[key: string]: any} = {};
527
-
528
- for (const key of dataKeys){
529
- if(this.isDbVariable(key)){
530
- sanitizedData[key] = data[key];
531
- }
532
- }
533
-
534
- return sanitizedData;
535
- }
536
-
537
- public static async watchCollection<ChildClass extends RWSModel<ChildClass>>(
538
- this: OpModelType<ChildClass>,
539
- preRun: () => void
540
- ){
541
- const collection = Reflect.get(this, '_collection');
542
- this.checkForInclusionWithThrow(this.name);
543
- return await this.services.dbService.watchCollection(collection, preRun);
544
- }
545
-
546
- public static async findOneBy<ChildClass extends RWSModel<ChildClass>>(
547
- this: OpModelType<ChildClass>,
548
- findParams?: FindByType
549
- ): Promise<ChildClass | null> {
550
- const conditions = findParams?.conditions ?? {};
551
- const ordering = findParams?.ordering ?? null;
552
- const fields = findParams?.fields ?? null;
553
- const allowRelations = findParams?.allowRelations ?? true;
554
- const fullData = findParams?.fullData ?? false;
555
-
556
- this.checkForInclusionWithThrow('');
557
-
558
-
559
- const collection = Reflect.get(this, '_collection');
560
- const dbData = await this.services.dbService.findOneBy(collection, conditions, fields, ordering, allowRelations);
561
-
562
-
563
- if (dbData) {
564
- const inst: ChildClass = new (this as { new(): ChildClass })();
565
- return await inst._asyncFill(dbData, fullData, allowRelations);
566
- }
567
-
568
- return null;
569
- }
570
-
571
- public static async find<ChildClass extends RWSModel<ChildClass>>(
572
- this: OpModelType<ChildClass>,
573
- id: string,
574
- findParams: Omit<FindByType, 'conditions'> = null
575
- ): Promise<ChildClass | null> {
576
- const ordering = findParams?.ordering ?? null;
577
- const fields = findParams?.fields ?? null;
578
- const allowRelations = findParams?.allowRelations ?? true;
579
- const fullData = findParams?.fullData ?? false;
580
-
581
- const collection = Reflect.get(this, '_collection');
582
- this.checkForInclusionWithThrow(this.name);
583
-
584
- const dbData = await this.services.dbService.findOneBy(collection, { id }, fields, ordering, allowRelations);
585
-
586
- if (dbData) {
587
- const inst: ChildClass = new (this as { new(): ChildClass })();
588
- return await inst._asyncFill(dbData, fullData, allowRelations);
589
- }
590
-
591
- return null;
592
- }
593
-
594
- public static async findBy<ChildClass extends RWSModel<ChildClass>>(
595
- this: OpModelType<ChildClass>,
596
- findParams?: FindByType
597
- ): Promise<ChildClass[]> {
598
- const conditions = findParams?.conditions ?? {};
599
- const ordering = findParams?.ordering ?? null;
600
- const fields = findParams?.fields ?? null;
601
- const allowRelations = findParams?.allowRelations ?? true;
602
- const fullData = findParams?.fullData ?? false;
603
-
604
- const collection = Reflect.get(this, '_collection');
605
- this.checkForInclusionWithThrow(this.name);
606
- try {
607
- const dbData = await this.services.dbService.findBy(collection, conditions, fields, ordering, allowRelations);
608
- if (dbData.length) {
609
- const instanced: ChildClass[] = [];
610
-
611
- for (const data of dbData) {
612
- const inst: ChildClass = new (this as { new(): ChildClass })();
613
- instanced.push((await inst._asyncFill(data, fullData,allowRelations)) as ChildClass);
614
- }
615
-
616
- return instanced;
617
- }
618
-
619
- return [];
620
- } catch (rwsError: Error | any) {
621
- console.error(rwsError);
622
-
623
- throw rwsError;
624
- }
625
- }
626
-
627
- public static async delete<ChildClass extends RWSModel<ChildClass>>(
628
- this: OpModelType<ChildClass>,
629
- conditions: any
630
- ): Promise<void> {
631
- const collection = Reflect.get(this, '_collection');
632
- this.checkForInclusionWithThrow(this.name);
633
- return await this.services.dbService.delete(collection, conditions);
634
- }
635
-
636
- public async delete<ChildClass extends RWSModel<ChildClass>>(): Promise<void> {
637
- const collection = Reflect.get(this, '_collection');
638
- this.checkForInclusionWithThrow();
639
- return await this.dbService.delete(collection, {
640
- id: this.id
641
- });
642
- }
643
-
644
-
645
- static async create<T extends RWSModel<T>>(this: new () => T, data: any): Promise<T> {
646
- const newModel = new this();
647
-
648
- const sanitizedData = newModel.sanitizeDBData(data);
649
-
650
- await newModel._asyncFill(sanitizedData);
651
-
652
- return newModel;
653
- }
654
-
655
- static loadModels(): OpModelType<any>[]
656
- {
657
- return this.allModels || [];
658
- }
659
-
660
- loadModels(): OpModelType<any>[]
661
- {
662
- return RWSModel.loadModels();
663
- }
664
-
665
- private checkRelEnabled(key: string): boolean
666
- {
667
- return Object.keys((this as any).constructor._RELATIONS).includes(key) && (this as any).constructor._RELATIONS[key] === true
668
- }
669
-
670
- public static setServices(services: IRWSModelServices){
671
- this.allModels = services.configService.get('db_models');
672
- this.services = services;
673
- }
674
- }
675
-
676
- export { IModel, TrackType, IMetaOpts, RWSModel };
677
-
678
-