Something went wrong. Try again.
mirror your GitHub repos to tangled.org automatically synchub.to
mirror sync tangled github git
Something went wrong. Try again.
857 B · 33 lines
TypeScript
at main
12345678910111213141516171819202122232425262728293031323334import { neon } from '@neondatabase/serverless'import { drizzle as drizzleNeon } from 'drizzle-orm/neon-http'import * as schema from '../db/schema'
export type Db = ReturnType<typeof drizzleNeon<typeof schema>>
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 }