@koalarx/nest 3.1.26 → 3.1.28

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.
@@ -8,6 +8,7 @@ const auto_mapping_list_1 = require("../mapping/auto-mapping-list");
8
8
  const generate_prisma_include_schema_1 = require("../utils/generate-prisma-include-schema");
9
9
  const list_1 = require("../utils/list");
10
10
  const entity_base_1 = require("./entity.base");
11
+ const proxy_1 = require("../utils/proxy");
11
12
  class RepositoryBase {
12
13
  _context;
13
14
  _modelName;
@@ -72,6 +73,13 @@ class RepositoryBase {
72
73
  select: this.getSelectRootPrismaSchema(instance.constructor),
73
74
  };
74
75
  }
76
+ else if (instance instanceof list_1.List) {
77
+ const list = new entity()[prop.name];
78
+ const entityInstance = list.entityType;
79
+ selectSchema[prop.name] = {
80
+ select: this.getWhereByIdSchema(new entityInstance(), true),
81
+ };
82
+ }
75
83
  else {
76
84
  selectSchema[prop.name] = true;
77
85
  }
@@ -300,51 +308,45 @@ class RepositoryBase {
300
308
  select: this.getSelectRootPrismaSchema(entity),
301
309
  where,
302
310
  })
303
- .then((data) => this.enrichEntityWithRelations(entity, data, cache));
311
+ .then((data) => this.enrichEntityWithRelations(entity, (0, proxy_1.createProxy)(data), cache));
304
312
  }
305
313
  async enrichEntityWithRelations(entity, data, cache = new Map()) {
306
- if (!data)
307
- return data;
308
- const relationQueries = [];
309
- const relationKeys = [];
314
+ if (!data) {
315
+ return null;
316
+ }
310
317
  const allProps = auto_mapping_list_1.AutoMappingList.getAllProps(entity);
311
- allProps.forEach((prop) => {
318
+ for (const prop of allProps) {
312
319
  const propName = prop.name;
313
320
  const propDef = auto_mapping_list_1.AutoMappingList.getPropDefinitions(entity, propName);
314
321
  if (propDef?.type === list_1.List.name) {
315
322
  const list = new entity()[prop.name];
316
323
  const entityInstance = list.entityType;
317
- relationKeys.push(propName);
318
324
  const items = [];
319
- data[propName]?.forEach((item) => {
320
- const cacheKey = `${entity.name}-${propName}-${this.getIdOnEntity(new entityInstance(), item)}`;
325
+ for (const item of data[propName] || []) {
326
+ const cacheKey = `${entity.name}-${this.getIdOnEntity(new entityInstance(), item)}`;
321
327
  if (cache.has(cacheKey)) {
322
- items.push(Promise.resolve(cache.get(cacheKey)));
323
- return;
328
+ items.push(cache.get(cacheKey));
329
+ continue;
324
330
  }
325
331
  cache.set(cacheKey, item);
326
- items.push(this.loadRelationForEntity(this.getWhereByIdSchema(new entityInstance(), item), entityInstance, cache));
327
- });
328
- relationQueries.push(Promise.all(items));
329
- return;
332
+ const enrichedItem = await this.loadRelationForEntity(this.getWhereByIdSchema(new entityInstance(), item), entityInstance, cache);
333
+ cache.set(cacheKey, enrichedItem);
334
+ items.push(enrichedItem);
335
+ }
336
+ data[propName] = items;
337
+ continue;
330
338
  }
331
339
  const relationEntity = auto_mapping_list_1.AutoMappingList.getSourceByName(propDef?.type ?? '');
332
340
  if (relationEntity && data[propName]) {
333
- const cacheKey = `${entity.name}-${propName}-${this.getIdOnEntity(new relationEntity(), data[propName])}`;
341
+ const cacheKey = `${entity.name}-${this.getIdOnEntity(new relationEntity(), data[propName])}`;
334
342
  if (cache.has(cacheKey)) {
335
343
  data[propName] = cache.get(cacheKey);
336
- return;
344
+ continue;
337
345
  }
338
346
  cache.set(cacheKey, data[propName]);
339
- relationKeys.push(propName);
340
- relationQueries.push(this.loadRelationForEntity(this.getWhereByIdSchema(relationEntity, data[propName]), relationEntity, cache));
347
+ data[propName] = await this.loadRelationForEntity(this.getWhereByIdSchema(new relationEntity(), data[propName]), relationEntity, cache);
348
+ cache.set(cacheKey, data[propName]);
341
349
  }
342
- });
343
- if (relationQueries.length > 0) {
344
- const results = await Promise.all(relationQueries);
345
- relationKeys.forEach((key, index) => {
346
- data[key] = results[index];
347
- });
348
350
  }
349
351
  return data;
350
352
  }
@@ -416,7 +418,7 @@ class RepositoryBase {
416
418
  });
