Search code examples
typescripttransactionsprismatyping

Pass Prisma transaction into a function in typescript


Hi I have following issue. I have prisma transaction but I would like to pass the prisma transaction client into a function like this:

...
prisma.$transaction(async (tx) => {
  someFunction(tx)
})
...

function someFunction(tx: WHATTOTYPEHERE){
}

However I am doing it in typescript and I don't want to use type ANY. But i dont know how to type the interactive transactin prisma client... the "WHATTOTYPEHERE" type for it.

Any help is appreciated


Solution

  • 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];