Database Handbook
Supabase schema, migrations, and data types in KAP
1 Overview
KAP uses Supabase (managed Postgres, Auth, Storage, Realtime) as the primary data store for the web app, agent tooling, and reporting. Application code talks to Postgres through PostgREST (@supabase/supabase-js on the server and client) under Row Level Security (RLS).
This handbook summarizes how the database fits the product, where migrations live, and how TypeScript types stay aligned with the live schema.
1.1 Product data types (conceptual)
Day-to-day features are organized around three user-visible data types; table names in Postgres may differ until a future rename.
| Concept | Primary table / API today |
|---|---|
| Datasets | datasets (campaigns are dataset rows under an organization) |
| Notes | location_notes (REST and routes under /api/.../notes) |
| Reports | pdf_reports (PDF URLs plus KRML fields: report_id, rendered, metadata, optional template_name) |
Work-order-style workflows use notes with structured metadata (for example kind: "work_order") rather than a separate work_orders table unless reporting needs justify one later.
1.2 Schema changes (migrations)
- Location:
supabase/migrations/at the repository root. - Apply: Use your team’s standard path (Supabase CLI
db push, Dashboard SQL, or CI) sosupabase_migrations.schema_migrationsstays in sync with the repo. - Idempotency: Prefer
IF NOT EXISTS/DROP IF EXISTSpatterns where replays or branches are common.
New tables and columns should follow existing RLS patterns: gate access through organization membership (organization_members + datasets.organization_id) unless the row is intentionally public.
1.3 TypeScript types
- Generated file:
web/lib/database.generated.ts(and companions if your project splits types). - Refresh: From
web/, runnpm run db:typesafter migrations are applied (requires Supabase CLI and project linkage as documented in the web package).
Do not hand-edit generated tables for long; regenerate after DDL changes so inserts and selects stay type-safe.
1.4 pdf_reports column semantics (KRML)
Persisted reports and templates live in pdf_reports (legacy hermes_* tables were removed in favor of this single store):
- Report rows:
report_id,name(title),rendered(Markdown),status,facility,metadata(typically includeskind: "krml_report"). - Layout templates: rows with
template_nameset andmetadata.kind: "krml_layout_template"(section_order,section_config).
Hermes registers LLM-callable tools named krml_* (krml_save_report, …) implemented in ai/src/kavai/systems/hermes/tools/krml_renderer.py and skills/krml_composition.py. Web or other services should use the same columns and metadata conventions when writing report rows so all consumers stay interoperable.
1.6 Quick reference (common tables)
The live project may include additional tables (Auth, Storage, Realtime, extensions). Application tables frequently touched by KAP include:
organizations,organization_members,profilesdatasets,images,data_groupslocation_notes,annotationspdf_reports,chat_sessions,messagesgas_readings,equipment,investigations(and related domain tables)
Use Supabase Studio or information_schema when you need an authoritative column list for your environment.