import { neon } from '@neondatabase/serverless' import { drizzle as drizzleNeon } from 'drizzle-orm/neon-http' import * as schema from '../db/schema' export type Db = ReturnType> let _db: Db | undefined /** * Override the DB returned by `useDb()`. Tests inject a PGlite-backed Drizzle * instance with the same schema. Production code never calls this. */ export function setDb(db: Db) { _db = db } /** Clear the cached DB so the next `useDb()` reconstructs from runtime config. */ export function clearDb() { _db = undefined } export function useDb(): Db { if (_db) return _db const { databaseUrl } = useRuntimeConfig() if (!databaseUrl) { throw new Error('NUXT_DATABASE_URL is not set') } const client = neon(databaseUrl) _db = drizzleNeon(client, { schema }) return _db } export { schema }