@rws-framework/db 4.1.5 → 4.1.6

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.
@@ -222,7 +222,7 @@ class RWSModel {
222
222
  if (entryExists) {
223
223
  await this.preUpdate();
224
224
  const pk = ModelUtils_1.ModelUtils.findPrimaryKeyFields(this.constructor);
225
- updatedModelData = await this.dbService.update(data, this.getCollection(), pk);
225
+ updatedModelData = await this.dbService.update(data, this.getCollection(), pk, this.constructor);
226
226
  await this._asyncFill(updatedModelData);
227
227
  await this.postUpdate();
228
228
  }
@@ -23,7 +23,7 @@ declare class DBService {
23
23
  cloneDatabase(source: string, target: string): Promise<void>;
24
24
  watchCollection(collectionName: string, preRun: () => void): Promise<any>;
25
25
  insert(data: any, collection: string, isTimeSeries?: boolean): Promise<any>;
26
- update(data: any, collection: string, pk: string | string[]): Promise<IModel>;
26
+ update(data: any, collection: string, pk: string | string[], modelClass?: any): Promise<IModel>;
27
27
  findOneBy(collection: string, conditions: any, fields?: string[] | null, ordering?: OrderByType, prismaOptions?: any): Promise<IModel | null>;
28
28
  delete(collection: string, conditions: any): Promise<void>;
29
29
  findBy(collection: string, conditions: any, fields?: string[] | null, ordering?: OrderByType, pagination?: IPaginationParams, prismaOptions?: any): Promise<IModel[]>;
@@ -37,7 +37,7 @@ declare class DBService {
37
37
  }): Promise<number>;
38
38
  /**
39
39
  * Convert foreign key fields to Prisma relation syntax
40
- * Handles common patterns like user_id -> creator, avatar_id -> avatar, etc.
40
+ * Dynamically reads relation metadata from model decorators
41
41
  */
42
42
  private convertForeignKeysToRelations;
43
43
  getPrismaClient(): PrismaClient;
@@ -1,4 +1,37 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
37
  };
@@ -101,7 +134,7 @@ class DBService {
101
134
  result = await prismaCollection.create({ data });
102
135
  return await this.findOneBy(collection, { id: result.id });
103
136
  }
