20 lines
574 B
TypeScript
20 lines
574 B
TypeScript
import { drizzle } from "drizzle-orm/postgres-js";
|
|
import postgres from "postgres";
|
|
import type { PostgresJsDatabase } from "drizzle-orm/postgres-js";
|
|
import dotenv from "dotenv";
|
|
|
|
dotenv.config();
|
|
|
|
const connectionString = process.env.DATABASE_URL!;
|
|
const driver = postgres(connectionString, { max: 1 });
|
|
let db: PostgresJsDatabase = drizzle(driver);
|
|
|
|
if (process.env.NODE_ENV !== "production") {
|
|
const g = globalThis as unknown as { drizzleClient: PostgresJsDatabase };
|
|
if (!g.drizzleClient) {
|
|
g.drizzleClient = db;
|
|
}
|
|
db = g.drizzleClient;
|
|
}
|
|
|
|
export { db }; |