Initial commit of Vellum, an event photo product for guest uploads, host moderation, and original-quality galleries.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { existsSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
import { config } from "dotenv";
|
||||
import { drizzle } from "drizzle-orm/postgres-js";
|
||||
import postgres from "postgres";
|
||||
import * as appSchema from "./schema";
|
||||
import * as authSchema from "./auth-schema";
|
||||
|
||||
for (const path of [
|
||||
resolve(process.cwd(), ".env"),
|
||||
resolve(process.cwd(), "../../.env"),
|
||||
]) {
|
||||
if (existsSync(path)) {
|
||||
config({ path, override: false });
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const schema = { ...appSchema, ...authSchema };
|
||||
|
||||
type PostgresClient = ReturnType<typeof postgres>;
|
||||
type Database = ReturnType<typeof drizzle<typeof schema>>;
|
||||
export type { Database };
|
||||
const globalDatabase = globalThis as typeof globalThis & {
|
||||
albumPostgresClient?: PostgresClient;
|
||||
albumDatabase?: Database;
|
||||
};
|
||||
|
||||
let client = globalDatabase.albumPostgresClient;
|
||||
let database = globalDatabase.albumDatabase;
|
||||
|
||||
export function getDb() {
|
||||
if (database) return database;
|
||||
const url = process.env.DATABASE_URL;
|
||||
if (!url) {
|
||||
throw new Error("DATABASE_URL is required for database operations");
|
||||
}
|
||||
client = postgres(url, {
|
||||
max: process.env.NODE_ENV === "production" ? 10 : 3,
|
||||
idle_timeout: 20,
|
||||
});
|
||||
database = drizzle(client, { schema });
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
globalDatabase.albumPostgresClient = client;
|
||||
globalDatabase.albumDatabase = database;
|
||||
}
|
||||
return database;
|
||||
}
|
||||
|
||||
export async function closeDb() {
|
||||
await client?.end();
|
||||
client = undefined;
|
||||
database = undefined;
|
||||
globalDatabase.albumPostgresClient = undefined;
|
||||
globalDatabase.albumDatabase = undefined;
|
||||
}
|
||||
Reference in New Issue
Block a user