To do an "interactive" transaction in Prisma, one writes:
prisa.$transaction(async (txnClient) => {
// Write code that uses txnClient just like a non-transactional PrismaClient instance.
Given an instance of PrismaClient, is there a way to test if it's currently inside of a transaction (i.e. it's a txnClient
from above)?
My use-case is to optionally share a transaction across two functions that both must wrap their own code in a transaction, e.g. each could look like:
function someWork(prisma: PrismaClient): Promise<unknown> {
if (prisma.isAlreadyInTransaction) {
// Do the work
} else {
// Start a transaction and then do the work
}
It does seem that the test '$transaction' in prisma
would work, but is that official API or just coincidental?