@koalarx/nest 3.1.13 → 3.1.14

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.
@@ -28,6 +28,7 @@ export declare abstract class RepositoryBase<TEntity extends EntityBase<TEntity>
28
28
  private findManySchema;
29
29
  private createEntity;
30
30
  private orphanRemoval;
31
+ private getIdOnEntity;
31
32
  private loadRelationForEntity;
32
33
  private enrichEntityWithRelations;
33
34
  private persistRelations;
@@ -22,18 +22,30 @@ class RepositoryBase {
22
22
  });
23
23
  }
24
24
  getIdPropName(entity) {
25
- return (Reflect.getMetadata('entity:id', entity ? entity.constructor.prototype : this._modelName.prototype) ?? 'id');
25
+ const idConfig = Reflect.getMetadata('entity:id', entity ? entity.constructor.prototype : this._modelName.prototype);
26
+ if (idConfig) {
27
+ if (idConfig.single) {
28
+ return idConfig.single;
29
+ }
30
+ else if (idConfig.composite) {
31
+ return idConfig.composite;
32
+ }
33
+ }
34
+ return 'id';
26
35
  }
27
36
  getWhereByIdSchema(entity, value) {
28
37
  const propIdName = this.getIdPropName(entity);
29
38
  if (Array.isArray(propIdName)) {
30
39
  const whereSchema = {};
31
40
  propIdName.forEach((propName) => {
32
- whereSchema[propName] = value[propName];
41
+ whereSchema[propName] =
42
+ typeof value === 'object' ? value[propName] : value;
33
43
  });
34
44
  return whereSchema;
35
45
  }
36
- return { [propIdName]: value[propIdName] };
46
+ return {
47
+ [propIdName]: typeof value === 'object' ? value[propIdName] : value,
48
+ };
37
49
  }
38
50
  getConnectPrismaSchemaForRelation(entity, data) {
39
51
  return {
@@ -74,14 +86,14 @@ class RepositoryBase {
74
86
  }
75
87
  if (instance instanceof entity_base_1.EntityBase) {
76
88
  selectSchema[prop.name] = {
77
- select: this.getSelectRootPrismaSchema(instance),
89
+ select: this.getWhereByIdSchema(instance, true),
78
90
  };
79
91
  }
80
92
  else if (instance instanceof list_1.List) {
81
93
  const list = new entity()[prop.name];
82
94
  const entityInstance = list.entityType;
83
95
  selectSchema[prop.name] = {
84
- select: this.getSelectRootPrismaSchema(new entityInstance()),
96
+ select: this.getWhereByIdSchema(new entityInstance(), true),
85
97
  };
86
98
  }
87
99
  else {
@@ -243,15 +255,28 @@ class RepositoryBase {
243
255
  .forEach((key) => (where[key] = entity[key]));
244
256
  return client[(0, KlString_1.toCamelCase)(entity.constructor.name)].delete({ where });
245
257
  }
246
- async loadRelationForEntity(where, entity) {
258
+ getIdOnEntity(entity, data) {
259
+ const propIdName = this.getIdPropName(entity);
260
+ if (Array.isArray(propIdName)) {
261
+ const idValues = [];
262
+ propIdName.forEach((propName) => {
263
+ idValues.push(data[propName]);
264
+ });
265
+ return idValues.join('-');
266
+ }
267
+ return data[propIdName];
268
+ }
269
+ async loadRelationForEntity(where, entity, cache) {
247
270
  return this._context[(0, KlString_1.toCamelCase)((0, KlString_1.toCamelCase)(entity.name))]
248
- .findUnique({
271
+ .findFirst({
249
272
  select: this.getSelectWithRelationsId(entity),
250
273
  where,
251
274
  })
252
- .then((data) => this.enrichEntityWithRelations(entity, data));
275
+ .then((data) => this.enrichEntityWithRelations(entity, data, cache));
253
276
  }
254
- async enrichEntityWithRelations(entity, data) {
277
+ async enrichEntityWithRelations(entity, data, cache = new Map()) {
278
+ if (!data)
279
+ return data;
255
280
  const relationQueries = [];
256
281
  const relationKeys = [];
257
282
  const allProps = auto_mapping_list_1.AutoMappingList.getAllProps(entity);
@@ -264,15 +289,27 @@ class RepositoryBase {
264
289
  relationKeys.push(propName);
265
290
  const items = [];
266
291
  data[propName].forEach((item) => {
267
- items.push(this.loadRelationForEntity(item, entityInstance));
292
+ const cacheKey = `${entity.constructor.name}-${propName}-${this.getIdOnEntity(new entityInstance(), item)}`;
293
+ if (cache.has(cacheKey)) {
294
+ items.push(Promise.resolve(cache.get(cacheKey)));
295
+ return;
296
+ }
297
+ cache.set(cacheKey, item);
298
+ items.push(this.loadRelationForEntity(item, entityInstance, cache));
268
299
  });
269
300
  relationQueries.push(Promise.all(items));
270
301
  return;
271
302
  }
272
303
  const relationEntity = auto_mapping_list_1.AutoMappingList.getSourceByName(propDef?.type ?? '');
273
304
  if (relationEntity && data[propName]) {
305
+ const cacheKey = `${entity.constructor.name}-${propName}-${this.getIdOnEntity(new relationEntity(), data[propName])}`;
306
+ if (cache.has(cacheKey)) {
307
+ data[propName] = cache.get(cacheKey);
308
+ return;
309
+ }
310
+ cache.set(cacheKey, data[propName]);
274
311
  relationKeys.push(propName);
275
- relationQueries.push(this.loadRelationForEntity(this.getWhereByIdSchema(relationEntity, data[propName]), relationEntity));
312
+ relationQueries.push(this.loadRelationForEntity(this.getWhereByIdSchema(relationEntity, data[propName]), relationEntity, cache));
276
313
  }
277
314
  });
278
315
  if (relationQueries.length > 0) {
@@ -341,7 +378,7 @@ class RepositoryBase {
341
378
  });
342
379
  if (!data)
343
380
  return null;
344
- const enrichedEntity = await this.enrichEntityWithRelations(this._modelName.prototype.constructor, data);
381
+ const enrichedEntity = await this.enrichEntityWithRelations(this._modelName.prototype.constructor, { ...data });
345
382
  return this.createEntity(enrichedEntity);
346
383
  }
347
384
  async findFirst(where) {
@@ -351,7 +388,7 @@ class RepositoryBase {
351
388
  });
352
389
  if (!data)
353
390
  return null;
354
- const enrichedEntity = await this.enrichEntityWithRelations(this._modelName.prototype.constructor, data);
391
+ const enrichedEntity = await this.enrichEntityWithRelations(this._modelName.prototype.constructor, { ...data });
355
392
  return this.createEntity(enrichedEntity);
356
393
  }
357
394
  async findUnique(where) {
@@ -361,7 +398,7 @@ class RepositoryBase {
361
398
  });
362
399
  if (!data)
363
400
  return null;
364
- const enrichedEntity = await this.enrichEntityWithRelations(this._modelName.prototype.constructor, data);
401
+ const enrichedEntity = await this.enrichEntityWithRelations(this._modelName.prototype.constructor, { ...data });
365
402
  return this.createEntity(enrichedEntity);
366
403
  }
367
404
  async findMany(where, pagination) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koalarx/nest",
3
- "version": "3.1.13",
3
+ "version": "3.1.14",
4
4
  "description": "",
5
5
  "author": "Igor D. Rangel",
6
6
  "license": "MIT",