How to type the Postgres transaction object?

I'm generating a transaction object using the following setup
await db.transaction(async (tx) => {
....
}
await db.transaction(async (tx) => {
....
}
What I'm trying to achieve is to be able to type the tx object when I pass it as a function but I'm not sure what the type should be.
const someFunctionCall = (tx : WhatIsTheTypeHere) => {
...
}
const someFunctionCall = (tx : WhatIsTheTypeHere) => {
...
}
6 Replies
Mykhailo
Mykhailo12mo ago
Hello, @Jude! Try this
PgTransaction<PostgresJsQueryResultHKT, typeof schema, ExtractTablesWithRelations<typeof schema>>
PgTransaction<PostgresJsQueryResultHKT, typeof schema, ExtractTablesWithRelations<typeof schema>>
francis
francis12mo ago
I do this:
export type DrizzleClient = ReturnType<typeof drizzle>;
export type DrizzleTransaction = Parameters<Parameters<DrizzleClient["transaction"]>[0]>[0];
export type DrizzleClient = ReturnType<typeof drizzle>;
export type DrizzleTransaction = Parameters<Parameters<DrizzleClient["transaction"]>[0]>[0];
Jude
JudeOP12mo ago
Thanks guys. Hey @solo is schema here an instance of the pgTable schema or something else? Hey @francis could you let me know where the drizzle object comes from? How should one import that?
francis
francis12mo ago
for the database driver I use it's import { drizzle } from "drizzle-orm/postgres-js"; but the point is to get the type of the client that you would call .transaction on. The Parameters.... etc type is to extract the type of the transaction object from that method.
Mykhailo
Mykhailo12mo ago
@Jude it's import of all tables from your schema.ts file
import * as schema from '../drizzle/schema';
import * as schema from '../drizzle/schema';
Jude
JudeOP12mo ago
Thanks guys, that really worked!!
Want results from more Discord servers?
Add your server