@koalarx/nest 1.18.7 → 1.18.9
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.
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Prisma } from 'prisma/generated/client';
|
|
2
2
|
export declare abstract class PrismaTransactionalClient {
|
|
3
3
|
protected readonly transactionalClient: Prisma.TransactionClient;
|
|
4
|
+
[key: symbol]: any;
|
|
4
5
|
constructor(transactionalClient: Prisma.TransactionClient);
|
|
5
6
|
withTransaction<F>(fn: (prisma: Prisma.TransactionClient) => Promise<F>): Promise<F>;
|
|
6
7
|
$executeRaw(query: TemplateStringsArray | Prisma.Sql, ...values: any[]): Prisma.PrismaPromise<number>;
|
|
@@ -8,33 +8,34 @@ import { PrismaTransactionalClient } from './prisma-transactional-client';
|
|
|
8
8
|
type RepositoryInclude<TEntity> = Omit<{
|
|
9
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
10
|
}, '_id' | '_action'>;
|
|
11
|
-
interface RepositoryInitProps<TEntity extends EntityBase<TEntity
|
|
12
|
-
context:
|
|
11
|
+
interface RepositoryInitProps<TEntity extends EntityBase<TEntity>, TContext extends PrismaTransactionalClient> {
|
|
12
|
+
context: TContext;
|
|
13
13
|
modelName: Type<TEntity>;
|
|
14
|
-
transactionContext?: Type<
|
|
14
|
+
transactionContext?: Type<TContext>;
|
|
15
15
|
include?: RepositoryInclude<TEntity>;
|
|
16
16
|
}
|
|
17
|
-
export declare abstract class RepositoryBase<TEntity extends EntityBase<TEntity
|
|
18
|
-
protected _context:
|
|
17
|
+
export declare abstract class RepositoryBase<TEntity extends EntityBase<TEntity>, TContext extends PrismaTransactionalClient = PrismaTransactionalClient, TModelKey extends keyof TContext = keyof TContext> {
|
|
18
|
+
protected _context: TContext;
|
|
19
19
|
private readonly _modelName;
|
|
20
20
|
private readonly _include?;
|
|
21
|
-
constructor({ context, modelName, include }: RepositoryInitProps<TEntity>);
|
|
22
|
-
|
|
21
|
+
constructor({ context, modelName, include, }: RepositoryInitProps<TEntity, TContext>);
|
|
22
|
+
protected context(transactionalClient?: TContext): TContext[TModelKey];
|
|
23
23
|
protected findById(id: IComparableId): Promise<TEntity | null>;
|
|
24
24
|
protected findFirst<T>(where: T): Promise<TEntity | null>;
|
|
25
25
|
protected findUnique<T>(where: T): Promise<TEntity | null>;
|
|
26
26
|
protected findMany<T>(where: T, pagination?: PaginationDto): Promise<TEntity[]>;
|
|
27
27
|
protected findManyAndCount<T>(where: T, pagination?: PaginationDto): Promise<ListResponse<TEntity>>;
|
|
28
28
|
protected saveChanges<TWhere = any>(entity: TEntity, updateWhere?: TWhere): Promise<TEntity>;
|
|
29
|
+
protected saveMany<TWhere = any>(entities: TEntity[], updateWhere?: TWhere): Promise<void>;
|
|
29
30
|
protected remove<TWhere = any>(where: TWhere, externalServices?: Promise<any>, notCascadeEntityProps?: Array<keyof TEntity>): Promise<any>;
|
|
30
31
|
protected removeMany<TWhere = any>(where: TWhere, externalServices?: Promise<any>, notCascadeEntityProps?: Array<keyof TEntity>): Promise<void>;
|
|
31
32
|
private listToRelationActionList;
|
|
32
33
|
private entityToPrisma;
|
|
33
|
-
private context;
|
|
34
34
|
private findManySchema;
|
|
35
35
|
private createEntity;
|
|
36
36
|
private orphanRemoval;
|
|
37
37
|
private getIdPropName;
|
|
38
38
|
private getInclude;
|
|
39
|
+
withTransaction(fn: (prisma: TContext) => Promise<any>): Promise<any>;
|
|
39
40
|
}
|
|
40
41
|
export {};
|
|
@@ -10,15 +10,20 @@ class RepositoryBase {
|
|
|
10
10
|
_context;
|
|
11
11
|
_modelName;
|
|
12
12
|
_include;
|
|
13
|
-
constructor({ context, modelName, include }) {
|
|
13
|
+
constructor({ context, modelName, include, }) {
|
|
14
14
|
this._context = context;
|
|
15
15
|
this._modelName = modelName;
|
|
16
16
|
this._include = include;
|
|
17
17
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
context(transactionalClient) {
|
|
19
|
+
const modelName = this._modelName.name;
|
|
20
|
+
if (!modelName)
|
|
21
|
+
throw new Error('modelName não informado no contrutor do repositorio');
|
|
22
|
+
const contextKey = (0, KlString_1.toCamelCase)(modelName);
|
|
23
|
+
if (transactionalClient) {
|
|
24
|
+
return transactionalClient[contextKey];
|
|
25
|
+
}
|
|
26
|
+
return this._context[contextKey];
|
|
22
27
|
}
|
|
23
28
|
async findById(id) {
|
|
24
29
|
return this.context()
|
|
@@ -95,9 +100,26 @@ class RepositoryBase {
|
|
|
95
100
|
...relationUpdates.map((relation) => client[relation.modelName].updateMany(relation.schema)),
|
|
96
101
|
...relationDeletes.map((relation) => client[relation.modelName].deleteMany(relation.schema)),
|
|
97
102
|
]);
|
|
98
|
-
})).then(() => this.
|
|
103
|
+
})).then(() => this.findUnique(where));
|
|
99
104
|
}
|
|
100
105
|
}
|
|
106
|
+
async saveMany(entities, updateWhere) {
|
|
107
|
+
await this.withTransaction(async (client) => {
|
|
108
|
+
const prismaEntities = entities.map((entity) => this.entityToPrisma(entity));
|
|
109
|
+
return Promise.all([
|
|
110
|
+
this.context(client).createMany({
|
|
111
|
+
data: prismaEntities,
|
|
112
|
+
skipDuplicates: true,
|
|
113
|
+
}),
|
|
114
|
+
...entities
|
|
115
|
+
.filter((entity) => !!entity._id)
|
|
116
|
+
.map((entity) => this.context(client).update({
|
|
117
|
+
data: entity,
|
|
118
|
+
where: updateWhere ?? { id: entity._id },
|
|
119
|
+
})),
|
|
120
|
+
]);
|
|
121
|
+
});
|
|
122
|
+
}
|
|
101
123
|
async remove(where, externalServices, notCascadeEntityProps) {
|
|
102
124
|
const entity = await this.findUnique(where);
|
|
103
125
|
if (!entity) {
|
|
@@ -213,15 +235,6 @@ class RepositoryBase {
|
|
|
213
235
|
});
|
|
214
236
|
return prismaSchema;
|
|
215
237
|
}
|
|
216
|
-
context(transactionalClient) {
|
|
217
|
-
const modelName = this._modelName.name;
|
|
218
|
-
if (!modelName)
|
|
219
|
-
throw new Error('modelName não informado no contrutor do repositorio');
|
|
220
|
-
if (transactionalClient) {
|
|
221
|
-
return transactionalClient[(0, KlString_1.toCamelCase)(modelName)];
|
|
222
|
-
}
|
|
223
|
-
return this._context[modelName];
|
|
224
|
-
}
|
|
225
238
|
findManySchema(where, pagination) {
|
|
226
239
|
return {
|
|
227
240
|
include: this.getInclude(),
|
|
@@ -262,5 +275,10 @@ class RepositoryBase {
|
|
|
262
275
|
});
|
|
263
276
|
return result;
|
|
264
277
|
}
|
|
278
|
+
withTransaction(fn) {
|
|
279
|
+
return this._context.withTransaction(async (client) => {
|
|
280
|
+
return fn(new koala_global_vars_1.KoalaGlobalVars.dbTransactionContext(client));
|
|
281
|
+
});
|
|
282
|
+
}
|
|
265
283
|
}
|
|
266
284
|
exports.RepositoryBase = RepositoryBase;
|