@rws-framework/db 2.3.2 → 2.4.1

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 +0 -0
  6. package/dist/decorators/InverseRelation.js +0 -0
  7. package/dist/decorators/InverseTimeSeries.d.ts +0 -0
  8. package/dist/decorators/InverseTimeSeries.js +0 -0
  9. package/dist/decorators/RWSCollection.d.ts +0 -0
  10. package/dist/decorators/RWSCollection.js +0 -0
  11. package/dist/decorators/Relation.d.ts +0 -0
  12. package/dist/decorators/Relation.js +0 -0
  13. package/dist/decorators/TrackType.d.ts +0 -0
  14. package/dist/decorators/TrackType.js +0 -0
  15. package/dist/decorators/index.d.ts +0 -0
  16. package/dist/decorators/index.js +0 -0
  17. package/dist/helper/DbHelper.d.ts +0 -0
  18. package/dist/helper/DbHelper.js +7 -7
  19. package/dist/helper/FieldsHelper.d.ts +0 -0
  20. package/dist/helper/FieldsHelper.js +0 -0
  21. package/dist/index.d.ts +0 -0
  22. package/dist/index.js +0 -0
  23. package/dist/models/TimeSeriesModel.d.ts +7 -7
  24. package/dist/models/TimeSeriesModel.js +33 -33
  25. package/dist/models/_model.d.ts +0 -0
  26. package/dist/models/_model.js +0 -0
  27. package/dist/models/core/RWSModel.d.ts +0 -0
  28. package/dist/models/core/RWSModel.js +0 -0
  29. package/dist/models/core/TimeSeriesModel.d.ts +0 -0
  30. package/dist/models/core/TimeSeriesModel.js +0 -0
  31. package/dist/models/index.d.ts +0 -0
  32. package/dist/models/index.js +0 -0
  33. package/dist/models/interfaces/IModel.d.ts +0 -0
  34. package/dist/models/interfaces/IModel.js +0 -0
  35. package/dist/models/interfaces/IRWSModelServices.d.ts +0 -0
  36. package/dist/models/interfaces/IRWSModelServices.js +0 -0
  37. package/dist/models/interfaces/OpModelType.d.ts +0 -0
  38. package/dist/models/interfaces/OpModelType.js +0 -0
  39. package/dist/models/types/RelationTypes.d.ts +0 -0
  40. package/dist/models/types/RelationTypes.js +0 -0
  41. package/dist/models/utils/ModelUtils.d.ts +0 -0
  42. package/dist/models/utils/ModelUtils.js +0 -0
  43. package/dist/models/utils/PaginationUtils.d.ts +0 -0
  44. package/dist/models/utils/PaginationUtils.js +0 -0
  45. package/dist/models/utils/RelationUtils.d.ts +0 -0
  46. package/dist/models/utils/RelationUtils.js +0 -0
  47. package/dist/models/utils/TimeSeriesUtils.d.ts +0 -0
  48. package/dist/models/utils/TimeSeriesUtils.js +0 -0
  49. package/dist/services/DBService.d.ts +1 -1
  50. package/dist/services/DBService.js +6 -6
  51. package/dist/types/DbConfigHandler.d.ts +3 -2
  52. package/dist/types/DbConfigHandler.js +0 -0
  53. package/dist/types/FindParams.d.ts +0 -0
  54. package/dist/types/FindParams.js +0 -0
  55. package/dist/types/IRWSModel.d.ts +0 -0
  56. package/dist/types/IRWSModel.js +0 -0
  57. package/dist/types/ITimeSeries.d.ts +0 -0
  58. package/dist/types/ITimeSeries.js +0 -0
  59. package/exec/console.js +110 -110
  60. package/exec/db.rws.webpack.config.js +168 -168
  61. package/exec/src/cli.ts +75 -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,19 +1,19 @@
