Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 627
StykPohlavsson Avatar asked May 11 '26 18:05

StykPohlavsson


1 Answers

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;
like image 74
Vishal Kumar Avatar answered May 14 '26 06:05

Vishal Kumar