@rws-framework/db 3.7.0 → 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.
@@ -25,6 +25,7 @@ declare class RWSModel<T> implements IModel {
25
25
  static checkForInclusion(this: OpModelType<any>, checkModelType: string): boolean;
26
26
  protected _fill(data: any): RWSModel<T>;
27
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;
@@ -89,6 +89,9 @@ class RWSModel {
89
89
  async hasRelation(key) {
90
90
  return RelationUtils_1.RelationUtils.hasRelation(this.constructor, key);
91
91
  }
92
+ async getRelationKey(key) {
93
+ return RelationUtils_1.RelationUtils.getRelationKey(this.constructor, key);
94
+ }
92
95
  bindRelation(key, relatedModel) {
93
96
  return RelationUtils_1.RelationUtils.bindRelation(relatedModel);
94
97
  }
@@ -140,6 +143,13 @@ class RWSModel {
140
143
  for (const key in this) {
141
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))) {
@@ -10,5 +10,6 @@ export declare class RelationUtils {
10
10
  };
11
11
  } | null;
12
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
  }
@@ -49,7 +49,7 @@ class RelationUtils {
49
49
  return relIds;
50
50
  }
51
51
  static bindRelation(relatedModel) {
52
- if (!relatedModel.id) {
52
+ if (!relatedModel || !relatedModel.id) {
53
53
  return null;
54
54
  }
55
55
  return {
@@ -66,6 +66,18 @@ class RelationUtils {
66
66
  .map((element) => element.key);
67
67
  return dbProperties.includes(variable);
68
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;
80
+ }
69
81
  static checkRelDisabled(model, key) {
70
82
  const constructor = model.constructor;
71
83
  let declaredRelations = [];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rws-framework/db",
3
3
  "private": false,
4
- "version": "3.7.0",
4
+ "version": "3.7.1",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -105,6 +105,10 @@ class RWSModel<T> implements IModel {
105
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
  }
@@ -178,7 +182,15 @@ class RWSModel<T> implements IModel {
178
182
 
179
183
  for (const key in (this as any)) {
180
184
  if (await this.hasRelation(key)) {
181
- data[key] = this.bindRelation(key, this[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
 
@@ -2,6 +2,7 @@ import { RelOneMetaType, RelManyMetaType } from '../types/RelationTypes';
2
2
  import { IRWSModel } from '../../types/IRWSModel';
3
3
  import { OpModelType, RWSModel } from '../_model';
4
4
  import { ModelUtils } from './ModelUtils';
5
+ import { IRelationOpts } from 'src/decorators/Relation';
5
6
 
6
7
  export class RelationUtils {
7
8
  static async getRelationOneMeta(model: RWSModel<any>, classFields: string[]): Promise<RelOneMetaType<IRWSModel>> {
@@ -58,8 +59,8 @@ export class RelationUtils {
58
59
  return relIds;
59
60
  }
60
61
 
61
- static bindRelation(relatedModel: RWSModel<any>): { connect: { id: string | number } } | null {
62
- if(!relatedModel.id){
62
+ static bindRelation(relatedModel: RWSModel<any>): { connect: { id: string | number } } | null {
63
+ if(!relatedModel || !relatedModel.id){
63
64
  return null;
64
65
  }
65
66
 
@@ -82,6 +83,24 @@ export class RelationUtils {
82
83
  return dbProperties.includes(variable);
83
84
  }
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;
102
+ }
103
+
85
104
  static checkRelDisabled(model: RWSModel<any>, key: string): boolean {
86
105
  const constructor = model.constructor as OpModelType<any>;
87
106