@koalarx/nest 3.1.0 → 3.1.2
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.
- package/core/database/entity.base.js +4 -2
- package/core/database/repository.base.d.ts +0 -1
- package/core/database/repository.base.js +11 -36
- package/core/utils/generate-prisma-include-schema.d.ts +9 -0
- package/core/utils/generate-prisma-include-schema.js +54 -0
- package/package.json +1 -1
- package/tsconfig.lib.tsbuildinfo +1 -1
|
@@ -41,10 +41,12 @@ class EntityBase {
|
|
|
41
41
|
(props[key] instanceof Array || Array.isArray(props[key]))) {
|
|
42
42
|
this[key] = new utils_1.KlArray(props[key]);
|
|
43
43
|
}
|
|
44
|
-
else if (propDefinitions?.type === 'KlDate' &&
|
|
44
|
+
else if (propDefinitions?.type === 'KlDate' &&
|
|
45
|
+
props[key] instanceof Date) {
|
|
45
46
|
this[key] = new utils_1.KlDate(props[key]);
|
|
46
47
|
}
|
|
47
|
-
else if (propDefinitions?.type === 'KlTime' &&
|
|
48
|
+
else if (propDefinitions?.type === 'KlTime' &&
|
|
49
|
+
props[key] instanceof Date) {
|
|
48
50
|
this[key] = new utils_1.KlTime(props[key].getHours(), props[key].getMinutes(), props[key].getSeconds(), props[key].getMilliseconds());
|
|
49
51
|
}
|
|
50
52
|
else if (EntityOnPropKey) {
|
|
@@ -20,7 +20,6 @@ export declare abstract class RepositoryBase<TEntity extends EntityBase<TEntity>
|
|
|
20
20
|
private resetDeepIncludeCount;
|
|
21
21
|
constructor({ context, modelName, deepIncludeLimit, }: RepositoryInitProps<TEntity, TContext>);
|
|
22
22
|
private getConnectPrismaSchemaForRelation;
|
|
23
|
-
private autogenerateIncludeSchema;
|
|
24
23
|
private getSelectRootPrismaSchema;
|
|
25
24
|
private getPropNameFromEntitySource;
|
|
26
25
|
private listRelationEntities;
|
|
@@ -4,9 +4,10 @@ exports.RepositoryBase = void 0;
|
|
|
4
4
|
const KlString_1 = require("@koalarx/utils/KlString");
|
|
5
5
|
const pagination_dto_1 = require("../dtos/pagination.dto");
|
|
6
6
|
const koala_global_vars_1 = require("../koala-global-vars");
|
|
7
|
+
const auto_mapping_list_1 = require("../mapping/auto-mapping-list");
|
|
8
|
+
const generate_prisma_include_schema_1 = require("../utils/generate-prisma-include-schema");
|
|
7
9
|
const list_1 = require("../utils/list");
|
|
8
10
|
const entity_base_1 = require("./entity.base");
|
|
9
|
-
const auto_mapping_list_1 = require("../mapping/auto-mapping-list");
|
|
10
11
|
class RepositoryBase {
|
|
11
12
|
_context;
|
|
12
13
|
_modelName;
|
|
@@ -21,9 +22,16 @@ class RepositoryBase {
|
|
|
21
22
|
this._context = context;
|
|
22
23
|
this._modelName = modelName;
|
|
23
24
|
this.deepIncludeLimit = deepIncludeLimit ?? 5;
|
|
24
|
-
this._include =
|
|
25
|
+
this._include = (0, generate_prisma_include_schema_1.generateIncludeSchema)({
|
|
26
|
+
deepLimit: deepIncludeLimit || 5,
|
|
27
|
+
entity: this._modelName,
|
|
28
|
+
});
|
|
25
29
|
this.resetDeepIncludeCount();
|
|
26
|
-
this._includeFindMany =
|
|
30
|
+
this._includeFindMany = (0, generate_prisma_include_schema_1.generateIncludeSchema)({
|
|
31
|
+
forList: true,
|
|
32
|
+
deepLimit: deepIncludeLimit || 1,
|
|
33
|
+
entity: this._modelName,
|
|
34
|
+
});
|
|
27
35
|
}
|
|
28
36
|
getConnectPrismaSchemaForRelation(entity, data) {
|
|
29
37
|
const propIdName = this.getIdPropName(entity);
|
|
@@ -33,39 +41,6 @@ class RepositoryBase {
|
|
|
33
41
|
},
|
|
34
42
|
};
|
|
35
43
|
}
|
|
36
|
-
autogenerateIncludeSchema(forList = false, relation) {
|
|
37
|
-
if (this.deepIncludeCount >= this.deepIncludeLimit) {
|
|
38
|
-
return true;
|
|
39
|
-
}
|
|
40
|
-
const includeSchema = {};
|
|
41
|
-
const entity = relation ? new relation() : new this._modelName();
|
|
42
|
-
Object.keys(entity)
|
|
43
|
-
.filter((key) => !['_id', '_action'].includes(key))
|
|
44
|
-
.forEach((key) => {
|
|
45
|
-
let includes;
|
|
46
|
-
if (entity[key] instanceof list_1.List) {
|
|
47
|
-
if (forList) {
|
|
48
|
-
includeSchema[key] = true;
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
includes = this.autogenerateIncludeSchema(forList, entity[key].entityType);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
else if (entity[key] instanceof entity_base_1.EntityBase) {
|
|
55
|
-
includes = this.autogenerateIncludeSchema(forList, entity[key].constructor);
|
|
56
|
-
}
|
|
57
|
-
if (includes) {
|
|
58
|
-
if (includes === true || Object.keys(includes).length > 0) {
|
|
59
|
-
includeSchema[key] = includes;
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
includeSchema[key] = true;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
this.deepIncludeCount += 1;
|
|
67
|
-
return includeSchema;
|
|
68
|
-
}
|
|
69
44
|
getSelectRootPrismaSchema(entity) {
|
|
70
45
|
const selectSchema = {};
|
|
71
46
|
Object.keys(entity)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Type } from '@nestjs/common';
|
|
2
|
+
import { EntityBase } from '../database/entity.base';
|
|
3
|
+
export interface GeneratePrismaIncludeSchemaOptions {
|
|
4
|
+
entity: Type<EntityBase<any>>;
|
|
5
|
+
deepLimit: number;
|
|
6
|
+
deepIncludeCount?: number;
|
|
7
|
+
forList?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare function generateIncludeSchema({ forList, entity, deepIncludeCount, deepLimit, }: GeneratePrismaIncludeSchemaOptions): {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateIncludeSchema = generateIncludeSchema;
|
|
4
|
+
const entity_base_1 = require("../database/entity.base");
|
|
5
|
+
const list_1 = require("./list");
|
|
6
|
+
const auto_mapping_list_1 = require("../mapping/auto-mapping-list");
|
|
7
|
+
function generateIncludeSchema({ forList, entity, deepIncludeCount = 0, deepLimit, }) {
|
|
8
|
+
if (deepIncludeCount >= deepLimit) {
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
const includeSchema = {};
|
|
12
|
+
const entityInstance = new entity();
|
|
13
|
+
Object.keys(entityInstance)
|
|
14
|
+
.filter((key) => !['_id', '_action'].includes(key))
|
|
15
|
+
.forEach((key) => {
|
|
16
|
+
let includes;
|
|
17
|
+
if (entityInstance[key] instanceof list_1.List) {
|
|
18
|
+
if (forList) {
|
|
19
|
+
includeSchema[key] = true;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
includes = generateIncludeSchema({
|
|
23
|
+
forList,
|
|
24
|
+
entity: entityInstance[key].entityType,
|
|
25
|
+
deepLimit,
|
|
26
|
+
deepIncludeCount: deepIncludeCount > 0 ? deepIncludeCount + 1 : 1,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
const propDefinitions = auto_mapping_list_1.AutoMappingList.getPropDefinitions(entityInstance.constructor, key);
|
|
32
|
+
if (propDefinitions) {
|
|
33
|
+
const source = auto_mapping_list_1.AutoMappingList.getSourceByName(propDefinitions.type);
|
|
34
|
+
if (source?.prototype instanceof entity_base_1.EntityBase) {
|
|
35
|
+
includes = generateIncludeSchema({
|
|
36
|
+
forList,
|
|
37
|
+
entity: source,
|
|
38
|
+
deepLimit,
|
|
39
|
+
deepIncludeCount: deepIncludeCount > 0 ? deepIncludeCount + 1 : 1,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (includes) {
|
|
45
|
+
if (includes === true || Object.keys(includes).length > 0) {
|
|
46
|
+
includeSchema[key] = includes;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
includeSchema[key] = true;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return includeSchema;
|
|
54
|
+
}
|