@koalarx/nest 3.1.27 → 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;
|
|
@@ -76,7 +77,7 @@ class RepositoryBase {
|
|
|
76
77
|
const list = new entity()[prop.name];
|
|
77
78
|
const entityInstance = list.entityType;
|
|
78
79
|
selectSchema[prop.name] = {
|
|
79
|
-
select: this.
|
|
80
|
+
select: this.getWhereByIdSchema(new entityInstance(), true),
|
|
80
81
|
};
|
|
81
82
|
}
|
|
82
83
|
else {
|
|
@@ -307,11 +308,12 @@ class RepositoryBase {
|
|
|
307
308
|
select: this.getSelectRootPrismaSchema(entity),
|
|
308
309
|
where,
|
|
309
310
|
})
|
|
310
|
-
.then((data) => this.enrichEntityWithRelations(entity, data, cache));
|
|
311
|
+
.then((data) => this.enrichEntityWithRelations(entity, (0, proxy_1.createProxy)(data), cache));
|
|
311
312
|
}
|
|
312
313
|
async enrichEntityWithRelations(entity, data, cache = new Map()) {
|
|
313
|
-
if (!data)
|
|
314
|
-
return
|
|
314
|
+
if (!data) {
|
|
315
|
+
return null;
|
|
316
|
+
}
|
|
315
317
|
const allProps = auto_mapping_list_1.AutoMappingList.getAllProps(entity);
|
|
316
318
|
for (const prop of allProps) {
|
|
317
319
|
const propName = prop.name;
|
|
@@ -321,7 +323,7 @@ class RepositoryBase {
|
|
|
321
323
|
const entityInstance = list.entityType;
|
|
322
324
|
const items = [];
|
|
323
325
|
for (const item of data[propName] || []) {
|
|
324
|
-
const cacheKey = `${entity.name}-${
|
|
326
|
+
const cacheKey = `${entity.name}-${this.getIdOnEntity(new entityInstance(), item)}`;
|
|
325
327
|
if (cache.has(cacheKey)) {
|
|
326
328
|
items.push(cache.get(cacheKey));
|
|
327
329
|
continue;
|
|
@@ -336,13 +338,13 @@ class RepositoryBase {
|
|
|
336
338
|
}
|
|
337
339
|
const relationEntity = auto_mapping_list_1.AutoMappingList.getSourceByName(propDef?.type ?? '');
|
|
338
340
|
if (relationEntity && data[propName]) {
|
|
339
|
-
const cacheKey = `${entity.name}-${
|
|
341
|
+
const cacheKey = `${entity.name}-${this.getIdOnEntity(new relationEntity(), data[propName])}`;
|
|
340
342
|
if (cache.has(cacheKey)) {
|
|
341
343
|
data[propName] = cache.get(cacheKey);
|
|
342
|
-
|
|
344
|
+
continue;
|
|
343
345
|
}
|
|
344
346
|
cache.set(cacheKey, data[propName]);
|
|
345
|
-
data[propName] = await this.loadRelationForEntity(this.getWhereByIdSchema(relationEntity, data[propName]), relationEntity, cache);
|
|
347
|
+
data[propName] = await this.loadRelationForEntity(this.getWhereByIdSchema(new relationEntity(), data[propName]), relationEntity, cache);
|
|
346
348
|
cache.set(cacheKey, data[propName]);
|
|
347
349
|
}
|
|
348
350
|
}
|
|
@@ -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,
|
|
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,
|
|
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,
|
|
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
|
+
}
|