Spaces:
Sleeping
Sleeping
| import { drizzle, NodePgDatabase } from "drizzle-orm/node-postgres"; | |
| import pg, {Pool} from "pg"; | |
| import * as schema from "@/schema/schema"; | |
| declare global { | |
| var db: undefined | NodePgDatabase<typeof schema>; | |
| var pool: undefined | Pool; | |
| } | |
| export function getDatabase() { | |
| if (global.db && global.pool) { | |
| return { pool: global.pool, db: global.db }; | |
| } | |
| if (!process.env.DATABASE_URL) { | |
| throw new Error( | |
| "DATABASE_URL must be set. Did you forget to provision a database?", | |
| ); | |
| } | |
| const pool = new Pool({ connectionString: process.env.DATABASE_URL }); | |
| const db = drizzle(pool, { schema }); | |
| global.db = db; | |
| global.pool = pool; | |
| return { | |
| pool, | |
| db, | |
| } | |
| } | |