26 lines
573 B
TypeScript
26 lines
573 B
TypeScript
import { Kysely, PostgresDialect } from "kysely";
|
|
import { Pool } from "pg";
|
|
|
|
import { getDatabaseUrl } from "@/lib/config";
|
|
import type { Database } from "@/lib/db/types";
|
|
|
|
declare global {
|
|
var __weddingDb: Kysely<Database> | undefined;
|
|
}
|
|
|
|
function createDatabase() {
|
|
return new Kysely<Database>({
|
|
dialect: new PostgresDialect({
|
|
pool: new Pool({
|
|
connectionString: getDatabaseUrl(),
|
|
}),
|
|
}),
|
|
});
|
|
}
|
|
|
|
export const db = globalThis.__weddingDb ?? createDatabase();
|
|
|
|
if (process.env.NODE_ENV !== "production") {
|
|
globalThis.__weddingDb = db;
|
|
}
|