import type { Env } from '@zync/types'
import type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'
import { createPlatformDb } from './platform-driver'
import type * as schema from './schema'

export type Db = PostgresJsDatabase<typeof schema>

export function createDb(env: Env): Db {
  return createPlatformDb(env)
}

/**
 * Transaction handle passed to a `db.transaction(async (tx) => …)` callback.
 * Query helpers that may run either standalone or inside a transaction should
 * accept `Db | DbTx` so callers can hand them `tx` without a cast.
 */
export type DbTx = Parameters<Parameters<Db['transaction']>[0]>[0]
