@koalarx/nest 3.0.11 → 3.1.0
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.
|
@@ -2,26 +2,25 @@ import { Type } from '@nestjs/common';
|
|
|
2
2
|
import { ListResponse } from '..';
|
|
3
3
|
import { PaginationDto } from '../dtos/pagination.dto';
|
|
4
4
|
import { IComparableId } from '../utils/interfaces/icomparable';
|
|
5
|
-
import { List } from '../utils/list';
|
|
6
5
|
import { EntityBase } from './entity.base';
|
|
7
6
|
import { PrismaTransactionalClient } from './prisma-transactional-client';
|
|
8
|
-
type RepositoryInclude<TEntity> = Omit<{
|
|
9
|
-
[K in keyof TEntity as TEntity[K] extends Function ? never : K]?: boolean | (TEntity[K] extends List<infer U> ? RepositoryInclude<U> : RepositoryInclude<TEntity[K]>);
|
|
10
|
-
}, '_id' | '_action'>;
|
|
11
7
|
interface RepositoryInitProps<TEntity extends EntityBase<TEntity>, TContext extends PrismaTransactionalClient> {
|
|
12
8
|
context: TContext;
|
|
13
9
|
modelName: Type<TEntity>;
|
|
14
10
|
transactionContext?: Type<TContext>;
|
|
15
|
-
|
|
16
|
-
includeFindMany?: RepositoryInclude<TEntity>;
|
|
11
|
+
deepIncludeLimit?: number;
|
|
17
12
|
}
|
|
18
13
|
export declare abstract class RepositoryBase<TEntity extends EntityBase<TEntity>, TContext extends PrismaTransactionalClient = PrismaTransactionalClient, TModelKey extends keyof TContext = keyof TContext> {
|
|
19
14
|
protected _context: TContext;
|
|
20
15
|
private readonly _modelName;
|
|
21
16
|
private readonly _include?;
|
|
22
17
|
private readonly _includeFindMany?;
|
|
23
|
-
|
|
18
|
+
private readonly deepIncludeLimit;
|
|
19
|
+
private deepIncludeCount;
|
|
20
|
+
private resetDeepIncludeCount;
|
|
21
|
+
constructor({ context, modelName, deepIncludeLimit, }: RepositoryInitProps<TEntity, TContext>);
|
|
24
22
|
private getConnectPrismaSchemaForRelation;
|
|
23
|
+
private autogenerateIncludeSchema;
|
|
25
24
|
private getSelectRootPrismaSchema;
|
|
26
25
|
private getPropNameFromEntitySource;
|
|
27
26
|
private listRelationEntities;
|
|
@@ -12,12 +12,18 @@ class RepositoryBase {
|
|
|
12
12
|
_modelName;
|
|
13
13
|
_include;
|
|
14
14
|
_includeFindMany;
|
|
15
|
-
|
|
15
|
+
deepIncludeLimit;
|
|
16
|
+
deepIncludeCount = 0;
|
|
17
|
+
resetDeepIncludeCount() {
|
|
18
|
+
this.deepIncludeCount = 0;
|
|
19
|
+
}
|
|
20
|
+
constructor({ context, modelName, deepIncludeLimit, }) {
|
|
16
21
|
this._context = context;
|
|
17
22
|
this._modelName = modelName;
|
|
18
|
-
this.
|
|
19
|
-
this.
|
|
20
|
-
|
|
23
|
+
this.deepIncludeLimit = deepIncludeLimit ?? 5;
|
|
24
|
+
this._include = this.autogenerateIncludeSchema();
|
|
25
|
+
this.resetDeepIncludeCount();
|
|
26
|
+
this._includeFindMany = this.autogenerateIncludeSchema(true);
|
|
21
27
|
}
|
|
22
28
|
getConnectPrismaSchemaForRelation(entity, data) {
|
|
23
29
|
const propIdName = this.getIdPropName(entity);
|
|
@@ -27,6 +33,39 @@ class RepositoryBase {
|
|
|
27
33
|
},
|
|
28
34
|
};
|
|
29
35
|
}
|
|
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
|
+
}
|
|
30
69
|
getSelectRootPrismaSchema(entity) {
|
|
31
70
|
const selectSchema = {};
|
|
32
71
|
Object.keys(entity)
|
|
@@ -87,7 +126,7 @@ class RepositoryBase {
|
|
|
87
126
|
const entityInstance = list.entityType;
|
|
88
127
|
const modelName = entityInstance.name;
|
|
89
128
|
const parentModelName = entity.constructor.name;
|
|
90
|
-
const parentPropName = this.getPropNameFromEntitySource(new entityInstance(), entity.constructor) ?? parentModelName;
|
|
129
|
+
const parentPropName = this.getPropNameFromEntitySource(new entityInstance(), entity.constructor) ?? (0, KlString_1.toCamelCase)(parentModelName);
|
|
91
130
|
if (modelName) {
|
|
92
131
|
list.toArray('removed').forEach((item) => {
|
|
93
132
|
relationDeletes.push({
|