104
- async update(data, collection, pk) {
137
+ async update(data, collection, pk, modelClass) {
105
138
  const prismaCollection = this.getCollectionHandler(collection);
106
139
  const where = {};
107
140
  if (Array.isArray(pk)) {
@@ -121,7 +154,7 @@ class DBService {
121
154
  }
122
155
  }
123
156
  // Convert foreign key fields to Prisma relation syntax
124
- const processedData = this.convertForeignKeysToRelations(data);
157
+ const processedData = await this.convertForeignKeysToRelations(data, modelClass);
125
158
  await prismaCollection.update({
126
159
  where,
127
160
  data: processedData,
@@ -248,37 +281,37 @@ class DBService {
248
281
  }
249
282
  /**
250
283
  * Convert foreign key fields to Prisma relation syntax
251
- * Handles common patterns like user_id -> creator, avatar_id -> avatar, etc.
284
+ * Dynamically reads relation metadata from model decorators
252
285
  */
253
- convertForeignKeysToRelations(data) {
286
+ async convertForeignKeysToRelations(data, modelClass) {
254
287
  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
- };
288
+ let relationMappings = {};
289
+ // If model class is provided, dynamically build relation mappings from metadata
290
+ if (modelClass) {
291
+ try {
292
+ const { ModelUtils } = await Promise.resolve().then(() => __importStar(require('../models/utils/ModelUtils')));
293
+ const modelAnnotations = await ModelUtils.getModelAnnotations(modelClass);
294
+ // Build relation mappings from the model's relation metadata
295
+ Object.keys(modelAnnotations).forEach(propertyKey => {
296
+ const annotation = modelAnnotations[propertyKey];
297
+ if (annotation.annotationType === 'Relation') {
298
+ const metadata = annotation.metadata;
299
+ const relationField = metadata.relationField;
300
+ const relationName = metadata.relationName || propertyKey;
301
+ if (relationField) {
302
+ relationMappings[relationField] = relationName;
303
+ }
304
+ }
305
+ });
306
+ }
307
+ catch (error) {
308
+ console.warn('Failed to read model relation metadata, falling back to static mappings:', error);
309
+ }
310
+ }
311
+ // Fallback to static mappings if no model class provided or metadata extraction fails
312
+ if (Object.keys(relationMappings).length === 0) {
313
+ relationMappings = {};
314
+ }
282
315
  // Convert foreign key fields to relation syntax
283
316
  Object.keys(processedData).forEach(key => {
284
317
  if (key.endsWith('_id') && relationMappings[key]) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rws-framework/db",
3
3
  "private": false,
4
- "version": "4.1.5",
4
+ "version": "4.1.6",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -279,7 +279,7 @@ class RWSModel<T> implements IModel {
279
279
 
280
280
  const pk = ModelUtils.findPrimaryKeyFields(this.constructor as OpModelType<any>);
281
281
 
282
- updatedModelData = await this.dbService.update(data, this.getCollection(), pk);
282
+ updatedModelData = await this.dbService.update(data, this.getCollection(), pk, this.constructor);
283
283
 
284
284
  await this._asyncFill(updatedModelData);
285
285
  await this.postUpdate();
@@ -138,7 +138,7 @@ class DBService {
138
138
  return await this.findOneBy(collection, { id: result.id });
139
139
  }
140
140
 
141
- async update(data: any, collection: string, pk: string | string[]): Promise<IModel>
141
+ async update(data: any, collection: string, pk: string | string[], modelClass?: any): Promise<IModel>
142
142
  {
143
143
 
144
144
  const prismaCollection = this.getCollectionHandler(collection);
@@ -163,7 +163,7 @@ class DBService {
163
163
  }
164
164
 
165
165
  // Convert foreign key fields to Prisma relation syntax
166
- const processedData = this.convertForeignKeysToRelations(data);
166
+ const processedData = await this.convertForeignKeysToRelations(data, modelClass);
167
167
 
168
168
  await prismaCollection.update({
169
169
  where,
@@ -337,37 +337,41 @@ class DBService {
337
337
 
338
338
  /**
339
339
  * Convert foreign key fields to Prisma relation syntax
340
- * Handles common patterns like user_id -> creator, avatar_id -> avatar, etc.
340
+ * Dynamically reads relation metadata from model decorators
341
341
  */
342
- private convertForeignKeysToRelations(data: any): any {
342
+ private async convertForeignKeysToRelations(data: any, modelClass?: any): Promise<any> {
343
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
- };
344
+ let relationMappings: { [key: string]: string } = {};
345
+
346
+ // If model class is provided, dynamically build relation mappings from metadata
347
+ if (modelClass) {
348
+ try {
349
+ const { ModelUtils } = await import('../models/utils/ModelUtils');
350
+ const modelAnnotations = await ModelUtils.getModelAnnotations(modelClass);
351
+
352
+ // Build relation mappings from the model's relation metadata
353
+ Object.keys(modelAnnotations).forEach(propertyKey => {
354
+ const annotation = modelAnnotations[propertyKey];
355
+ if (annotation.annotationType === 'Relation') {
356
+ const metadata = annotation.metadata;
357
+ const relationField = metadata.relationField;
358
+ const relationName = metadata.relationName || propertyKey;
359
+
360
+ if (relationField) {
361
+ relationMappings[relationField] = relationName;
362
+ }
363
+ }
364
+ });
365
+ } catch (error) {
366
+ console.warn('Failed to read model relation metadata, falling back to static mappings:', error);
367
+ }
368
+ }
369
+
370
+ // Fallback to static mappings if no model class provided or metadata extraction fails
371
+ if (Object.keys(relationMappings).length === 0) {
372
+ relationMappings = {
373
+ };
374
+ }
371
375
 
372
376
  // Convert foreign key fields to relation syntax
373
377
  Object.keys(processedData).forEach(key => {