import { Prisma } from '../../generated/prisma'; import { PrismaService } from './prisma.service'; /** * The root client or the client of an interactive transaction, for helpers * that work on either (e.g. notifyWorker). A helper that must run inside the * caller's business transaction takes Prisma.TransactionClient instead and * checks it with assertTransactionClient. */ export type DbClient = PrismaService | Prisma.TransactionClient; /** * Throws if `db` is the root client: a write through it would commit on its * own, even if the caller's business transaction rolls back. `$connect` is * withheld from transaction clients; `$transaction` does not tell the two * apart, because a Prisma 7 transaction client can open nested transactions. * `caller` names the method in the error, e.g. "DomainEventsService.recordAll". */ export function assertTransactionClient(db: DbClient, caller: string): void { if ('$connect' in db) { throw new Error(`${caller} needs the transaction client`); } }