Files
lunch/lib/db.ts
T
Kai BöhmandClaude Opus 5.5 fa83e560b2
ci / olares chart · sanity (pull_request) Successful in 5s
ci / app · types + tests + build (pull_request) Successful in 3m54s
release / which version (pull_request) Successful in 1s
release / olares chart · sanity (pull_request) Successful in 3s
release / app (pull_request) Successful in 1m25s
release / tag and release (pull_request) Skipped
Scaffold the app, chart, CI and release (26.11.1)
Next.js 15 with next-intl (de, en, fr, es; locale per person, no URL
prefix), Tailwind v4 tokens for the warm palette with dark mode, a
shadcn/ui button, Serwist service worker and PWA manifest with icons.
One Postgres pool with withHousehold() setting app.household_id per
transaction; the first migration creates households with FORCE ROW
LEVEL SECURITY, and a two-household test checks the isolation as the
table owner. scripts/migrate.mjs applies db/migrations once each with a
checksum and retries until the database is reachable.

Olares chart with one deployment (init: chown, migrate from the same
image), ClusterIP service, entrance internal until the own sign-in
exists, image path fixed in the template and tag from appVersion.
scripts/check-chart.sh carries the rules from Rocket; CI runs it, type
check, tests against Postgres and the build; release builds
lunch-app, tags and attaches the chart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HUc2nv7rJVU2gp3Bgtha3q
2026-11-06 19:08:21 +00:00

56 lines
1.9 KiB
TypeScript

import "server-only";
import { Pool, type PoolClient } from "pg";
// Zugang zur Postgres von Olares. Die Werte injiziert das Chart aus
// .Values.postgres.* (CLAUDE.md, Olares-Regel 5) — nie hartkodieren.
// Lokal reicht DATABASE_URL.
function createPool(): Pool {
if (process.env.DATABASE_URL) return new Pool({ connectionString: process.env.DATABASE_URL, max: 10 });
return new Pool({
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT ?? 5432),
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
max: 10,
});
}
const globalForPool = globalThis as unknown as { lunchPool?: Pool };
export const pool: Pool = globalForPool.lunchPool ?? createPool();
if (process.env.NODE_ENV !== "production") globalForPool.lunchPool = pool;
/**
* Führt `fn` in einer Transaktion im Namen eines Haushalts aus.
*
* Jede Fachtabelle hat RLS mit FORCE und die Policy „Mitglied des Haushalts“
* über `app.household_id`. Ohne diesen Aufruf sieht eine Abfrage nichts —
* die App verbindet als Tabelleneigentümerin, FORCE ist die einzige Wand.
* `set_config(…, true)` gilt nur bis zum Ende der Transaktion; ein Client,
* der in den Pool zurückgeht, trägt keinen Haushalt weiter.
*/
export async function withHousehold<T>(householdId: string, fn: (client: PoolClient) => Promise<T>): Promise<T> {
const client = await pool.connect();
try {
await client.query("begin");
await client.query("select set_config('app.household_id', $1, true)", [householdId]);
const result = await fn(client);
await client.query("commit");
return result;
} catch (error) {
await client.query("rollback").catch(() => undefined);
throw error;
} finally {
client.release();
}
}
export async function databaseReachable(): Promise<boolean> {
try {
await pool.query("select 1");
return true;
} catch {
return false;
}
}