@rws-framework/db 3.3.0 → 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.
- package/dist/helper/DbHelper.js +1 -1
- package/dist/helper/db/schema-generator.js +7 -1
- package/dist/helper/db/utils.d.ts +1 -0
- package/dist/helper/db/utils.js +5 -0
- package/dist/models/utils/RelationUtils.js +17 -2
- package/package.json +1 -1
- package/src/helper/DbHelper.ts +1 -1
- package/src/helper/db/schema-generator.ts +9 -2
- package/src/helper/db/utils.ts +7 -0
- package/src/models/utils/HydrateUtils.ts +3 -0
- package/src/models/utils/RelationUtils.ts +24 -3
package/dist/helper/DbHelper.js
CHANGED
|
@@ -34,7 +34,7 @@ class DbHelper {
|
|
|
34
34
|
}
|
|
35
35
|
static async migrateDBModels(configService, dbService, leaveFile = false) {
|
|
36
36
|
process.env = { ...process.env, [this.dbUrlVarName]: configService.get('db_url') };
|
|
37
|
-
const [_, schemaPath] = db_1.DbUtils.
|
|
37
|
+
const [_, schemaPath] = db_1.DbUtils.getProcessedSchemaDir();
|
|
38
38
|
await console_1.rwsShell.runCommand(`${db_1.DbUtils.detectInstaller()} prisma migrate dev --create-only --schema=${schemaPath}`, process.cwd());
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
@@ -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`;
|
|
@@ -298,7 +304,7 @@ datasource db {
|
|
|
298
304
|
*/
|
|
299
305
|
static async pushDBModels(configService, dbService, leaveFile = false) {
|
|
300
306
|
process.env = { ...process.env, [this.dbUrlVarName]: configService.get('db_url') };
|
|
301
|
-
const [_, schemaPath] = utils_1.DbUtils.
|
|
307
|
+
const [_, schemaPath] = utils_1.DbUtils.getProcessedSchemaDir();
|
|
302
308
|
await console_1.rwsShell.runCommand(`${utils_1.DbUtils.detectInstaller()} prisma db push --schema=${schemaPath}`, process.cwd());
|
|
303
309
|
}
|
|
304
310
|
}
|
package/dist/helper/db/utils.js
CHANGED
|
@@ -22,6 +22,11 @@ class DbUtils {
|
|
|
22
22
|
const schemaPath = path_1.default.join(schemaDir, 'schema.prisma');
|
|
23
23
|
return [schemaDir, schemaPath];
|
|
24
24
|
}
|
|
25
|
+
static getProcessedSchemaDir() {
|
|
26
|
+
const schemaDir = path_1.default.join(workspaceRoot, 'node_modules', '.prisma', 'client');
|
|
27
|
+
const schemaPath = path_1.default.join(schemaDir, 'schema.prisma');
|
|
28
|
+
return [schemaDir, schemaPath];
|
|
29
|
+
}
|
|
25
30
|
/**
|
|
26
31
|
* Detect the package installer (yarn or npx)
|
|
27
32
|
*/
|
|
@@ -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
|
-
|
|
62
|
-
|
|
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
package/src/helper/DbHelper.ts
CHANGED
|
@@ -46,7 +46,7 @@ export class DbHelper {
|
|
|
46
46
|
static async migrateDBModels(configService: IDbConfigHandler, dbService: DBService, leaveFile = false): Promise<void> {
|
|
47
47
|
process.env = { ...process.env, [this.dbUrlVarName]: configService.get('db_url') };
|
|
48
48
|
|
|
49
|
-
const [_, schemaPath] = DbUtils.
|
|
49
|
+
const [_, schemaPath] = DbUtils.getProcessedSchemaDir();
|
|
50
50
|
|
|
51
51
|
await rwsShell.runCommand(`${DbUtils.detectInstaller()} prisma migrate dev --create-only --schema=${schemaPath}`, process.cwd());
|
|
52
52
|
}
|
|
@@ -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') {
|
|
@@ -383,7 +390,7 @@ datasource db {
|
|
|
383
390
|
static async pushDBModels(configService: IDbConfigHandler, dbService: DBService, leaveFile = false): Promise<void> {
|
|
384
391
|
process.env = { ...process.env, [this.dbUrlVarName]: configService.get('db_url') };
|
|
385
392
|
|
|
386
|
-
const [_, schemaPath] = DbUtils.
|
|
393
|
+
const [_, schemaPath] = DbUtils.getProcessedSchemaDir();
|
|
387
394
|
|
|
388
395
|
await rwsShell.runCommand(`${DbUtils.detectInstaller()} prisma db push --schema=${schemaPath}`, process.cwd());
|
|
389
396
|
}
|
package/src/helper/db/utils.ts
CHANGED
|
@@ -24,6 +24,13 @@ export class DbUtils {
|
|
|
24
24
|
return [schemaDir, schemaPath];
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
static getProcessedSchemaDir(): [string, string] {
|
|
28
|
+
const schemaDir = path.join(workspaceRoot, 'node_modules','.prisma', 'client');
|
|
29
|
+
const schemaPath = path.join(schemaDir, 'schema.prisma');
|
|
30
|
+
|
|
31
|
+
return [schemaDir, schemaPath];
|
|
32
|
+
}
|
|
33
|
+
|
|
27
34
|
/**
|
|
28
35
|
* Detect the package installer (yarn or npx)
|
|
29
36
|
*/
|
|
@@ -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
|
-
|
|
74
|
-
|
|
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
|
}
|