26 lines
752 B
TypeScript
26 lines
752 B
TypeScript
|
|
import { drizzle } from "drizzle-orm/postgres-js";
|
||
|
|
import postgres from "postgres";
|
||
|
|
import * as schema from "./schema";
|
||
|
|
|
||
|
|
const connectionString = process.env.DATABASE_URL;
|
||
|
|
if (!connectionString) {
|
||
|
|
throw new Error("DATABASE_URL environment variable is required");
|
||
|
|
}
|
||
|
|
|
||
|
|
const isProduction = process.env.NODE_ENV === "production";
|
||
|
|
const requireSsl =
|
||
|
|
process.env.DATABASE_REQUIRE_SSL === "true" ||
|
||
|
|
(isProduction && process.env.DATABASE_REQUIRE_SSL !== "false");
|
||
|
|
|
||
|
|
const client = postgres(connectionString, {
|
||
|
|
ssl: requireSsl ? "require" : undefined,
|
||
|
|
connect_timeout: 10,
|
||
|
|
idle_timeout: 20,
|
||
|
|
max: 20,
|
||
|
|
});
|
||
|
|
export const db = drizzle(client, { schema });
|
||
|
|
|
||
|
|
export type Database = typeof db;
|
||
|
|
export { schema };
|
||
|
|
export { runMigrations } from "./migrate";
|