@rws-framework/db 3.6.1 → 3.7.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.
@@ -24,7 +24,8 @@ 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
+ protected getRelationKey(key: string): Promise<string>;
28
29
  protected bindRelation(key: string, relatedModel: RWSModel<any>): {
29
30
  connect: {
30
31
  id: string | number;
@@ -86,8 +86,11 @@ 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
+ }
92
+ async getRelationKey(key) {
93
+ return RelationUtils_1.RelationUtils.getRelationKey(this.constructor, key);
91
94
  }
92
95
  bindRelation(key, relatedModel) {
93
96
  return RelationUtils_1.RelationUtils.bindRelation(relatedModel);
@@ -138,8 +141,15 @@ class RWSModel {
138
141
  const timeSeriesIds = TimeSeriesUtils_1.TimeSeriesUtils.getTimeSeriesModelFields(this);
139
142
  const timeSeriesHydrationFields = [];
140
143
  for (const key in this) {
141
- if (this.hasRelation(key)) {
144
+ if (await this.hasRelation(key)) {
142
145
  data[key] = this.bindRelation(key, this[key]);
146
+ if (data[key] === null) {
147
+ const relationKey = await this.getRelationKey(key);
148
+ if (relationKey) {
149
+ data[relationKey] = null;
150
+ delete data[key];
151
+ }
152
+ }
143
153
  continue;
144
154
  }
145
155
  if (!(await this.isDbVariable(key))) {
@@ -8,7 +8,8 @@ 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
+ static getRelationKey(constructor: any, variable: string): Promise<string | null>;
13
14
  static checkRelDisabled(model: RWSModel<any>, key: string): boolean;
14
15
  }
@@ -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,34 @@ class RelationUtils {
48
49
  return relIds;
49
50
  }
50
51
  static bindRelation(relatedModel) {
52
+ if (!relatedModel || !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);
68
+ }
69
+ static async getRelationKey(constructor, variable) {
70
+ const dbAnnotations = await ModelUtils_1.ModelUtils.getModelAnnotations(constructor);
71
+ const relationMeta = Object.keys(dbAnnotations)
72
+ .map((key) => { return { ...dbAnnotations[key], key }; })
73
+ .filter((element) => element.annotationType === 'Relation')
74
+ .find((element) => element.key === variable);
75
+ ;
76
+ if (!relationMeta) {
77
+ return null;
78
+ }
79
+ return relationMeta.metadata.relationField;
60
80
  }
61
81
  static checkRelDisabled(model, key) {
62
82
  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.1",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -101,10 +101,14 @@ 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
+ protected async getRelationKey(key: string): Promise<string> {
109
+ return RelationUtils.getRelationKey((this as any).constructor, key);
110
+ }
111
+
108
112
  protected bindRelation(key: string, relatedModel: RWSModel<any>): { connect: { id: string | number } } {
109
113
  return RelationUtils.bindRelation(relatedModel);
110
114
  }
@@ -177,8 +181,16 @@ class RWSModel<T> implements IModel {
177
181
  const timeSeriesHydrationFields: string[] = [];
178
182
 
179
183
  for (const key in (this as any)) {
180
- if (this.hasRelation(key)) {
181
- data[key] = this.bindRelation(key, this[key]);
184
+ if (await this.hasRelation(key)) {
185
+ data[key] = this.bindRelation(key, this[key]);
186
+
187
+ if(data[key] === null){
188
+ const relationKey = await this.getRelationKey(key);
189
+ if(relationKey){
190
+ data[relationKey] = null;
191
+ delete data[key];
192
+ }
193
+ }
182
194
  continue;
183
195
  }
184
196
 
@@ -192,6 +204,7 @@ class RWSModel<T> implements IModel {
192
204
  ).includes(key) &&
193
205
  !timeSeriesHydrationFields.includes(key)
194
206
  ;
207
+
195
208
 
196
209
  if (passedFieldCondition) {
197
210
  data[key] = this[key];
@@ -201,8 +214,8 @@ class RWSModel<T> implements IModel {
201
214
  data[key] = this[key];
202
215
  timeSeriesHydrationFields.push(timeSeriesIds[key].hydrationField);
203
216
  }
204
- }
205
-
217
+ }
218
+
206
219
  return data;
207
220
  }
208
221
 
@@ -1,6 +1,8 @@
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';
5
+ import { IRelationOpts } from 'src/decorators/Relation';
4
6
 
5
7
  export class RelationUtils {
6
8
  static async getRelationOneMeta(model: RWSModel<any>, classFields: string[]): Promise<RelOneMetaType<IRWSModel>> {
@@ -57,7 +59,11 @@ export class RelationUtils {
57
59
  return relIds;
58
60
  }
59
61
 
60
- static bindRelation(relatedModel: RWSModel<any>): { connect: { id: string | number } } {
62
+ static bindRelation(relatedModel: RWSModel<any>): { connect: { id: string | number } } | null {
63
+ if(!relatedModel || !relatedModel.id){
64
+ return null;
65
+ }
66
+
61
67
  return {
62
68
  connect: {
63
69
  id: relatedModel.id
@@ -65,9 +71,34 @@ export class RelationUtils {
65
71
  };
66
72
  }
67
73
 
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];
74
+ static async hasRelation(constructor: any, variable: string): Promise<boolean> {
75
+ const dbAnnotations = await ModelUtils.getModelAnnotations(constructor);
76
+ type AnnotationType = { annotationType: string, key: string };
77
+
78
+ const dbProperties: string[] = Object.keys(dbAnnotations)
79
+ .map((key: string): AnnotationType => {return {...dbAnnotations[key], key};})
80
+ .filter((element: AnnotationType) => element.annotationType === 'Relation' )
81
+ .map((element: AnnotationType) => element.key);
82
+
83
+ return dbProperties.includes(variable);
84
+ }
85
+
86
+ static async getRelationKey(constructor: any, variable: string): Promise<string | null> {
87
+ const dbAnnotations = await ModelUtils.getModelAnnotations(constructor);
88
+ type AnnotationType = { annotationType: string, key: string };
89
+
90
+ const relationMeta = Object.keys(dbAnnotations)
91
+ .map((key: string) => {return {...dbAnnotations[key], key};})
92
+ .filter((element: AnnotationType) => element.annotationType === 'Relation' )
93
+ .find((element: AnnotationType) => element.key === variable) as unknown as { metadata: IRelationOpts };
94
+ ;
95
+
96
+
97
+ if(!relationMeta){
98
+ return null;
99
+ }
100
+
101
+ return relationMeta.metadata.relationField;
71
102
  }
72
103
 
73
104
  static checkRelDisabled(model: RWSModel<any>, key: string): boolean {