417
419
  if (!data)
418
420
  return null;
419
- const enrichedEntity = await this.enrichEntityWithRelations(this._modelName.prototype.constructor, { ...data });
421
+ const enrichedEntity = await this.enrichEntityWithRelations(this._modelName.prototype.constructor, (0, proxy_1.createProxy)(data));
420
422
  return this.createEntity(enrichedEntity);
421
423
  }
422
424
  async findFirst(where) {
@@ -426,7 +428,7 @@ class RepositoryBase {
426
428
  });
427
429
  if (!data)
428
430
  return null;
429
- const enrichedEntity = await this.enrichEntityWithRelations(this._modelName.prototype.constructor, { ...data });
431
+ const enrichedEntity = await this.enrichEntityWithRelations(this._modelName.prototype.constructor, (0, proxy_1.createProxy)(data));
430
432
  return this.createEntity(enrichedEntity);
431
433
  }
432
434
  async findUnique(where) {
@@ -436,7 +438,7 @@ class RepositoryBase {
436
438
  });
437
439
  if (!data)
438
440
  return null;
439
- const enrichedEntity = await this.enrichEntityWithRelations(this._modelName.prototype.constructor, { ...data });
441
+ const enrichedEntity = await this.enrichEntityWithRelations(this._modelName.prototype.constructor, (0, proxy_1.createProxy)(data));
440
442
  return this.createEntity(enrichedEntity);
441
443
  }
442
444
  async findMany(where, pagination) {
@@ -0,0 +1 @@
1
+ export declare function isPlainObject(val: any): boolean;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isPlainObject = isPlainObject;
4
+ function isPlainObject(val) {
5
+ if (typeof val !== 'object' || val === null)
6
+ return false;
7
+ return Object.prototype.toString.call(val) === '[object Object]';
8
+ }
@@ -0,0 +1 @@
1
+ export declare function createProxy(target: any): any;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createProxy = createProxy;
4
+ const is_plain_object_1 = require("./is-plain-object");
5
+ const handle = {
6
+ get(target, prop) {
7
+ if (prop === '__isProxy')
8
+ return true;
9
+ return target[prop];
10
+ },
11
+ set(target, prop, value) {
12
+ target[prop] = value;
13
+ return true;
14
+ },
15
+ };
16
+ function createProxy(target) {
17
+ const proxy = new Proxy(target, handle);
18
+ Object.keys(proxy).forEach((key) => {
19
+ if ((0, is_plain_object_1.isPlainObject)(proxy[key])) {
20
+ proxy[key] = new Proxy(proxy[key], handle);
21
+ }
22
+ else if (Array.isArray(proxy[key])) {
23
+ proxy[key] = proxy[key].map((item) => (0, is_plain_object_1.isPlainObject)(item) ? new Proxy(item, handle) : item);
24
+ }
25
+ });
26
+ return proxy;
27
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koalarx/nest",
3
- "version": "3.1.26",
3
+ "version": "3.1.28",
4
4
  "description": "",
5
5
  "author": "Igor D. Rangel",
6
6
  "license": "MIT",