@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.
Files changed (35) hide show
  1. package/dist/decorators/IdType.js +6 -0
  2. package/dist/decorators/InverseRelation.js +3 -4
  3. package/dist/decorators/Relation.js +4 -3
  4. package/dist/decorators/TrackType.js +2 -2
  5. package/dist/helper/DbHelper.d.ts +1 -1
  6. package/dist/helper/DbHelper.js +4 -4
  7. package/dist/helper/db/relation-manager.js +1 -1
  8. package/dist/helper/db/schema-generator.js +73 -26
  9. package/dist/helper/db/type-converter.d.ts +2 -1
  10. package/dist/helper/db/type-converter.js +33 -9
  11. package/dist/helper/db/utils.d.ts +7 -0
  12. package/dist/helper/db/utils.js +80 -26
  13. package/dist/models/core/RWSModel.js +29 -32
  14. package/dist/models/interfaces/IDbOpts.d.ts +3 -0
  15. package/dist/models/interfaces/IIdTypeOpts.d.ts +2 -0
  16. package/dist/models/interfaces/ITrackerOpts.d.ts +1 -0
  17. package/dist/models/utils/HydrateUtils.js +0 -1
  18. package/dist/models/utils/ModelUtils.js +8 -1
  19. package/dist/models/utils/PaginationUtils.js +5 -6
  20. package/dist/services/DBService.js +7 -8
  21. package/exec/console.js +0 -2
  22. package/package.json +1 -1
  23. package/src/decorators/IdType.ts +10 -1
  24. package/src/decorators/InverseRelation.ts +2 -2
  25. package/src/decorators/Relation.ts +6 -4
  26. package/src/helper/DbHelper.ts +1 -1
  27. package/src/helper/db/schema-generator.ts +104 -36
  28. package/src/helper/db/type-converter.ts +41 -8
  29. package/src/helper/db/utils.ts +114 -40
  30. package/src/models/interfaces/IDbOpts.ts +3 -0
  31. package/src/models/interfaces/IIdTypeOpts.ts +2 -0
  32. package/src/models/interfaces/ITrackerOpts.ts +1 -0
  33. package/src/models/utils/HydrateUtils.ts +0 -2
  34. package/src/models/utils/ModelUtils.ts +10 -1
  35. package/tsconfig.json +1 -1
@@ -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 = false;
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
- if(modelMetadata.dbOptions?.mysql?.useUuid){
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
- let idString: string;
76
-
77
- let reqStr = '';
155
+ return useUuid;
156
+ }
78
157
 
79
- if(optional){
80
- reqStr = '?';
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
- idString = `${field} String${reqStr} @id @default(auto()) @map("_id") @db.ObjectId`;
86
- break;
174
+ return `auto()`;
87
175
 
88
176
  case 'mysql':
89
- idString = useUuid
90
- ? `${field} String${reqStr} @id @default(uuid())`
91
- : `${field} Int${reqStr} @id @default(autoincrement())`;
92
- break;
177
+ return useUuid
178
+ ? `uuid()`
179
+ : `autoincrement()`;
93
180
 
94
181
  case 'postgresql':
95
182
  case 'postgres':
96
- idString = useUuid
97
- ? `${field} String${reqStr} @id @default(uuid())`
98
- : `${field} Int${reqStr} @id @default(autoincrement())`;
99
- break;
183
+ return useUuid
184
+ ? `uuid()`
185
+ : `autoincrement()`;
100
186
 
101
187
  case 'sqlite':
102
- idString = `${field} Int${reqStr} @id @default(autoincrement())`;
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
  }
@@ -1,4 +1,6 @@
1
1
  import { IDbOpts } from "./IDbOpts";
2
2
 
3
3
  export interface IIdTypeOpts extends IDbOpts {
4
+ unique?: boolean | string;
5
+ noAuto?: boolean;
4
6
  }
@@ -10,4 +10,5 @@ export interface ITrackerOpts extends IDbOpts {
10
10
  relatedTo?: OpModelType<any>,
11
11
  inversionModel?: OpModelType<any>,
12
12
  relationName?: string
13
+ noAuto?: boolean;
13
14
  }
@@ -53,8 +53,6 @@ export class HydrateUtils {
53
53
  }
54
54
 
55
55
  const relMeta = relManyData[key];
56
-
57
- // console.log({relMeta});
58
56
 
59
57
  const relationEnabled = !RelationUtils.checkRelDisabled(model, relMeta.key);
60
58
  if (relationEnabled) {
@@ -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 = metadataKeys.map(async (fullKey: string) => {
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
 
package/tsconfig.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "baseUrl": ".",
4
4
  "experimentalDecorators": true,
5
5
  "emitDecoratorMetadata": true,
6
- "target": "ES2018",
6
+ "target": "ES2022",
7
7
  "module": "commonjs",
8
8
  "moduleResolution": "node",
9
9
  "strict": true,