PrismaP
Prisma2y ago
Rev

There are easy way to use transaction in prisma?

I have the controller, service and repository and only the repository must have the prisma client implementation, but i need to use prisma service inside the service to initialize an transaction and pass to the repository the transaction instance...
in spring boot i only need to add the @Transaction in the main service function and everthing inside is in transaction

Service:
await this.prisma.$transaction(
async (transaction: Prisma.TransactionClient) => {
const createdCompany = await this.companyRepository.createCompany(
companyCreate,
transaction,
);
})

Repository:
async createCompany(
companyBody: PrismaInterfaces.CompanyCreateInput,
transaction?: PrismaInterfaces.TransactionClient,
) {
const prisma = transaction || this.prisma;
return await prisma.company.create({
data: companyBody,
});
}
Was this page helpful?