Files
0pt/lib/db.ts
T
adminandClaude Opus 4.8 0b09ee5795 feat(api): order persistence (Postgres or in-memory fallback) + orders API
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>
2026-06-24 18:11:34 +02:00

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;
}