@rws-framework/db 3.1.0 → 3.3.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.
- package/dist/decorators/IdType.js +6 -0
- package/dist/decorators/InverseRelation.js +3 -4
- package/dist/decorators/Relation.js +4 -3
- package/dist/decorators/TrackType.js +2 -2
- package/dist/helper/DbHelper.d.ts +1 -1
- package/dist/helper/DbHelper.js +4 -4
- package/dist/helper/db/relation-manager.js +1 -1
- package/dist/helper/db/schema-generator.js +73 -26
- package/dist/helper/db/type-converter.d.ts +2 -1
- package/dist/helper/db/type-converter.js +33 -9
- package/dist/helper/db/utils.d.ts +7 -0
- package/dist/helper/db/utils.js +80 -26
- package/dist/models/core/RWSModel.js +29 -32
- package/dist/models/interfaces/IDbOpts.d.ts +3 -0
- package/dist/models/interfaces/IIdTypeOpts.d.ts +2 -0
- package/dist/models/interfaces/ITrackerOpts.d.ts +1 -0
- package/dist/models/utils/HydrateUtils.js +0 -1
- package/dist/models/utils/ModelUtils.js +8 -1
- package/dist/models/utils/PaginationUtils.js +5 -6
- package/dist/services/DBService.js +7 -8
- package/exec/console.js +0 -2
- package/package.json +1 -1
- package/src/decorators/IdType.ts +10 -1
- package/src/decorators/InverseRelation.ts +2 -2
- package/src/decorators/Relation.ts +6 -4
- package/src/helper/DbHelper.ts +1 -1
- package/src/helper/db/schema-generator.ts +104 -36
- package/src/helper/db/type-converter.ts +41 -8
- package/src/helper/db/utils.ts +114 -40
- package/src/models/interfaces/IDbOpts.ts +3 -0
- package/src/models/interfaces/IIdTypeOpts.ts +2 -0
- package/src/models/interfaces/ITrackerOpts.ts +1 -0
- package/src/models/utils/HydrateUtils.ts +0 -2
- package/src/models/utils/ModelUtils.ts +10 -1
- package/tsconfig.json +1 -1
package/src/helper/db/utils.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { IDbConfigParams } from '../../types/DbConfigHandler';
|
|
|
5
5
|
import { IIdMetaOpts, IIdTypeOpts } from '../../decorators/IdType';
|
|
6
6
|
import { TypeConverter } from './type-converter';
|
|
7
7
|
import { IDbOpts } from '../../models/interfaces/IDbOpts';
|
|
8
|
+
import { ITrackerMetaOpts } from '../../models/_model';
|
|
8
9
|
|
|
9
10
|
const workspaceRoot = rwsPath.findRootWorkspacePath();
|
|
10
11
|
const moduleDir = path.resolve(workspaceRoot, 'node_modules', '@rws-framework', 'db');
|
|
@@ -41,78 +42,151 @@ export class DbUtils {
|
|
|
41
42
|
dbType: IDbConfigParams['db_type'],
|
|
42
43
|
modelMeta: Record<string, { annotationType: string, metadata: IIdMetaOpts }>,
|
|
43
44
|
optional = false
|
|
44
|
-
): string {
|
|
45
|
-
let useUuid =
|
|
45
|
+
): string {
|
|
46
|
+
let useUuid = this.doesUseUuid(modelMeta);
|
|
46
47
|
let field = 'id';
|
|
47
48
|
const tags: string[] = [];
|
|
48
49
|
|
|
49
50
|
for (const key in modelMeta) {
|
|
50
|
-
const modelMetadata: IIdMetaOpts = modelMeta[key].metadata;
|
|
51
|
-
const annotationType: string = modelMeta[key].annotationType;
|
|
51
|
+
const modelMetadata: IIdMetaOpts = modelMeta[key].metadata;
|
|
52
|
+
const annotationType: string = modelMeta[key].annotationType;
|
|
52
53
|
|
|
53
|
-
if(key !== 'id'){
|
|
54
|
-
if(annotationType == 'IdType'){
|
|
54
|
+
if (key !== 'id') {
|
|
55
|
+
if (annotationType == 'IdType') {
|
|
55
56
|
const dbSpecificTags = TypeConverter.processTypeOptions({ tags: [], dbOptions: modelMetadata.dbOptions }, dbType);
|
|
56
|
-
tags.push(...dbSpecificTags);
|
|
57
|
-
|
|
58
|
-
field = key;
|
|
57
|
+
tags.push(...dbSpecificTags);
|
|
58
|
+
|
|
59
|
+
field = key;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const idPrismaType = this.getDefaultPrismaType(dbType, useUuid);
|
|
65
|
+
|
|
66
|
+
let reqStr = '';
|
|
67
|
+
|
|
68
|
+
if (optional) {
|
|
69
|
+
reqStr = '?';
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let idString: string = `${field} ${idPrismaType}${reqStr}`;
|
|
73
|
+
|
|
74
|
+
idString += this.addIdPart(dbType, useUuid, modelMeta[field].metadata.noAuto);
|
|
75
|
+
|
|
76
|
+
if(dbType === 'mongodb'){
|
|
77
|
+
tags.push('@map("_id")');
|
|
78
|
+
tags.push('@db.ObjectId');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (tags.length) {
|
|
82
|
+
idString += ' ' + tags.join(' ');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (!idString) {
|
|
86
|
+
throw new Error(`DB type "${dbType}" is not supported!`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
return idString;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
static getDefaultPrismaType(dbType: IDbConfigParams['db_type'], useUuid: boolean): string
|
|
94
|
+
{
|
|
95
|
+
let idPrismaType = 'String';
|
|
96
|
+
|
|
97
|
+
switch (dbType) {
|
|
98
|
+
case 'mysql':
|
|
99
|
+
if (useUuid) {
|
|
100
|
+
idPrismaType = 'String';
|
|
101
|
+
} else {
|
|
102
|
+
idPrismaType = 'Int';
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
break;
|
|
106
|
+
|
|
107
|
+
case 'postgresql':
|
|
108
|
+
case 'postgres':
|
|
109
|
+
if (useUuid) {
|
|
110
|
+
idPrismaType = 'String';
|
|
111
|
+
} else {
|
|
112
|
+
idPrismaType = 'Int';
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
break;
|
|
116
|
+
|
|
117
|
+
case 'sqlite':
|
|
118
|
+
if (useUuid) {
|
|
119
|
+
idPrismaType = 'String';
|
|
120
|
+
} else {
|
|
121
|
+
idPrismaType = 'Int';
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return idPrismaType;
|
|
128
|
+
}
|
|
59
129
|
|
|
60
|
-
|
|
130
|
+
static doesUseUuid(modelMeta: Record<string, { annotationType: string, metadata: IIdMetaOpts }>): boolean
|
|
131
|
+
{
|
|
132
|
+
let useUuid = false;
|
|
133
|
+
|
|
134
|
+
for (const key in modelMeta) {
|
|
135
|
+
const modelMetadata: IIdMetaOpts = modelMeta[key].metadata;
|
|
136
|
+
const annotationType: string = modelMeta[key].annotationType;
|
|
137
|
+
|
|
138
|
+
if (key !== 'id') {
|
|
139
|
+
if (annotationType == 'IdType') {
|
|
140
|
+
if (modelMetadata.dbOptions?.mysql?.useUuid) {
|
|
61
141
|
useUuid = true;
|
|
62
142
|
}
|
|
63
143
|
|
|
64
|
-
if(modelMetadata.dbOptions?.postgres?.useUuid){
|
|
144
|
+
if (modelMetadata.dbOptions?.postgres?.useUuid) {
|
|
65
145
|
useUuid = true;
|
|
66
146
|
}
|
|
67
147
|
|
|
68
|
-
if(modelMetadata.type.name === 'String'){
|
|
148
|
+
if (modelMetadata.type.name === 'String') {
|
|
69
149
|
useUuid = true;
|
|
70
150
|
}
|
|
71
151
|
}
|
|
72
152
|
}
|
|
73
153
|
}
|
|
74
154
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
let reqStr = '';
|
|
155
|
+
return useUuid;
|
|
156
|
+
}
|
|
78
157
|
|
|
79
|
-
|
|
80
|
-
|
|
158
|
+
static addIdPart(dbType: IDbConfigParams['db_type'], useUuid: boolean, noAuto: boolean = false): string
|
|
159
|
+
{
|
|
160
|
+
let idString = ` @id${!noAuto ? ` @default(${this.generateIdDefault(dbType, useUuid)})` : ''}`;
|
|
161
|
+
|
|
162
|
+
if(dbType === 'mongodb'){
|
|
163
|
+
idString += ' @map("_id")';
|
|
164
|
+
idString += ' @db.ObjectId';
|
|
81
165
|
}
|
|
82
166
|
|
|
167
|
+
return idString;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
static generateIdDefault(dbType: IDbConfigParams['db_type'], useUuid: boolean): string
|
|
171
|
+
{
|
|
83
172
|
switch (dbType) {
|
|
84
173
|
case 'mongodb':
|
|
85
|
-
|
|
86
|
-
break;
|
|
174
|
+
return `auto()`;
|
|
87
175
|
|
|
88
176
|
case 'mysql':
|
|
89
|
-
|
|
90
|
-
?
|
|
91
|
-
:
|
|
92
|
-
break;
|
|
177
|
+
return useUuid
|
|
178
|
+
? `uuid()`
|
|
179
|
+
: `autoincrement()`;
|
|
93
180
|
|
|
94
181
|
case 'postgresql':
|
|
95
182
|
case 'postgres':
|
|
96
|
-
|
|
97
|
-
?
|
|
98
|
-
:
|
|
99
|
-
break;
|
|
183
|
+
return useUuid
|
|
184
|
+
? `uuid()`
|
|
185
|
+
: `autoincrement()`;
|
|
100
186
|
|
|
101
187
|
case 'sqlite':
|
|
102
|
-
|
|
103
|
-
break;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if(tags.length){
|
|
107
|
-
idString += ' '+tags.join(' ');
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if(!idString){
|
|
111
|
-
throw new Error(`DB type "${dbType}" is not supported!`);
|
|
188
|
+
return 'autoincrement()';
|
|
112
189
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
return idString;
|
|
116
190
|
}
|
|
117
191
|
}
|
|
118
192
|
|
|
@@ -5,13 +5,16 @@ export interface IDbOpts {
|
|
|
5
5
|
useText?: boolean;
|
|
6
6
|
maxLength?: number;
|
|
7
7
|
useUuid?: boolean;
|
|
8
|
+
params?: string[]
|
|
8
9
|
};
|
|
9
10
|
postgres?: {
|
|
10
11
|
useText?: boolean;
|
|
11
12
|
useUuid?: boolean;
|
|
13
|
+
params?: string[]
|
|
12
14
|
};
|
|
13
15
|
mongodb?: {
|
|
14
16
|
customType?: string;
|
|
17
|
+
params?: string[]
|
|
15
18
|
};
|
|
16
19
|
}
|
|
17
20
|
}
|
|
@@ -6,9 +6,18 @@ export class ModelUtils {
|
|
|
6
6
|
const annotationsData: Record<string, {annotationType: string, metadata: any}> = {};
|
|
7
7
|
|
|
8
8
|
const metadataKeys = Reflect.getMetadataKeys(constructor.prototype);
|
|
9
|
+
|
|
10
|
+
const filteredMetaKeys = metadataKeys.filter((metaKey) => {
|
|
11
|
+
const [annotationType, annotatedField] = metaKey.split(':');
|
|
12
|
+
if(annotationType === 'TrackType' && annotatedField === 'id' && metadataKeys.includes('IdType:' + annotatedField)){
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return true;
|
|
17
|
+
});
|
|
9
18
|
|
|
10
19
|
// Process all metadata keys and collect promises
|
|
11
|
-
const metadataPromises =
|
|
20
|
+
const metadataPromises = filteredMetaKeys.map(async (fullKey: string) => {
|
|
12
21
|
const [annotationType, propertyKey] = fullKey.split(':');
|
|
13
22
|
const metadata = Reflect.getMetadata(fullKey, constructor.prototype);
|
|
14
23
|
|