@rws-framework/db 3.3.1 → 3.3.2

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.
@@ -109,9 +109,15 @@ datasource db {
109
109
  requiredString = '';
110
110
  }
111
111
  let relatedFieldType = type_converter_1.TypeConverter.toConfigCase(relatedFieldMeta.metadata, dbType, true);
112
+ /**
113
+ * @todo: Detect type
114
+ */
112
115
  if (relatedToField === 'id' && dbType !== 'mongodb') {
113
116
  relatedFieldType = 'Int';
114
117
  }
118
+ if (relationMeta.required === false) {
119
+ requiredString = '?';
120
+ }
115
121
  // Add relation field with appropriate type based on database
116
122
  if (dbType === 'mongodb') {
117
123
  section += `\t${relationFieldName} String${requiredString} @db.ObjectId\n`;
@@ -58,8 +58,23 @@ class RelationUtils {
58
58
  return !!model[key] && typeof model[key] === 'object' && model[key] !== null && 'id' in model[key];
59
59
  }
60
60
  static checkRelDisabled(model, key) {
61
- return Object.keys(model.constructor._RELATIONS).includes(key) &&
62
- model.constructor._RELATIONS[key] === false;
61
+ const constructor = model.constructor;
62
+ let declaredRelations = [];
63
+ for (const relKey in constructor._RELATIONS) {
64
+ const relEntry = constructor._RELATIONS[relKey];
65
+ if (relEntry === true) {
66
+ declaredRelations.push(relKey);
67
+ }
68
+ }
69
+ // if((model.constructor as OpModelType<any>)._collection === 'product'){
70
+ // console.log({key, declaredRelations});
71
+ // }
72
+ // A relation disabled through declared relations
73
+ if (declaredRelations.length && !declaredRelations.includes(key)) {
74
+ return true;
75
+ }
76
+ // A relation disabled directly
77
+ return Object.keys(constructor._RELATIONS).includes(key) && constructor._RELATIONS[key] === false;
63
78
  }
64
79
  }
65
80
  exports.RelationUtils = RelationUtils;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rws-framework/db",
3
3
  "private": false,
4
- "version": "3.3.1",
4
+ "version": "3.3.2",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -146,9 +146,16 @@ datasource db {
146
146
 
147
147
  let relatedFieldType = TypeConverter.toConfigCase(relatedFieldMeta.metadata, dbType, true);
148
148
 
149
+ /**
150
+ * @todo: Detect type
151
+ */
149
152
  if(relatedToField === 'id' && dbType !== 'mongodb'){
150
153
  relatedFieldType = 'Int';
151
- }
154
+ }
155
+
156
+ if(relationMeta.required === false){
157
+ requiredString = '?';
158
+ }
152
159
 
153
160
  // Add relation field with appropriate type based on database
154
161
  if (dbType === 'mongodb') {
@@ -55,6 +55,9 @@ export class HydrateUtils {
55
55
  const relMeta = relManyData[key];
56
56
 
57
57
  const relationEnabled = !RelationUtils.checkRelDisabled(model, relMeta.key);
58
+
59
+
60
+
58
61
  if (relationEnabled) {
59
62
  model[relMeta.key] = await relMeta.inversionModel.findBy({
60
63
  conditions: {
@@ -1,6 +1,6 @@
1
1
  import { RelOneMetaType, RelManyMetaType } from '../types/RelationTypes';
2
2
  import { IRWSModel } from '../../types/IRWSModel';
3
- import { RWSModel } from '../_model';
3
+ import { OpModelType, RWSModel } from '../_model';
4
4
 
5
5
  export class RelationUtils {
6
6
  static async getRelationOneMeta(model: RWSModel<any>, classFields: string[]): Promise<RelOneMetaType<IRWSModel>> {
@@ -70,7 +70,28 @@ export class RelationUtils {
70
70
  }
71
71
 
72
72
  static checkRelDisabled(model: RWSModel<any>, key: string): boolean {
73
- return Object.keys((model.constructor as any)._RELATIONS).includes(key) &&
74
- (model.constructor as any)._RELATIONS[key] === false;
73
+ const constructor = model.constructor as OpModelType<any>;
74
+
75
+ let declaredRelations: string[] = [];
76
+
77
+ for(const relKey in constructor._RELATIONS){
78
+ const relEntry = constructor._RELATIONS[relKey];
79
+
80
+ if(relEntry === true){
81
+ declaredRelations.push(relKey);
82
+ }
83
+ }
84
+
85
+ // if((model.constructor as OpModelType<any>)._collection === 'product'){
86
+ // console.log({key, declaredRelations});
87
+ // }
88
+
89
+ // A relation disabled through declared relations
90
+ if(declaredRelations.length && !declaredRelations.includes(key)){
91
+ return true;
92
+ }
93
+
94
+ // A relation disabled directly
95
+ return Object.keys(constructor._RELATIONS).includes(key) && constructor._RELATIONS[key] === false;
75
96
  }
76
97
  }