What is the type of transaction prop?

When creating a transaction I get this prop
const transaction = client.transaction((tx) => {

})
const transaction = client.transaction((tx) => {

})
I am trying to define a function like this
async function createUser(client:Client | Transaction = db, data:Data){
client.insert(...)
}
async function createUser(client:Client | Transaction = db, data:Data){
client.insert(...)
}
This way I can reuse the same functions with and without a transaction, I am having trouble typing the Transaction type, the type in vscode is a long complex type and I was wondering if there is some exported type I could use
4 Replies
hachoter
hachoterOP2y ago
right now I am typing it like this
export type Transaction = PgTransaction<PostgresJsQueryResultHKT, typeof import('/Users/john/Desktop/projects/project/packages/db/src/schema/index'), ExtractTablesWithRelations<typeof import('/Users/john/Desktop/projects/project/packages/db/src/schema/index')>>;
export type Transaction = PgTransaction<PostgresJsQueryResultHKT, typeof import('/Users/john/Desktop/projects/project/packages/db/src/schema/index'), ExtractTablesWithRelations<typeof import('/Users/john/Desktop/projects/project/packages/db/src/schema/index')>>;
but I want to get rid of at least the imports, it's gonna mess up when building on cloud
Md Jahidul Islam milon
export DbType= typeof<db> this should work. simple typescript
rphlmr ⚡
rphlmr ⚡2y ago
For postgres it is
import { type PostgresJsDatabase } from "drizzle-orm/postgres-js";
import { type PostgresJsDatabase } from "drizzle-orm/postgres-js";
G$Row
G$Row7mo ago
If anyone else runs across this here's what worked for me. This is an easy way to get the actual transaction type. (Using typeof db may work but it isn't the same.) tx: Parameters<Parameters<typeof db.transaction>[0]>[0]
Want results from more Discord servers?
Add your server