@rws-framework/db 3.6.0 → 3.7.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.
@@ -24,7 +24,7 @@ declare class RWSModel<T> implements IModel {
24
24
  checkForInclusion(): boolean;
25
25
  static checkForInclusion(this: OpModelType<any>, checkModelType: string): boolean;
26
26
  protected _fill(data: any): RWSModel<T>;
27
- protected hasRelation(key: string): boolean;
27
+ protected hasRelation(key: string): Promise<boolean>;
28
28
  protected bindRelation(key: string, relatedModel: RWSModel<any>): {
29
29
  connect: {
30
30
  id: string | number;
@@ -86,8 +86,8 @@ class RWSModel {
86
86
  }
87
87
  return this;
88
88
  }
89
- hasRelation(key) {
90
- return RelationUtils_1.RelationUtils.hasRelation(this, key);
89
+ async hasRelation(key) {
90
+ return RelationUtils_1.RelationUtils.hasRelation(this.constructor, key);
91
91
  }
92
92
  bindRelation(key, relatedModel) {
93
93
  return RelationUtils_1.RelationUtils.bindRelation(relatedModel);
@@ -138,7 +138,7 @@ class RWSModel {
138
138
  const timeSeriesIds = TimeSeriesUtils_1.TimeSeriesUtils.getTimeSeriesModelFields(this);
139
139
  const timeSeriesHydrationFields = [];
140
140
  for (const key in this) {
141
- if (this.hasRelation(key)) {
141
+ if (await this.hasRelation(key)) {
142
142
  data[key] = this.bindRelation(key, this[key]);
143
143
  continue;
144
144
  }
@@ -13,7 +13,7 @@ class FindUtils {
13
13
  const dbData = await opModel.services.dbService.findOneBy(collection, conditions, fields, ordering);
14
14
  if (dbData) {
15
15
  const inst = new opModel();
16
- const loaded = await inst._asyncFill(dbData, fullData, allowRelations, findParams.cancelPostLoad ? false : true);
16
+ const loaded = await inst._asyncFill(dbData, fullData, allowRelations, findParams?.cancelPostLoad ? false : true);
17
17
  return loaded;
18
18
  }
19
19
  return null;
@@ -28,7 +28,7 @@ class FindUtils {
28
28
  const dbData = await opModel.services.dbService.findOneBy(collection, { id }, fields, ordering);
29
29
  if (dbData) {
30
30
  const inst = new opModel();
31
- const loaded = await inst._asyncFill(dbData, fullData, allowRelations, findParams.cancelPostLoad ? false : true);
31
+ const loaded = await inst._asyncFill(dbData, fullData, allowRelations, findParams?.cancelPostLoad ? false : true);
32
32
  return loaded;
33
33
  }
34
34
  return null;
@@ -48,7 +48,7 @@ class FindUtils {
48
48
  const instanced = [];
49
49
  for (const data of dbData) {
50
50
  const inst = new opModel();
51
- instanced.push((await inst._asyncFill(data, fullData, allowRelations, findParams.cancelPostLoad ? false : true)));
51
+ instanced.push((await inst._asyncFill(data, fullData, allowRelations, findParams?.cancelPostLoad ? false : true)));
52
52
  }
53
53
  return instanced;
54
54
  }
@@ -73,7 +73,7 @@ class FindUtils {
73
73
  const instanced = [];
74
74
  for (const data of dbData) {
75
75
  const inst = new opModel();
76
- instanced.push((await inst._asyncFill(data, fullData, allowRelations, findParams.cancelPostLoad ? false : true)));
76
+ instanced.push((await inst._asyncFill(data, fullData, allowRelations, findParams?.cancelPostLoad ? false : true)));
77
77
  }
78
78
  return instanced;
79
79
  }
@@ -8,7 +8,7 @@ export declare class RelationUtils {
8
8
  connect: {
9
9
  id: string | number;
10
10
  };
11
- };
12
- static hasRelation(model: RWSModel<any>, key: string): boolean;
11
+ } | null;
12
+ static hasRelation(constructor: any, variable: string): Promise<boolean>;
13
13
  static checkRelDisabled(model: RWSModel<any>, key: string): boolean;
14
14
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RelationUtils = void 0;
4
+ const ModelUtils_1 = require("./ModelUtils");
4
5
  class RelationUtils {
5
6
  static async getRelationOneMeta(model, classFields) {
6
7
  const relIds = {};
@@ -48,15 +49,22 @@ class RelationUtils {
48
49
  return relIds;
49
50
  }
50
51
  static bindRelation(relatedModel) {
52
+ if (!relatedModel.id) {
53
+ return null;
54
+ }
51
55
  return {
52
56
  connect: {
53
57
  id: relatedModel.id
54
58
  }
55
59
  };
56
60
  }
57
- static hasRelation(model, key) {
58
- // Check if the property exists and is an object with an id property
59
- return !!model[key] && typeof model[key] === 'object' && model[key] !== null && 'id' in model[key];
61
+ static async hasRelation(constructor, variable) {
62
+ const dbAnnotations = await ModelUtils_1.ModelUtils.getModelAnnotations(constructor);
63
+ const dbProperties = Object.keys(dbAnnotations)
64
+ .map((key) => { return { ...dbAnnotations[key], key }; })
65
+ .filter((element) => element.annotationType === 'Relation')
66
+ .map((element) => element.key);
67
+ return dbProperties.includes(variable);
60
68
  }
61
69
  static checkRelDisabled(model, key) {
62
70
  const constructor = model.constructor;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rws-framework/db",
3
3
  "private": false,
4
- "version": "3.6.0",
4
+ "version": "3.7.0",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -101,8 +101,8 @@ class RWSModel<T> implements IModel {
101
101
  return this;
102
102
  }
103
103
 
104
- protected hasRelation(key: string): boolean {
105
- return RelationUtils.hasRelation(this, key);
104
+ protected async hasRelation(key: string): Promise<boolean> {
105
+ return RelationUtils.hasRelation((this as any).constructor, key);
106
106
  }
107
107
 
108
108
  protected bindRelation(key: string, relatedModel: RWSModel<any>): { connect: { id: string | number } } {
@@ -177,7 +177,7 @@ class RWSModel<T> implements IModel {
177
177
  const timeSeriesHydrationFields: string[] = [];
178
178
 
179
179
  for (const key in (this as any)) {
180
- if (this.hasRelation(key)) {
180
+ if (await this.hasRelation(key)) {
181
181
  data[key] = this.bindRelation(key, this[key]);
182
182
  continue;
183
183
  }
@@ -192,6 +192,7 @@ class RWSModel<T> implements IModel {
192
192
  ).includes(key) &&
193
193
  !timeSeriesHydrationFields.includes(key)
194
194
  ;
195
+
195
196
 
196
197
  if (passedFieldCondition) {
197
198
  data[key] = this[key];
@@ -201,8 +202,8 @@ class RWSModel<T> implements IModel {
201
202
  data[key] = this[key];
202
203
  timeSeriesHydrationFields.push(timeSeriesIds[key].hydrationField);
203
204
  }
204
- }
205
-
205
+ }
206
+
206
207
  return data;
207
208
  }
208
209
 
@@ -27,7 +27,7 @@ export class FindUtils {
27
27
 
28
28
  if (dbData) {
29
29
  const inst: T = new (opModel as { new(): T })();
30
- const loaded = await inst._asyncFill(dbData, fullData, allowRelations, findParams.cancelPostLoad ? false : true);
30
+ const loaded = await inst._asyncFill(dbData, fullData, allowRelations, findParams?.cancelPostLoad ? false : true);
31
31
  return loaded as T;
32
32
  }
33
33
 
@@ -51,7 +51,7 @@ export class FindUtils {
51
51
 
52
52
  if (dbData) {
53
53
  const inst: T = new (opModel as { new(): T })();
54
- const loaded = await inst._asyncFill(dbData, fullData, allowRelations, findParams.cancelPostLoad ? false : true);
54
+ const loaded = await inst._asyncFill(dbData, fullData, allowRelations, findParams?.cancelPostLoad ? false : true);
55
55
  return loaded as T;
56
56
  }
57
57
 
@@ -80,7 +80,7 @@ export class FindUtils {
80
80
  for (const data of dbData) {
81
81
  const inst: T = new (opModel as { new(): T })();
82
82
 
83
- instanced.push((await inst._asyncFill(data, fullData, allowRelations, findParams.cancelPostLoad ? false : true)) as T);
83
+ instanced.push((await inst._asyncFill(data, fullData, allowRelations, findParams?.cancelPostLoad ? false : true)) as T);
84
84
  }
85
85
 
86
86
  return instanced;
@@ -114,7 +114,7 @@ export class FindUtils {
114
114
 
115
115
  for (const data of dbData) {
116
116
  const inst: T = new (opModel as { new(): T })();
117
- instanced.push((await inst._asyncFill(data, fullData, allowRelations, findParams.cancelPostLoad ? false : true)) as T);
117
+ instanced.push((await inst._asyncFill(data, fullData, allowRelations, findParams?.cancelPostLoad ? false : true)) as T);
118
118
  }
119
119
 
120
120
  return instanced;
@@ -1,6 +1,7 @@
1
1
  import { RelOneMetaType, RelManyMetaType } from '../types/RelationTypes';
2
2
  import { IRWSModel } from '../../types/IRWSModel';
3
3
  import { OpModelType, RWSModel } from '../_model';
4
+ import { ModelUtils } from './ModelUtils';
4
5
 
5
6
  export class RelationUtils {
6
7
  static async getRelationOneMeta(model: RWSModel<any>, classFields: string[]): Promise<RelOneMetaType<IRWSModel>> {
@@ -57,7 +58,11 @@ export class RelationUtils {
57
58
  return relIds;
58
59
  }
59
60
 
60
- static bindRelation(relatedModel: RWSModel<any>): { connect: { id: string | number } } {
61
+ static bindRelation(relatedModel: RWSModel<any>): { connect: { id: string | number } } | null {
62
+ if(!relatedModel.id){
63
+ return null;
64
+ }
65
+
61
66
  return {
62
67
  connect: {
63
68
  id: relatedModel.id
@@ -65,9 +70,16 @@ export class RelationUtils {
65
70
  };
66
71
  }
67
72
 
68
- static hasRelation(model: RWSModel<any>, key: string): boolean {
69
- // Check if the property exists and is an object with an id property
70
- return !!model[key] && typeof model[key] === 'object' && model[key] !== null && 'id' in model[key];
73
+ static async hasRelation(constructor: any, variable: string): Promise<boolean> {
74
+ const dbAnnotations = await ModelUtils.getModelAnnotations(constructor);
75
+ type AnnotationType = { annotationType: string, key: string };
76
+
77
+ const dbProperties: string[] = Object.keys(dbAnnotations)
78
+ .map((key: string): AnnotationType => {return {...dbAnnotations[key], key};})
79
+ .filter((element: AnnotationType) => element.annotationType === 'Relation' )
80
+ .map((element: AnnotationType) => element.key);
81
+
82
+ return dbProperties.includes(variable);
71
83
  }
72
84
 
73
85
  static checkRelDisabled(model: RWSModel<any>, key: string): boolean {