@rws-framework/db 3.6.1 → 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
  }
@@ -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.1",
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
 
@@ -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 {