P
Prisma3mo 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, }); }
2 Replies
jonfanz
jonfanz3mo ago
Please use triple ticks ( ` three times) so that code is formatted. I think I need more information to understand what you're trying to achieve. Is your API or code calling your repository or service? Generally speaking, you probably don't need a repository pattern. Are you using that to ensure that prisma is a singleton? I would have written your service code like so:
await this.prisma.$transaction([
this.prisma.company.create({ data: companyBody })
])
await this.prisma.$transaction([
this.prisma.company.create({ data: companyBody })
])
But also note: there's only one call there. One db call in a transaction is the same as not having the transaction at all
await this.prisma.company.create({ data: companyBody })
await this.prisma.company.create({ data: companyBody })
You can learn more about transactions here: https://www.prisma.io/docs/orm/prisma-client/queries/transactions#transactions-overview
Transactions and batch queries (Reference) | Prisma Documentation
This page explains the transactions API of Prisma Client.
Rev
Rev3mo ago
It was just an example, in my project i have repository pattern to abstract the queries because i have many queries that have more than 500 lines to filter, select, order by etc and if i dont use repository pattern my service class would have 30k of lines I thought that was possible to create a function that generated one transaction and pass through the transaction instance like:
startTransaction()

try {
saveCompany()
editCompany()
getCompanyUpdated()

commitTransaction()
} catch {
rollbackTransaction()
}
startTransaction()

try {
saveCompany()
editCompany()
getCompanyUpdated()

commitTransaction()
} catch {
rollbackTransaction()
}
your example case is usefull but i need to make N calls in transaction and most part is not followed by each other, i call function X and then make and http request for another service and then i call function Y and i can't do
await this.prisma.$transaction([
functionX()
functionY()
])
await this.prisma.$transaction([
functionX()
functionY()
])
because between them i need to make request for http external service after functionX
Want results from more Discord servers?
Add your server