Prisma transaction client prop drilling
Transactions inside transactions, is that possible?
Anyone got experience with primsa transactions?
postgresql - Prisma: how to write transaction where results from one query are used by another query - Stack Overflow
Suppose I use prisma transaction this way:
this.prisma.$transaction(async (tx)=>{
const x1 = await this.x1Service.someTransaction(payload2,tx);
const x2 = await this.x2Service.someTransaction(x1,payload2,tx);
....
});Also suppose X1Service and X2Service pass over too many layers to reach prisma client, in order to transaction works I'd need to prop pass tx over all those layers. Is there a good way to manage it.
First Prisma transaction and it seemed simple enough. Doing the sequential method per the docs.
An operation failed because it depends on one or more records that were required but not found. Record to delete does not exist.
These records do exist, and I doubled checked the session id that it does match.
Deleting a person's account so want to make sure everything is deleted or not at all. I delete the entities related to account first because foreign key constraint. All using the sessionId to find appropriate item.
If one of the entities do not exist, like user never made products. Should this still work? I tried both ways. With products and without and still get the same error and no matter which order I do the transaction.
await prisma.$transaction([
prisma.store.delete({ where: { accountId: req.session.id }}),
prisma.product.deleteMany({ where: { accountId: req.session.id } }),
prisma.account.delete({
where: {
id: req.session.id,
},
}),
]);this code is also work and this is the 1st way
import { PrismaClient } from "@prisma/client";
export type PrismaTransactionalClient = Parameters<
Parameters<PrismaClient['$transaction']>[0]
>[0];
but there is a 2nd way and this is the official way
import { Prisma } from "@prisma/client";
export type PrismaTransactionalClient = Prisma.TransactionClient;
The type in the (EDIT: formerly) accepted answer is no longer correct in the latest version of Prisma.
As a more future-proof type, you can use the Parameters type helper to extract the type from the $transaction method itself.
This will work even if the transaction type changes as long as the $transaction's method signature stays the same.
import { PrismaClient } from "@prisma/client";
export type PrismaTransactionalClient = Parameters<
Parameters<PrismaClient['$transaction']>[0]
>[0];

