diff --git a/frontend/src/lib/db/index.ts b/frontend/src/lib/db/index.ts index 758392d..5f6b7d8 100644 --- a/frontend/src/lib/db/index.ts +++ b/frontend/src/lib/db/index.ts @@ -1,7 +1,19 @@ -import { drizzle } from 'drizzle-orm/postgres-js' +import { + drizzle, + type PostgresJsDatabase, +} from 'drizzle-orm/postgres-js' import postgres from 'postgres' import * as schema from './schema' import { env } from '$env/dynamic/private' -const client = postgres(env.DATABASE_URL) -export const db = drizzle(client, { schema }) +declare global { + var db: PostgresJsDatabase | undefined +} + +let db: PostgresJsDatabase +if (!global.db) { + global.db = drizzle(postgres(env.DATABASE_URL), { schema }) +} +db = global.db + +export { db }