import { Kysely, sql } from 'kysely' import { PostgresJSDialect } from 'kysely-postgres-js' import postgres from 'postgres' import type { Database } from '../database/schema' let _db: Kysely | null = null function getDb(): Kysely { if (_db) return _db const databaseUrl = useRuntimeConfig().databaseUrl || process.env.DATABASE_URL if (!databaseUrl) throw new Error('DATABASE_URL is not set') const isLocal = databaseUrl.includes('localhost') || databaseUrl.includes('127.0.0.1') _db = new Kysely({ dialect: new PostgresJSDialect({ postgres: postgres(databaseUrl, { ssl: isLocal ? false : 'require', max: 5, idle_timeout: 20, connect_timeout: 30, onnotice: () => {}, }), }), }) return _db } export const db = new Proxy({} as Kysely, { get: (_, prop) => { const real = getDb() const value = (real as any)[prop] return typeof value === 'function' ? value.bind(real) : value }, }) export { getDb, sql }