@koalarx/nest 1.12.4 → 1.12.6
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,12 +2,13 @@ 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';
|
|
5
6
|
import { EntityBase } from './entity.base';
|
|
6
7
|
import { PrismaTransactionalClient } from './prisma-transactional-client';
|
|
7
|
-
type RepositoryInclude<TEntity> = {
|
|
8
|
-
[
|
|
9
|
-
}
|
|
10
|
-
interface RepositoryInitProps<TEntity
|
|
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
|
+
interface RepositoryInitProps<TEntity extends EntityBase<TEntity>> {
|
|
11
12
|
context: PrismaTransactionalClient;
|
|
12
13
|
modelName: Type<TEntity>;
|
|
13
14
|
transactionContext?: Type<PrismaTransactionalClient>;
|
|
@@ -33,5 +34,6 @@ export declare abstract class RepositoryBase<TEntity extends EntityBase<TEntity>
|
|
|
33
34
|
private createEntity;
|
|
34
35
|
private orphanRemoval;
|
|
35
36
|
private getIdPropName;
|
|
37
|
+
private getInclude;
|
|
36
38
|
}
|
|
37
39
|
export {};
|
|
@@ -23,7 +23,7 @@ class RepositoryBase {
|
|
|
23
23
|
async findById(id) {
|
|
24
24
|
return this.context()
|
|
25
25
|
.findFirst({
|
|
26
|
-
include: this.
|
|
26
|
+
include: this.getInclude(),
|
|
27
27
|
where: { [this.getIdPropName()]: id },
|
|
28
28
|
})
|
|
29
29
|
.then((response) => {
|
|
@@ -36,7 +36,7 @@ class RepositoryBase {
|
|
|
36
36
|
async findFirst(where) {
|
|
37
37
|
return this.context()
|
|
38
38
|
.findFirst({
|
|
39
|
-
include: this.
|
|
39
|
+
include: this.getInclude(),
|
|
40
40
|
where,
|
|
41
41
|
})
|
|
42
42
|
.then((response) => {
|
|
@@ -49,7 +49,7 @@ class RepositoryBase {
|
|
|
49
49
|
async findUnique(where) {
|
|
50
50
|
return this.context()
|
|
51
51
|
.findUnique({
|
|
52
|
-
include: this.
|
|
52
|
+
include: this.getInclude(),
|
|
53
53
|
where,
|
|
54
54
|
})
|
|
55
55
|
.then((response) => {
|
|
@@ -78,7 +78,7 @@ class RepositoryBase {
|
|
|
78
78
|
return this.context()
|
|
79
79
|
.create({
|
|
80
80
|
data: prismaEntity,
|
|
81
|
-
include: this.
|
|
81
|
+
include: this.getInclude(),
|
|
82
82
|
})
|
|
83
83
|
.then((response) => this.createEntity(response));
|
|
84
84
|
}
|
|
@@ -162,17 +162,23 @@ class RepositoryBase {
|
|
|
162
162
|
if (entity[key][this.getIdPropName()]) {
|
|
163
163
|
prismaSchema[key] = {
|
|
164
164
|
connectOrCreate: {
|
|
165
|
-
where: {
|
|
165
|
+
where: {
|
|
166
|
+
[this.getIdPropName()]: entity[key][this.getIdPropName()],
|
|
167
|
+
},
|
|
166
168
|
create: this.entityToPrisma(entity[key]),
|
|
167
|
-
}
|
|
169
|
+
},
|
|
168
170
|
};
|
|
169
171
|
}
|
|
170
172
|
else {
|
|
171
|
-
prismaSchema[key] = {
|
|
173
|
+
prismaSchema[key] = {
|
|
174
|
+
create: this.entityToPrisma(entity[key]),
|
|
175
|
+
};
|
|
172
176
|
}
|
|
173
177
|
}
|
|
174
178
|
else {
|
|
175
|
-
prismaSchema[key] = {
|
|
179
|
+
prismaSchema[key] = {
|
|
180
|
+
update: this.entityToPrisma(entity[key]),
|
|
181
|
+
};
|
|
176
182
|
}
|
|
177
183
|
}
|
|
178
184
|
else {
|
|
@@ -192,7 +198,7 @@ class RepositoryBase {
|
|
|
192
198
|
}
|
|
193
199
|
findManySchema(where, pagination) {
|
|
194
200
|
return {
|
|
195
|
-
include: this.
|
|
201
|
+
include: this.getInclude(),
|
|
196
202
|
where,
|
|
197
203
|
orderBy: pagination?.generateOrderBy(),
|
|
198
204
|
skip: pagination?.skip(),
|
|
@@ -215,5 +221,20 @@ class RepositoryBase {
|
|
|
215
221
|
getIdPropName() {
|
|
216
222
|
return Reflect.getMetadata('entity:id', this._modelName.prototype) ?? 'id';
|
|
217
223
|
}
|
|
224
|
+
getInclude(include) {
|
|
225
|
+
include = include ?? this._include ?? {};
|
|
226
|
+
const result = {};
|
|
227
|
+
Object.keys(include).forEach((key) => {
|
|
228
|
+
if (typeof include[key] === 'boolean') {
|
|
229
|
+
result[key] = include[key];
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
result[key] = {
|
|
233
|
+
include: this.getInclude(include[key]),
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
return result;
|
|
238
|
+
}
|
|
218
239
|
}
|
|
219
240
|
exports.RepositoryBase = RepositoryBase;
|
package/package.json
CHANGED
package/test/koala-app-test.d.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import { INestApplication, Type } from '@nestjs/common';
|
|
1
|
+
import { CanActivate, INestApplication, Type } from '@nestjs/common';
|
|
2
2
|
import { BaseExceptionFilter } from '@nestjs/core';
|
|
3
3
|
import { PrismaTransactionalClient } from '../core/database/prisma-transactional-client';
|
|
4
4
|
export declare class KoalaAppTest {
|
|
5
5
|
private readonly app;
|
|
6
|
+
private _guards;
|
|
6
7
|
private _globalExceptionFilter;
|
|
7
8
|
private _prismaValidationExceptionFilter;
|
|
8
9
|
private _domainExceptionFilter;
|
|
9
10
|
private _zodExceptionFilter;
|
|
10
11
|
constructor(app: INestApplication<any>);
|
|
12
|
+
addGlobalGuard(Guard: Type<CanActivate>): this;
|
|
11
13
|
addCustomGlobalExceptionFilter(filter: BaseExceptionFilter): this;
|
|
12
14
|
addCustomPrismaValidationExceptionFilter(filter: BaseExceptionFilter): this;
|
|
13
15
|
addCustomDomainExceptionFilter(filter: BaseExceptionFilter): this;
|
package/test/koala-app-test.js
CHANGED
|
@@ -10,6 +10,7 @@ const zod_errors_filter_1 = require("../filters/zod-errors.filter");
|
|
|
10
10
|
const ilogging_service_1 = require("../services/logging/ilogging.service");
|
|
11
11
|
class KoalaAppTest {
|
|
12
12
|
app;
|
|
13
|
+
_guards = [];
|
|
13
14
|
_globalExceptionFilter;
|
|
14
15
|
_prismaValidationExceptionFilter;
|
|
15
16
|
_domainExceptionFilter;
|
|
@@ -25,6 +26,10 @@ class KoalaAppTest {
|
|
|
25
26
|
this._domainExceptionFilter = new domain_errors_filter_1.DomainErrorsFilter(loggingService);
|
|
26
27
|
this._zodExceptionFilter = new zod_errors_filter_1.ZodErrorsFilter(loggingService);
|
|
27
28
|
}
|
|
29
|
+
addGlobalGuard(Guard) {
|
|
30
|
+
this._guards.push((0, instanciate_class_with_dependencies_injection_1.instanciateClassWithDependenciesInjection)(this.app, Guard));
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
28
33
|
addCustomGlobalExceptionFilter(filter) {
|
|
29
34
|
this._globalExceptionFilter = filter;
|
|
30
35
|
return this;
|
|
@@ -63,6 +68,9 @@ class KoalaAppTest {
|
|
|
63
68
|
}
|
|
64
69
|
build() {
|
|
65
70
|
this.app.useGlobalFilters(this._globalExceptionFilter, this._prismaValidationExceptionFilter, this._domainExceptionFilter, this._zodExceptionFilter);
|
|
71
|
+
for (const guard of this._guards) {
|
|
72
|
+
this.app.useGlobalGuards(guard);
|
|
73
|
+
}
|
|
66
74
|
return this.app;
|
|
67
75
|
}
|
|
68
76
|
}
|