36 lines
811 B
TypeScript
36 lines
811 B
TypeScript
import { drizzle } from "drizzle-orm/postgres-js";
|
|
import postgres from "postgres";
|
|
import * as schema from "./schema";
|
|
|
|
const connectionString = process.env.DATABASE_URL;
|
|
|
|
let client: ReturnType<typeof postgres> | null = null;
|
|
let db: ReturnType<typeof drizzle<typeof schema>> | null = null;
|
|
|
|
export function getDb() {
|
|
if (!connectionString) {
|
|
throw new Error("DATABASE_URL is not set");
|
|
}
|
|
|
|
if (!client) {
|
|
client = postgres(connectionString, { max: 10 });
|
|
db = drizzle(client, { schema });
|
|
}
|
|
|
|
return db!;
|
|
}
|
|
|
|
export async function checkDbConnection(): Promise<boolean> {
|
|
if (!connectionString) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
const probe = postgres(connectionString, { max: 1 });
|
|
await probe`SELECT 1`;
|
|
await probe.end();
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
} |