Transaction Type

Is there a type to use for passing a transaction into a function? I would like to do something like
await db.transaction(async (tx) => insertUser(tx, form.data))
await db.transaction(async (tx) => insertUser(tx, form.data))
but the type of tx is super long and not convenient at all.
4 Replies
Gabriel
Gabriel2w ago
recent update changed the types and now I cant fit a Drizzle Transaction type into a normal Drizzle type. I'm not sure how aware the team is about it
Jakesdoc
Jakesdoc2w ago
Yea I used to be able to just do typeof db and that was good enough but now the new update has changed the types. It doesn't seem there has ever been a proper way to represent that type, just work arounds.
Dave
Dave5d ago
did you guys ever find a solution?
szeth
szeth4d ago
type DB = NodePgDatabase<Schema>;

// option 1. extract it from the db object itself
type ExtractTQuery<T> = T extends PgDatabase<infer TQueryResult, infer Schema>
? [TQueryResult, Schema] : never;
type Transaction<T> = PgTransaction<ExtractTQuery<T>[0], ExtractTQuery<T>[1],
ExtractTablesWithRelations<ExtractTQuery<T>[1]>>;

type TX = Transaction<App.DB>;

// option 2 (extract it from the transaction method)
type TX = Parameters<DB[' transaction']>[0] extends (tx: infer T) => Promise<unknown>
? T: never
type DB = NodePgDatabase<Schema>;

// option 1. extract it from the db object itself
type ExtractTQuery<T> = T extends PgDatabase<infer TQueryResult, infer Schema>
? [TQueryResult, Schema] : never;
type Transaction<T> = PgTransaction<ExtractTQuery<T>[0], ExtractTQuery<T>[1],
ExtractTablesWithRelations<ExtractTQuery<T>[1]>>;

type TX = Transaction<App.DB>;

// option 2 (extract it from the transaction method)
type TX = Parameters<DB[' transaction']>[0] extends (tx: infer T) => Promise<unknown>
? T: never
i had this same problem today, solved it this way i personally prefer option 1 since getting it from the params seems a bit janky (im on mobile so excuse the formatting and typos)
Want results from more Discord servers?
Add your server