mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2026-07-11 21:45:04 +10:00
33 lines
840 B
TypeScript
33 lines
840 B
TypeScript
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
import { drizzle } from "drizzle-orm/node-postgres";
|
|
import { Pool } from "pg";
|
|
import { env } from "@reactive-resume/env/server";
|
|
import { relations } from "./relations";
|
|
import * as schema from "./schema";
|
|
|
|
declare global {
|
|
var __pool: Pool | undefined;
|
|
var __drizzle: NodePgDatabase<typeof schema> | undefined;
|
|
}
|
|
|
|
export function getPool() {
|
|
if (!globalThis.__pool) {
|
|
globalThis.__pool = new Pool({ connectionString: env.DATABASE_URL });
|
|
}
|
|
return globalThis.__pool;
|
|
}
|
|
|
|
function makeDrizzleClient() {
|
|
const pool = getPool();
|
|
return drizzle({ client: pool, schema, relations });
|
|
}
|
|
|
|
export function createDatabase() {
|
|
if (!globalThis.__drizzle) {
|
|
globalThis.__drizzle = makeDrizzleClient();
|
|
}
|
|
return globalThis.__drizzle;
|
|
}
|
|
|
|
export const db = createDatabase();
|