@rws-framework/db 4.1.3 → 4.1.5

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.
@@ -19,7 +19,8 @@ function Relation(theModel, relationOptions = _DEFAULTS) {
19
19
  relationOptions.relationName :
20
20
  null
21
21
  };
22
- if (relationOptions.required) {
22
+ // Only set default cascade behavior if no explicit cascade was provided
23
+ if (relationOptions.required && !relationOptions.cascade) {
23
24
  if (!metaOpts.cascade) {
24
25
  metaOpts.cascade = {};
25
26
  }
@@ -35,6 +35,11 @@ declare class DBService {
35
35
  count<T = any>(opModel: OpModelType<T>, where?: {
36
36
  [k: string]: any;
37
37
  }): Promise<number>;
38
+ /**
39
+ * Convert foreign key fields to Prisma relation syntax
40
+ * Handles common patterns like user_id -> creator, avatar_id -> avatar, etc.
41
+ */
42
+ private convertForeignKeysToRelations;
38
43
  getPrismaClient(): PrismaClient;
39
44
  }
40
45
  export { DBService, IDBClientCreate };
@@ -120,9 +120,11 @@ class DBService {
120
120
  delete data[cKey];
121
121
  }
122
122
  }
123
+ // Convert foreign key fields to Prisma relation syntax
124
+ const processedData = this.convertForeignKeysToRelations(data);
123
125
  await prismaCollection.update({
124
126
  where,
125
- data: data,
127
+ data: processedData,
126
128
  });
127
129
  return await this.findOneBy(collection, where);
128
130
  }
@@ -244,6 +246,57 @@ class DBService {
244
246
  async count(opModel, where = {}) {
245
247
  return await this.getCollectionHandler(opModel._collection).count({ where });
246
248
  }
249
+ /**
250
+ * Convert foreign key fields to Prisma relation syntax
251
+ * Handles common patterns like user_id -> creator, avatar_id -> avatar, etc.
252
+ */
253
+ convertForeignKeysToRelations(data) {
254
+ const processedData = { ...data };
255
+ const relationMappings = {
256
+ // Common relation mappings for foreign keys to relation names
257
+ 'user_id': 'creator',
258
+ 'avatar_id': 'avatar',
259
+ 'file_id': 'logo',
260
+ 'company_id': 'company',
261
+ 'user_group_id': 'userGroup',
262
+ 'accountGrade_id': 'accountGrade',
263
+ 'account_balance_id': 'accountBalance',
264
+ 'knowledgeGroup_id': 'project',
265
+ 'conversation_id': 'conversation',
266
+ 'message_id': 'message',
267
+ 'knowledge_id': 'knowledge',
268
+ 'profession_id': 'profession',
269
+ 'step_id': 'step',
270
+ 'question_id': 'question',
271
+ 'tutorial_id': 'tutorial',
272
+ 'tutorial_step_id': 'tutorialStep',
273
+ 'tutorial_section_id': 'tutorialSection',
274
+ 'todo_id': 'todo',
275
+ 'bot_test_id': 'botTest',
276
+ 'bot_test_tester_id': 'tester',
277
+ 'bot_test_target_id': 'target',
278
+ 'branch_id': 'branch',
279
+ 'acl_policy_id': 'policy',
280
+ 'instruction_file_id': 'instructionFile'
281
+ };
282
+ // Convert foreign key fields to relation syntax
283
+ Object.keys(processedData).forEach(key => {
284
+ if (key.endsWith('_id') && relationMappings[key]) {
285
+ const relationField = relationMappings[key];
286
+ const value = processedData[key];
287
+ // Remove the foreign key field
288
+ delete processedData[key];
289
+ // Add the relation field with proper Prisma syntax
290
+ if (value === null || value === undefined) {
291
+ processedData[relationField] = { disconnect: true };
292
+ }
293
+ else {
294
+ processedData[relationField] = { connect: { id: value } };
295
+ }
296
+ }
297
+ });
298
+ return processedData;
299
+ }
247
300
  getPrismaClient() {
248
301
  if (!this.client || !this.connected) {
249
302
  this.connectToDB();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rws-framework/db",
3
3
  "private": false,
4
- "version": "4.1.3",
4
+ "version": "4.1.5",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -43,7 +43,8 @@ function Relation(theModel: () => OpModelType<RWSModel<any>>, relationOptions: P
43
43
  null
44
44
  };
45
45
 
46
- if(relationOptions.required){
46
+ // Only set default cascade behavior if no explicit cascade was provided
47
+ if(relationOptions.required && !relationOptions.cascade){
47
48
  if(!metaOpts.cascade){
48
49
  metaOpts.cascade = {};
49
50
  }
@@ -162,9 +162,12 @@ class DBService {
162
162
  }
163
163
  }
164
164
 
165
+ // Convert foreign key fields to Prisma relation syntax
166
+ const processedData = this.convertForeignKeysToRelations(data);
167
+
165
168
  await prismaCollection.update({
166
169
  where,
167
- data: data,
170
+ data: processedData,
168
171
  });
169
172
 
170
173
 
@@ -332,6 +335,61 @@ class DBService {
332
335
  return await this.getCollectionHandler(opModel._collection).count({where});
333
336
  }
334
337
 
338
+ /**
339
+ * Convert foreign key fields to Prisma relation syntax
340
+ * Handles common patterns like user_id -> creator, avatar_id -> avatar, etc.
341
+ */
342
+ private convertForeignKeysToRelations(data: any): any {
343
+ const processedData = { ...data };
344
+ const relationMappings: { [key: string]: string } = {
345
+ // Common relation mappings for foreign keys to relation names
346
+ 'user_id': 'creator',
347
+ 'avatar_id': 'avatar',
348
+ 'file_id': 'logo',
349
+ 'company_id': 'company',
350
+ 'user_group_id': 'userGroup',
351
+ 'accountGrade_id': 'accountGrade',
352
+ 'account_balance_id': 'accountBalance',
353
+ 'knowledgeGroup_id': 'project',
354
+ 'conversation_id': 'conversation',
355
+ 'message_id': 'message',
356
+ 'knowledge_id': 'knowledge',
357
+ 'profession_id': 'profession',
358
+ 'step_id': 'step',
359
+ 'question_id': 'question',
360
+ 'tutorial_id': 'tutorial',
361
+ 'tutorial_step_id': 'tutorialStep',
362
+ 'tutorial_section_id': 'tutorialSection',
363
+ 'todo_id': 'todo',
364
+ 'bot_test_id': 'botTest',
365
+ 'bot_test_tester_id': 'tester',
366
+ 'bot_test_target_id': 'target',
367
+ 'branch_id': 'branch',
368
+ 'acl_policy_id': 'policy',
369
+ 'instruction_file_id': 'instructionFile'
370
+ };
371
+
372
+ // Convert foreign key fields to relation syntax
373
+ Object.keys(processedData).forEach(key => {
374
+ if (key.endsWith('_id') && relationMappings[key]) {
375
+ const relationField = relationMappings[key];
376
+ const value = processedData[key];
377
+
378
+ // Remove the foreign key field
379
+ delete processedData[key];
380
+
381
+ // Add the relation field with proper Prisma syntax
382
+ if (value === null || value === undefined) {
383
+ processedData[relationField] = { disconnect: true };
384
+ } else {
385
+ processedData[relationField] = { connect: { id: value } };
386
+ }
387
+ }
388
+ });
389
+
390
+ return processedData;
391
+ }
392
+
335
393
  public getPrismaClient(): PrismaClient
336
394
  {
337
395
  if(!this.client || !this.connected){