Reading this once prevents misreading 80% of the tables.
Dual identity: two “users”#
| Table | Role |
|---|---|
"user" (Better Auth) | Login, session, OAuth. PK TEXT. camelCase columns. |
users (platform) | Product identity. PK UUID. auth_user_id → "user".id. |
Never use the Better Auth id as created_by on domain tables: use users.id.
Profile scope#
Almost every product entity has:
Domain queries always filter by profile_id (and membership in the API).
Soft delete#
Usual pattern:
| Column | Meaning |
|---|---|
deleted_at | Soft delete (NULL = active) |
archived_at / is_archived | Hide from active lists by default; the resource remains countable and visible in contextual views (project detail, tags, reports, search, metrics). This is not a soft-delete. |
permanently_deleted_at | Profiles only (purge) |
Unique indexes are usually partial:
Timestamps#
| Column | Type | Default |
|---|---|---|
created_at | TIMESTAMPTZ | NOW() |
updated_at | TIMESTAMPTZ | NOW() |
Primary keys#
- Product:
UUID DEFAULT gen_random_uuid() - Better Auth / billing webhooks: often TEXT natural keys
JSONB#
Used for settings, preferences, event payloads, etc.
| Example | Typical contents |
|---|---|
profiles.settings | enabled sections, flags |
user_preferences.appearance | { "theme": "system" } |
activities.recurrence_weekdays | array of days (habits) |
project_events.payload | feed event |
Money#
| Convention | Detail |
|---|---|
| Unit | Cents (BIGINT) |
| Names | *_cents, opening_balance_cents, amount_cents |
| Interest | sometimes basis points (annual_interest_rate_bps) |
Never DOUBLE for ledger money.
Business dates vs instants#
| Type | Format | Example column |
|---|---|---|
| Instant | TIMESTAMPTZ | occurred_at, created_at |
| Local day | TEXT YYYY-MM-DD | due_date_key, date_key, day_key, week_start_date_key |
The *_key columns avoid timezone ambiguity in daily UI (health, nutrition, tasks due).
Denormalized counters#
| Where | Field | Updated by |
|---|---|---|
profiles | member_count, project_count, seat_count | app / billing |
profile_projects | task_count | triggers on tasks |
profile_tags | link_count | app |
Polymorphic entity links#
Several tables link “any entity” with:
Examples: profile_tag_links, profile_project_links, goal_links.
Full-text search#
GIN indexes on to_tsvector('simple', …) on:
tasks.search_blob— denormalized: document name + body from the editor (000022)task_documents.search_text— body only (folded intotasks.search_blobon save)notes.search_blobfinance_transactions.search_blob- (+ trgm on names:
tasks.name,profile_tags.normalized_name)
Go helpers: internal/shared/fts (MatchSQL, RankSQL, TsQuery, JoinBlob). tsquery config: simple. Match with to_tsquery and tokens in prefix (token:* via TsQuery) for typeahead; ranking with ts_rank on search endpoints (tasks search, finance search/knowledge, notes list with q).
PostgreSQL runtime#
DATABASE_URLuses PgBouncer (port 6432);MIGRATE_DATABASE_URLuses a direct connection (port 5432).- The GORM runtime uses the pgx simple protocol and does not cache prepared statements. Session-bound prepared statements are not safe with the pooler and have caused collisions between connections.
- GORM does not ping implicitly: bootstrap runs a single
PingContextwith a contextualized error before serving traffic. - Each instance limits the pool to 10 open connections and 2 idle by default, avoiding unnecessary connection multiplication when Vercel scales horizontally.
Naming#
| Layer | Style |
|---|---|
| Product tables | snake_case plural or domain-prefixed (finance_, health_) |
| Better Auth | camelCase in quotes ("userId") |
| FKs | {singular_table}_id |
Checklist when adding a table#
-
profile_idif it is product data -
created_at/updated_at - Soft delete if the product has a “trash”
- Indexes for listings by profile
- Partial uniques with
WHERE deleted_at IS NULL - Up and down migration
- Update this docs tab in the same PR