DT
Drizzle Team•12mo ago
francis

Is there a better way to extract Typescript types for use in function args than what I'm doing here?

export const client = drizzle(adminPgClient, { schema });
export type DrizzleClient = typeof client;
export type DrizzleTransaction = Parameters<Parameters<DrizzleClient["transaction"]>[0]>[0];
export const client = drizzle(adminPgClient, { schema });
export type DrizzleClient = typeof client;
export type DrizzleTransaction = Parameters<Parameters<DrizzleClient["transaction"]>[0]>[0];
It works well enough, but this seems like something which should be exposed on a library level, maybe? I need it in order to e.g. write a function which takes in a transaction and performs operations on it, such that multiple functions can be called on a single transaction and then the transaction either committed or rolled back as one unit (so the client cannot be passed in instead).
6 Replies
francis
francisOP•12mo ago
example:
async function myFunction(tx: DrizzleTransaction, ...) { ... }

await client.transaction(async (tx) => await myFunction(tx); ...)
async function myFunction(tx: DrizzleTransaction, ...) { ... }

await client.transaction(async (tx) => await myFunction(tx); ...)
Angelelz
Angelelz•12mo ago
that tx object is a class that extends the db class, depending on your dialect PGDatabase or something like that So you could do:
export type DrizzleTransaction = typeof client;
export type DrizzleTransaction = typeof client;
You would only be missing the tx.rollback() method, but you don't really need it, you can just throw an error and get the same functionality
francis
francisOP•12mo ago
while true, I would like the ability to force the functions to be called with a transaction rather than a client interface. (and also, my client is an RLS client proxy with select, etc intentionally removed 😉 ) so I'm guessing that means there is no simpler way to get the types? I'll leave this question here for posterity in case anyone else has a similar problem and searches for it
Angelelz
Angelelz•12mo ago
Yeah, my solution is just the type, at runtime they would use whatever you want, a transaction would just fit the type If you want to use the actual type, I guess you could do:
export type DrizzleTransaction = PostgresJsTransaction<typeof schema, ExtractTablesWithRelations<typeof schema>>
export type DrizzleTransaction = PostgresJsTransaction<typeof schema, ExtractTablesWithRelations<typeof schema>>
In case you are using postgres.js as driver
francis
francisOP•12mo ago
yep. the computed type I get from Intellisense is:
type DrizzleTransaction = PgTransaction<PostgresJsQueryResultHKT, typeof schema, ExtractTablesWithRelations<typeof schema>>
type DrizzleTransaction = PgTransaction<PostgresJsQueryResultHKT, typeof schema, ExtractTablesWithRelations<typeof schema>>
(to be clear, my Parameters mess works, it just looks bad and feels like there must be a better way that doesn't involve typing it manually in such a way that a drizzle library update might break it 😉 )
Angelelz
Angelelz•12mo ago
Yeah, your solution is totally fine, I don't think you'll have issues, the API is very likely staying the same
Want results from more Discord servers?
Add your server