dev env setup
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
import { PrismaPg } from "@prisma/adapter-pg";
|
||||
import { type Request } from "express";
|
||||
import { PrismaClient } from "../../generated/prisma/client.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,
|
||||
@@ -33,9 +39,9 @@ function createExtendedClient(client: PrismaClient) {
|
||||
// });
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class DatabaseService {
|
||||
private readonly prismaClient: ExtendedClient;
|
||||
private readonly safeClient: SafeClient;
|
||||
|
||||
public constructor() {
|
||||
const dbUser = process.env["TKD_DB_USER"];
|
||||
@@ -46,17 +52,19 @@ export class DatabaseService {
|
||||
|
||||
const adapter = new PrismaPg({ connectionString });
|
||||
this.prismaClient = createExtendedClient(new PrismaClient({ adapter }));
|
||||
this.safeClient = this.prismaClient as SafeClient;
|
||||
|
||||
// this.prismaClient.$connect();
|
||||
// this.prismaClient.$disconnect();
|
||||
// this.prismaClient.$on();
|
||||
// this.prismaClient.$transaction();
|
||||
}
|
||||
|
||||
public async doRequest<T>(
|
||||
request: (prisma: SafeClient) => Promise<T>,
|
||||
command: (prisma: SafeClient) => Promise<T>,
|
||||
request: Request,
|
||||
): Promise<T> {
|
||||
return await request(this.safeClient);
|
||||
const transaction = (request as any).transaction as SafeClient;
|
||||
return await command(transaction);
|
||||
}
|
||||
|
||||
public async createTransaction(
|
||||
action: (prisma: TransactionClient) => Promise<void>,
|
||||
) {
|
||||
await this.prismaClient.$transaction(action);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user