mpellegrini / fullstack-typescript-monorepo-starter

1 stars 1 forks source link

Define generic types for Drizzle Database and Transaction to be able to use them interchangeably #108

Open mpellegrini opened 1 month ago

mpellegrini commented 1 month ago
const doWork = (..., tx: Transaction = db) {}

By doing this a given function can either be a standalone database operation or can be part of a higher up defined transaction

Here is one way to define the types

export type DB = typeof db;
export type TX = Parameters<Parameters<DB['transaction']>[0]>[0];
export type DbOrTx = DB | TX;

and here is another

export type Transaction =
  | typeof db
  | Parameters<Parameters<typeof db.transaction>[0]>[0];