/** * Postgres caps backend: row locks serialize reservations per counter. * * A reservation runs in one READ COMMITTED transaction: it makes sure a row * exists per counter in `grantex_cap_counters`, locks those rows in a fixed * order (so concurrent reservations cannot deadlock), prunes and sums the * counter's reservations in `grantex_cap_reservations`, and inserts the new * reservation only if every limit holds. Time comes from the database * (`clock_timestamp()`) unless a clock is injected for tests. Same tables and * statements as the Python SDK's `PostgresCapsBackend`. * * Rows carry a SHA-256-derived hash of the tenant id, not the tenant id, in * `tenant_id`: isolation comes from that hash being part of every key and * query, and row-level security policies written against plain tenant ids do * not match these rows. * * Expired rows are deleted lazily when their counter is next reserved. Run * `prune()` periodically (for example hourly) to delete expired reservations, * per-case reservations older than `caseTtlSeconds` if one is set, and * counters with no reservations left. * * `pool` is a `pg` Pool, or anything with the same `connect()` / `query()` / * `release()` shape. Create the tables with `CAPS_SCHEMA_SQL` through your own * migration tool, or `ensureSchema()`. */ import { type CapsBackend, type ResolvedCapLimit } from './meter.js'; export declare const CAPS_SCHEMA_SQL = "\nCREATE TABLE IF NOT EXISTS grantex_cap_counters (\n tenant_id TEXT NOT NULL,\n counter_key TEXT NOT NULL,\n PRIMARY KEY (tenant_id, counter_key)\n);\n\nCREATE TABLE IF NOT EXISTS grantex_cap_reservations (\n tenant_id TEXT NOT NULL,\n counter_key TEXT NOT NULL,\n reservation_id TEXT NOT NULL,\n units BIGINT NOT NULL CHECK (units >= 0),\n reserved_at TIMESTAMPTZ NOT NULL,\n expires_at TIMESTAMPTZ,\n PRIMARY KEY (tenant_id, counter_key, reservation_id),\n FOREIGN KEY (tenant_id, counter_key)\n REFERENCES grantex_cap_counters (tenant_id, counter_key)\n);\n\nALTER TABLE grantex_cap_reservations ADD COLUMN IF NOT EXISTS expires_at TIMESTAMPTZ;\n\nCREATE INDEX IF NOT EXISTS idx_grantex_cap_reservations_time\n ON grantex_cap_reservations (tenant_id, counter_key, reserved_at);\n\nCREATE INDEX IF NOT EXISTS idx_grantex_cap_reservations_expiry\n ON grantex_cap_reservations (expires_at)\n WHERE expires_at IS NOT NULL;\n"; export interface PgClientLike { query(text: string, values?: readonly unknown[]): Promise<{ rows: Array>; rowCount?: number | null; }>; /** `pg` destroys the connection instead of reusing it when given an error. */ release(err?: Error | boolean): void; } export interface PgPoolLike { connect(): Promise; } export declare class PostgresCapsBackend implements CapsBackend { #private; /** * `caseTtlSeconds` optionally lets `prune()` delete per-case reservations * that old; by default they are kept, because deleting them would reset the * per-case cap. */ constructor(pool: PgPoolLike, options?: { caseTtlSeconds?: number; }); ensureSchema(): Promise; reserve(tenantId: string, reservationId: string, limits: readonly ResolvedCapLimit[], nowMs: number | undefined): Promise; refund(tenantId: string, reservationId: string, limits: readonly ResolvedCapLimit[]): Promise; usage(tenantId: string, limit: ResolvedCapLimit, nowMs: number | undefined): Promise; /** * Delete expired reservations and counters left empty; returns the numbers * deleted. Counters being reserved are locked and skipped, and a counter that * still has reservations cannot be deleted (the foreign key refuses it, * failing this run rather than losing a reservation; run it again). */ prune(nowMs?: number): Promise<{ reservations: number; counters: number; }>; } //# sourceMappingURL=postgres.d.ts.map