event subscriptions

This commit is contained in:
toni
2026-03-15 15:39:45 +01:00
parent 29c6828dd1
commit 2620538a2c
26 changed files with 1028 additions and 214 deletions
+13 -28
View File
@@ -5,10 +5,6 @@ import type { UserModel } from "../../generated/prisma/models.js";
import { Injectable } from "../../infrastructure/di/injectable-decorator.js";
type ExtendedClient = ReturnType<typeof createExtendedClient>;
// type TransactionClient = Omit<
// ExtendedClient,
// "$connect" | "$disconnect" | "$on" | "$transaction" | "$extends"
// >;
export type SafeClient = Omit<
ExtendedClient,
@@ -26,28 +22,17 @@ export type SafeClient = Omit<
function createExtendedClient(client: PrismaClient) {
return client.$extends({
client: {
async ensureUserKnown(userId: string): Promise<UserModel>{
let user = await client.user.findUnique({where: {id: userId}});
if(user==null){
user = await client.user.create({data:{id: userId}});
async ensureUserKnown(userId: string): Promise<UserModel> {
let user = await client.user.findUnique({
where: { id: userId },
});
if (user == null) {
user = await client.user.create({ data: { id: userId } });
}
return user
}
}
return user;
},
},
});
// .$extends({
// name: "testExtension",
// model: {
// member: {
// withFullName(member: Member) {
// return {
// ...member,
// fullName: ` ${member.vorname} ${member.nachname}`,
// };
// },
// },
// },
// });
}
@Injectable()
@@ -69,9 +54,9 @@ export class DatabaseService {
command: (prisma: SafeClient) => Promise<T>,
request: Request | null,
): Promise<T> {
const transaction =
(request as any | null)?.transaction ??
(this.prismaClient as SafeClient);
return await command(transaction);
const transaction: SafeClient | null = (request as any | null)
?.transaction;
const effectiveClient = transaction ?? this.prismaClient;
return await command(effectiveClient);
}
}