lib/db (env-guarded postgres-js), lib/orders-store (createOrder/getOrder/listOrders/setStatus, DB-or-memory), /api/orders (POST create+estimate, GET list), /api/orders/[id] (GET, PATCH status with escrow-safe state machine). Activates fully on DATABASE_URL/POSTGRES_URL. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
24 lines
730 B
TypeScript
24 lines
730 B
TypeScript
import postgres from "postgres";
|
|
|
|
// Lazy, env-guarded Postgres client. Works with Vercel Postgres (POSTGRES_URL)
|
|
// or any DATABASE_URL. Returns null when no DB is configured so the app still
|
|
// builds/runs (with in-memory fallback) before storage is provisioned.
|
|
let _sql: ReturnType<typeof postgres> | null = null;
|
|
|
|
export function dbUrl(): string | undefined {
|
|
return process.env.DATABASE_URL || process.env.POSTGRES_URL || process.env.POSTGRES_PRISMA_URL;
|
|
}
|
|
|
|
export function hasDb(): boolean {
|
|
return Boolean(dbUrl());
|
|
}
|
|
|
|
export function getSql() {
|
|
const url = dbUrl();
|
|
if (!url) return null;
|
|
if (!_sql) {
|
|
_sql = postgres(url, { prepare: false, idle_timeout: 20, max: 5, ssl: "require" });
|
|
}
|
|
return _sql;
|
|
}
|