1
- import { RWSModel } from './RWSModel';
2
- import { TrackType } from '../../decorators';
3
-
4
- // export default class TimeSeriesModel<T> extends RWSModel<T> {
5
- // @TrackType(Number) value: number;
6
-
7
- // @TrackType(Date) timestamp: Date;
8
-
9
- // @TrackType(Object)
10
- // params: any;
11
-
12
- // constructor(data?: any) {
13
- // super(data);
14
-
15
- // if(!this.timestamp) {
16
- // this.timestamp = new Date();
17
- // }
18
- // }
19
- // }
1
+ import { RWSModel } from './RWSModel';
2
+ import { TrackType } from '../../decorators';
3
+
4
+ // export default class TimeSeriesModel<T> extends RWSModel<T> {
5
+ // @TrackType(Number) value: number;
6
+
7
+ // @TrackType(Date) timestamp: Date;
8
+
9
+ // @TrackType(Object)
10
+ // params: any;
11
+
12
+ // constructor(data?: any) {
13
+ // super(data);
14
+
15
+ // if(!this.timestamp) {
16
+ // this.timestamp = new Date();
17
+ // }
18
+ // }
19
+ // }
@@ -1,20 +1,20 @@
1
- import { IModel } from './interfaces/IModel';
2
- import { IRWSModelServices } from './interfaces/IRWSModelServices';
3
- import { OpModelType } from './interfaces/OpModelType';
4
- import { RelationBindType, RelOneMetaType, RelManyMetaType } from './types/RelationTypes';
5
- import { RWSModel } from './core/RWSModel';
6
- // import TimeSeriesModel from './core/TimeSeriesModel';
7
- import { TrackType, IMetaOpts } from '../decorators';
8
-
9
- export {
10
- IModel,
11
- IRWSModelServices,
12
- OpModelType,
13
- RelationBindType,
14
- RelOneMetaType,
15
- RelManyMetaType,
16
- RWSModel,
17
- // TimeSeriesModel,
18
- TrackType,
19
- IMetaOpts
20
- };
1
+ import { IModel } from './interfaces/IModel';
2
+ import { IRWSModelServices } from './interfaces/IRWSModelServices';
3
+ import { OpModelType } from './interfaces/OpModelType';
4
+ import { RelationBindType, RelOneMetaType, RelManyMetaType } from './types/RelationTypes';
5
+ import { RWSModel } from './core/RWSModel';
6
+ // import TimeSeriesModel from './core/TimeSeriesModel';
7
+ import { TrackType, IMetaOpts } from '../decorators';
8
+
9
+ export {
10
+ IModel,
11
+ IRWSModelServices,
12
+ OpModelType,
13
+ RelationBindType,
14
+ RelOneMetaType,
15
+ RelManyMetaType,
16
+ RWSModel,
17
+ // TimeSeriesModel,
18
+ TrackType,
19
+ IMetaOpts
20
+ };
@@ -1,12 +1,12 @@
1
- import { IDbConfigHandler } from '../../types/DbConfigHandler';
2
- import { DBService } from '../../services/DBService';
3
-
4
- export interface IModel {
5
- [key: string]: any;
6
- id: string | null;
7
- save: () => void;
8
- getDb: () => DBService;
9
- getCollection: () => string | null;
10
- configService?: IDbConfigHandler;
11
- dbService?: DBService;
12
- }
1
+ import { IDbConfigHandler } from '../../types/DbConfigHandler';
2
+ import { DBService } from '../../services/DBService';
3
+
4
+ export interface IModel {
5
+ [key: string]: any;
6
+ id: string | null;
7
+ save: () => void;
8
+ getDb: () => DBService;
9
+ getCollection: () => string | null;
10
+ configService?: IDbConfigHandler;
11
+ dbService?: DBService;
12
+ }
@@ -1,7 +1,7 @@
1
- import { IDbConfigHandler } from '../../types/DbConfigHandler';
2
- import { DBService } from '../../services/DBService';
3
-
4
- export interface IRWSModelServices {
5
- configService?: IDbConfigHandler;
6
- dbService?: DBService;
7
- }
1
+ import { IDbConfigHandler } from '../../types/DbConfigHandler';
2
+ import { DBService } from '../../services/DBService';
3
+
4
+ export interface IRWSModelServices {
5
+ configService?: IDbConfigHandler;
6
+ dbService?: DBService;
7
+ }
@@ -1,49 +1,49 @@
1
- import { IRWSModel } from '../../types/IRWSModel';
2
- import { FindByType, IPaginationParams } from '../../types/FindParams';
3
- import { IRWSModelServices } from './IRWSModelServices';
4
- import { RelOneMetaType, RelManyMetaType } from '../types/RelationTypes';
5
- import { DBService } from '../../services/DBService';
6
-
7
- // Reference to the RWSModel class to avoid circular dependency
8
- import type { RWSModel } from '../core/RWSModel';
9
-
10
- export interface OpModelType<T> {
11
- new(data?: any | null): T;
12
- services: IRWSModelServices;
13
- name: string;
14
- _collection: string;
15
- _RELATIONS: {[key: string]: boolean};
16
- _CUT_KEYS: string[];
17
- allModels: OpModelType<any>[];
18
- loadModels: () => OpModelType<any>[];
19
- checkForInclusionWithThrow: (className: string) => void;
20
- checkForInclusion: (className: string) => boolean;
21
- findOneBy<T extends RWSModel<T>>(
22
- this: OpModelType<T>,
23
- findParams?: FindByType
24
- ): Promise<T | null>;
25
- find<T extends RWSModel<T>>(
26
- this: OpModelType<T>,
27
- id: string,
28
- findParams?: Omit<FindByType, 'conditions'>
29
- ): Promise<T | null>;
30
- findBy<T extends RWSModel<T>>(
31
- this: OpModelType<T>,
32
- findParams?: FindByType
33
- ): Promise<T[]>;
34
- paginate<T extends RWSModel<T>>(
35
- this: OpModelType<T>,
36
- paginateParams?: IPaginationParams,
37
- findParams?: FindByType
38
- ): Promise<T[]>;
39
- delete<T extends RWSModel<T>>(
40
- this: OpModelType<T>,
41
- conditions: any
42
- ): Promise<void>;
43
- create<T extends RWSModel<T>>(this: new () => T, data: any): Promise<T>;
44
- getRelationOneMeta(model: any, classFields: string[]): Promise<RelOneMetaType<IRWSModel>>;
45
- getRelationManyMeta(model: any, classFields: string[]): Promise<RelManyMetaType<IRWSModel>>;
46
- getCollection(): string;
47
- getDb(): DBService;
48
- setServices(services: IRWSModelServices): void;
49
- }
1
+ import { IRWSModel } from '../../types/IRWSModel';
2
+ import { FindByType, IPaginationParams } from '../../types/FindParams';
3
+ import { IRWSModelServices } from './IRWSModelServices';
4
+ import { RelOneMetaType, RelManyMetaType } from '../types/RelationTypes';
5
+ import { DBService } from '../../services/DBService';
6
+
7
+ // Reference to the RWSModel class to avoid circular dependency
8
+ import type { RWSModel } from '../core/RWSModel';
9
+
10
+ export interface OpModelType<T> {
11
+ new(data?: any | null): T;
12
+ services: IRWSModelServices;
13
+ name: string;
14
+ _collection: string;
15
+ _RELATIONS: {[key: string]: boolean};
16
+ _CUT_KEYS: string[];
17
+ allModels: OpModelType<any>[];
18
+ loadModels: () => OpModelType<any>[];
19
+ checkForInclusionWithThrow: (className: string) => void;
20
+ checkForInclusion: (className: string) => boolean;
21
+ findOneBy<T extends RWSModel<T>>(
22
+ this: OpModelType<T>,
23
+ findParams?: FindByType
24
+ ): Promise<T | null>;
25
+ find<T extends RWSModel<T>>(
26
+ this: OpModelType<T>,
27
+ id: string,
28
+ findParams?: Omit<FindByType, 'conditions'>
29
+ ): Promise<T | null>;
30
+ findBy<T extends RWSModel<T>>(
31
+ this: OpModelType<T>,
32
+ findParams?: FindByType
33
+ ): Promise<T[]>;
34
+ paginate<T extends RWSModel<T>>(
35
+ this: OpModelType<T>,
36
+ paginateParams?: IPaginationParams,
37
+ findParams?: FindByType
38
+ ): Promise<T[]>;
39
+ delete<T extends RWSModel<T>>(
40
+ this: OpModelType<T>,
41
+ conditions: any
42
+ ): Promise<void>;
43
+ create<T extends RWSModel<T>>(this: new () => T, data: any): Promise<T>;
44
+ getRelationOneMeta(model: any, classFields: string[]): Promise<RelOneMetaType<IRWSModel>>;
45
+ getRelationManyMeta(model: any, classFields: string[]): Promise<RelManyMetaType<IRWSModel>>;
46
+ getCollection(): string;
47
+ getDb(): DBService;
48
+ setServices(services: IRWSModelServices): void;
49
+ }
@@ -1,25 +1,25 @@
1
- import { IRWSModel } from '../../types/IRWSModel';
2
- // Using a type reference to avoid circular dependency
3
- type OpModelType<T> = any;
4
-
5
- export type RelationBindType = {
6
- connect: { id: string }
7
- };
8
-
9
- export type RelOneMetaType<T extends IRWSModel> = {
10
- [key: string]: {
11
- required: boolean,
12
- key?: string,
13
- model: OpModelType<T>,
14
- hydrationField: string,
15
- foreignKey: string
16
- }
17
- };
18
-
19
- export type RelManyMetaType<T extends IRWSModel> = {
20
- [key: string]: {
21
- key: string,
22
- inversionModel: OpModelType<T>,
23
- foreignKey: string
24
- }
25
- };
1
+ import { IRWSModel } from '../../types/IRWSModel';
2
+ // Using a type reference to avoid circular dependency
3
+ type OpModelType<T> = any;
4
+
5
+ export type RelationBindType = {
6
+ connect: { id: string }
7
+ };
8
+
9
+ export type RelOneMetaType<T extends IRWSModel> = {
10
+ [key: string]: {
11
+ required: boolean,
12
+ key?: string,
13
+ model: OpModelType<T>,
14
+ hydrationField: string,
15
+ foreignKey: string
16
+ }
17
+ };
18
+
19
+ export type RelManyMetaType<T extends IRWSModel> = {
20
+ [key: string]: {
21
+ key: string,
22
+ inversionModel: OpModelType<T>,
23
+ foreignKey: string
24
+ }
25
+ };
@@ -1,65 +1,65 @@
1
- import { RWSModel } from "../core/RWSModel";
2
- import { FieldsHelper } from '../../helper/FieldsHelper';
3
-
4
- export class ModelUtils {
5
- static async getModelAnnotations<T extends unknown>(constructor: new () => T): Promise<Record<string, {annotationType: string, metadata: any}>> {
6
- const annotationsData: Record<string, {annotationType: string, metadata: any}> = {};
7
-
8
- const metadataKeys = Reflect.getMetadataKeys(constructor.prototype);
9
-
10
- // Process all metadata keys and collect promises
11
- const metadataPromises = metadataKeys.map(async (fullKey: string) => {
12
- const [annotationType, propertyKey] = fullKey.split(':');
13
- const metadata = Reflect.getMetadata(fullKey, constructor.prototype);
14
-
15
- if (metadata) {
16
- // If this is a relation metadata with a promise
17
- if (metadata.promise && (annotationType === 'Relation' || annotationType === 'InverseRelation')) {
18
- const resolvedMetadata = await metadata.promise;
19
- annotationsData[propertyKey] = {
20
- annotationType,
21
- metadata: resolvedMetadata
22
- };
23
- } else {
24
- // Handle non-relation metadata as before
25
- const key = metadata.key || propertyKey;
26
- annotationsData[key] = {
27
- annotationType,
28
- metadata
29
- };
30
- }
31
- }
32
- });
33
-
34
- // Wait for all metadata to be processed
35
- await Promise.all(metadataPromises);
36
-
37
- return annotationsData;
38
- }
39
-
40
- static async checkDbVariable(constructor: any, variable: string): Promise<boolean> {
41
- if(variable === 'id'){
42
- return true;
43
- }
44
-
45
- const dbAnnotations = await ModelUtils.getModelAnnotations(constructor);
46
- type AnnotationType = { annotationType: string, key: string };
47
-
48
- const dbProperties: string[] = Object.keys(dbAnnotations)
49
- .map((key: string): AnnotationType => {return {...dbAnnotations[key], key};})
50
- .filter((element: AnnotationType) => element.annotationType === 'TrackType' )
51
- .map((element: AnnotationType) => element.key);
52
-
53
- return dbProperties.includes(variable);
54
- }
55
-
56
- static isSubclass<T, C extends new () => T>(constructor: C, baseClass: new () => T): boolean {
57
- return baseClass.prototype.isPrototypeOf(constructor.prototype);
58
- }
59
-
60
- static getModelScalarFields(model: RWSModel<any>): string[] {
61
- return FieldsHelper.getAllClassFields(model)
62
- .filter((item: string) => item.indexOf('TrackType') === 0)
63
- .map((item: string) => item.split(':').at(-1));
64
- }
65
- }
1
+ import { RWSModel } from "../core/RWSModel";
2
+ import { FieldsHelper } from '../../helper/FieldsHelper';
3
+
4
+ export class ModelUtils {
5
+ static async getModelAnnotations<T extends unknown>(constructor: new () => T): Promise<Record<string, {annotationType: string, metadata: any}>> {
6
+ const annotationsData: Record<string, {annotationType: string, metadata: any}> = {};
7
+
8
+ const metadataKeys = Reflect.getMetadataKeys(constructor.prototype);
9
+
10
+ // Process all metadata keys and collect promises
11
+ const metadataPromises = metadataKeys.map(async (fullKey: string) => {
12
+ const [annotationType, propertyKey] = fullKey.split(':');
13
+ const metadata = Reflect.getMetadata(fullKey, constructor.prototype);
14
+
15
+ if (metadata) {
16
+ // If this is a relation metadata with a promise
17
+ if (metadata.promise && (annotationType === 'Relation' || annotationType === 'InverseRelation')) {
18
+ const resolvedMetadata = await metadata.promise;
19
+ annotationsData[propertyKey] = {
20
+ annotationType,
21
+ metadata: resolvedMetadata
22
+ };
23
+ } else {
24
+ // Handle non-relation metadata as before
25
+ const key = metadata.key || propertyKey;
26
+ annotationsData[key] = {
27
+ annotationType,
28
+ metadata
29
+ };
30
+ }
31
+ }
32
+ });
33
+
34
+ // Wait for all metadata to be processed
35
+ await Promise.all(metadataPromises);
36
+
37
+ return annotationsData;
38
+ }
39
+
40
+ static async checkDbVariable(constructor: any, variable: string): Promise<boolean> {
41
+ if(variable === 'id'){
42
+ return true;
43
+ }
44
+
45
+ const dbAnnotations = await ModelUtils.getModelAnnotations(constructor);
46
+ type AnnotationType = { annotationType: string, key: string };
47
+
48
+ const dbProperties: string[] = Object.keys(dbAnnotations)
49
+ .map((key: string): AnnotationType => {return {...dbAnnotations[key], key};})
50
+ .filter((element: AnnotationType) => element.annotationType === 'TrackType' )
51
+ .map((element: AnnotationType) => element.key);
52
+
53
+ return dbProperties.includes(variable);
54
+ }
55
+
56
+ static isSubclass<T, C extends new () => T>(constructor: C, baseClass: new () => T): boolean {
57
+ return baseClass.prototype.isPrototypeOf(constructor.prototype);
58
+ }
59
+
60
+ static getModelScalarFields(model: RWSModel<any>): string[] {
61
+ return FieldsHelper.getAllClassFields(model)
62
+ .filter((item: string) => item.indexOf('TrackType') === 0)
63
+ .map((item: string) => item.split(':').at(-1));
64
+ }
65
+ }
@@ -1,42 +1,42 @@
1
- import { RelOneMetaType, RelManyMetaType } from '../types/RelationTypes';
2
- import { IRWSModel } from '../../types/IRWSModel';
3
- import { OpModelType, RWSModel } from '../_model';
4
- import { FindByType, IPaginationParams } from '../../types/FindParams';
5
-
6
- export class PaginationUtils {
7
-
8
- public static async paginate<T extends RWSModel<T>>(
9
- this: OpModelType<T>,
10
- paginationParams: IPaginationParams = { page: 0, per_page: 50 },
11
- findParams: FindByType = {},
12
- ): Promise<T[]> {
13
- const conditions = findParams?.conditions ?? {};
14
- const ordering = findParams?.ordering ?? null;
15
- const fields = findParams?.fields ?? null;
16
- const allowRelations = findParams?.allowRelations ?? true;
17
- const fullData = findParams?.fullData ?? false;
18
-
19
- const collection = Reflect.get(this, '_collection');
20
- this.checkForInclusionWithThrow(this.name);
21
- try {
22
- const dbData = await this.services.dbService.findBy(collection, conditions, fields, ordering, paginationParams);
23
- if (dbData.length) {
24
- const instanced: T[] = [];
25
-
26
- for (const data of dbData) {
27
- const inst: T = new (this as { new(): T })();
28
- instanced.push((await inst._asyncFill(data, fullData,allowRelations)) as T);
29
- }
30
-
31
- return instanced;
32
- }
33
-
34
- return [];
35
- } catch (rwsError: Error | any) {
36
- console.error(rwsError);
37
-
38
- throw rwsError;
39
- }
40
- }
41
-
42
- }
1
+ import { RelOneMetaType, RelManyMetaType } from '../types/RelationTypes';
2
+ import { IRWSModel } from '../../types/IRWSModel';
3
+ import { OpModelType, RWSModel } from '../_model';
4
+ import { FindByType, IPaginationParams } from '../../types/FindParams';
5
+
6
+ export class PaginationUtils {
7
+
8
+ public static async paginate<T extends RWSModel<T>>(
9
+ this: OpModelType<T>,
10
+ paginationParams: IPaginationParams = { page: 0, per_page: 50 },
11
+ findParams: FindByType = {},
12
+ ): Promise<T[]> {
13
+ const conditions = findParams?.conditions ?? {};
14
+ const ordering = findParams?.ordering ?? null;
15
+ const fields = findParams?.fields ?? null;
16
+ const allowRelations = findParams?.allowRelations ?? true;
17
+ const fullData = findParams?.fullData ?? false;
18
+
19
+ const collection = Reflect.get(this, '_collection');
20
+ this.checkForInclusionWithThrow(this.name);
21
+ try {
22
+ const dbData = await this.services.dbService.findBy(collection, conditions, fields, ordering, paginationParams);
23
+ if (dbData.length) {
24
+ const instanced: T[] = [];
25
+
26
+ for (const data of dbData) {
27
+ const inst: T = new (this as { new(): T })();
28
+ instanced.push((await inst._asyncFill(data, fullData,allowRelations)) as T);
29
+ }
30
+
31
+ return instanced;
32
+ }
33
+
34
+ return [];
35
+ } catch (rwsError: Error | any) {
36
+ console.error(rwsError);
37
+
38
+ throw rwsError;
39
+ }
40
+ }
41
+
42
+ }
@@ -1,76 +1,76 @@
1
- import { RelOneMetaType, RelManyMetaType } from '../types/RelationTypes';
2
- import { IRWSModel } from '../../types/IRWSModel';
3
- import { RWSModel } from '../_model';
4
-
5
- export class RelationUtils {
6
- static async getRelationOneMeta(model: RWSModel<any>, classFields: string[]): Promise<RelOneMetaType<IRWSModel>> {
7
- const relIds: RelOneMetaType<IRWSModel> = {};
8
- const relationFields = classFields
9
- .filter((item: string) => item.indexOf('Relation') === 0 && !item.includes('Inverse'))
10
- .map((item: string) => item.split(':').at(-1));
11
-
12
- for (const key of relationFields) {
13
- const metadataKey = `Relation:${key}`;
14
- const metadata = Reflect.getMetadata(metadataKey, model);
15
-
16
- if (metadata && metadata.promise) {
17
- const resolvedMetadata = await metadata.promise;
18
- if (!relIds[key]) {
19
- relIds[key] = {
20
- key: resolvedMetadata.key,
21
- required: resolvedMetadata.required,
22
- model: resolvedMetadata.relatedTo,
23
- hydrationField: resolvedMetadata.relationField,
24
- foreignKey: resolvedMetadata.relatedToField
25
- };
26
- }
27
- }
28
- }
29
-
30
- return relIds;
31
- }
32
-
33
- static async getRelationManyMeta(model: RWSModel<any>, classFields: string[]): Promise<RelManyMetaType<IRWSModel>> {
34
- const relIds: RelManyMetaType<IRWSModel> = {};
35
-
36
- const inverseFields = classFields
37
- .filter((item: string) => item.indexOf('InverseRelation') === 0)
38
- .map((item: string) => item.split(':').at(-1));
39
-
40
- for (const key of inverseFields) {
41
- const metadataKey = `InverseRelation:${key}`;
42
- const metadata = Reflect.getMetadata(metadataKey, model);
43
-
44
- if (metadata && metadata.promise) {
45
- const resolvedMetadata = await metadata.promise;
46
- if (!relIds[key]) {
47
- relIds[key] = {
48
- key: resolvedMetadata.key,
49
- inversionModel: resolvedMetadata.inversionModel,
50
- foreignKey: resolvedMetadata.foreignKey
51
- };
52
- }
53
- }
54
- }
55
-
56
- return relIds;
57
- }
58
-
59
- static bindRelation(relatedModel: RWSModel<any>): { connect: { id: string } } {
60
- return {
61
- connect: {
62
- id: relatedModel.id
63
- }
64
- };
65
- }
66
-
67
- static hasRelation(model: RWSModel<any>, key: string): boolean {
68
- // Check if the property exists and is an object with an id property
69
- return !!model[key] && typeof model[key] === 'object' && model[key] !== null && 'id' in model[key];
70
- }
71
-
72
- static checkRelEnabled(model: RWSModel<any>, key: string): boolean {
73
- return Object.keys((model.constructor as any)._RELATIONS).includes(key) &&
74
- (model.constructor as any)._RELATIONS[key] === true;
75
- }
76
- }
1
+ import { RelOneMetaType, RelManyMetaType } from '../types/RelationTypes';
2
+ import { IRWSModel } from '../../types/IRWSModel';
3
+ import { RWSModel } from '../_model';
4
+
5
+ export class RelationUtils {
6
+ static async getRelationOneMeta(model: RWSModel<any>, classFields: string[]): Promise<RelOneMetaType<IRWSModel>> {
7
+ const relIds: RelOneMetaType<IRWSModel> = {};
8
+ const relationFields = classFields
9
+ .filter((item: string) => item.indexOf('Relation') === 0 && !item.includes('Inverse'))
10
+ .map((item: string) => item.split(':').at(-1));
11
+
12
+ for (const key of relationFields) {
13
+ const metadataKey = `Relation:${key}`;
14
+ const metadata = Reflect.getMetadata(metadataKey, model);
15
+
16
+ if (metadata && metadata.promise) {
17
+ const resolvedMetadata = await metadata.promise;
18
+ if (!relIds[key]) {
19
+ relIds[key] = {
20
+ key: resolvedMetadata.key,
21
+ required: resolvedMetadata.required,
22
+ model: resolvedMetadata.relatedTo,
23
+ hydrationField: resolvedMetadata.relationField,
24
+ foreignKey: resolvedMetadata.relatedToField
25
+ };
26
+ }
27
+ }
28
+ }
29
+
30
+ return relIds;
31
+ }
32
+
33
+ static async getRelationManyMeta(model: RWSModel<any>, classFields: string[]): Promise<RelManyMetaType<IRWSModel>> {
34
+ const relIds: RelManyMetaType<IRWSModel> = {};
35
+
36
+ const inverseFields = classFields
37
+ .filter((item: string) => item.indexOf('InverseRelation') === 0)
38
+ .map((item: string) => item.split(':').at(-1));
39
+
40
+ for (const key of inverseFields) {
41
+ const metadataKey = `InverseRelation:${key}`;
42
+ const metadata = Reflect.getMetadata(metadataKey, model);
43
+
44
+ if (metadata && metadata.promise) {
45
+ const resolvedMetadata = await metadata.promise;
46
+ if (!relIds[key]) {
47
+ relIds[key] = {
48
+ key: resolvedMetadata.key,
49
+ inversionModel: resolvedMetadata.inversionModel,
50
+ foreignKey: resolvedMetadata.foreignKey
51
+ };
52
+ }
53
+ }
54
+ }
55
+
56
+ return relIds;
57
+ }
58
+
59
+ static bindRelation(relatedModel: RWSModel<any>): { connect: { id: string } } {
60
+ return {
61
+ connect: {
62
+ id: relatedModel.id
63
+ }
64
+ };
65
+ }
66
+
67
+ static hasRelation(model: RWSModel<any>, key: string): boolean {
68
+ // Check if the property exists and is an object with an id property
69
+ return !!model[key] && typeof model[key] === 'object' && model[key] !== null && 'id' in model[key];
70
+ }
71
+
72
+ static checkRelEnabled(model: RWSModel<any>, key: string): boolean {
73
+ return Object.keys((model.constructor as any)._RELATIONS).includes(key) &&
74
+ (model.constructor as any)._RELATIONS[key] === true;
75
+ }
76
+ }