Skip to main content

Schema conventions

Shared patterns: IDs, soft delete, profile scope, money, dates, and dual users.
3 min read

Reading this once prevents misreading 80% of the tables.

Dual identity: two “users”#

TableRole
"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:

ColumnMeaning
deleted_atSoft delete (NULL = active)
archived_at / is_archivedHide 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_atProfiles only (purge)

Unique indexes are usually partial:

Timestamps#

ColumnTypeDefault
created_atTIMESTAMPTZNOW()
updated_atTIMESTAMPTZNOW()

Primary keys#

  • Product: UUID DEFAULT gen_random_uuid()
  • Better Auth / billing webhooks: often TEXT natural keys

JSONB#

Used for settings, preferences, event payloads, etc.

ExampleTypical contents
profiles.settingsenabled sections, flags
user_preferences.appearance{ "theme": "system" }
activities.recurrence_weekdaysarray of days (habits)
project_events.payloadfeed event

Money#

ConventionDetail
UnitCents (BIGINT)
Names*_cents, opening_balance_cents, amount_cents
Interestsometimes basis points (annual_interest_rate_bps)

Never DOUBLE for ledger money.

Business dates vs instants#

TypeFormatExample column
InstantTIMESTAMPTZoccurred_at, created_at
Local dayTEXT YYYY-MM-DDdue_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#

WhereFieldUpdated by
profilesmember_count, project_count, seat_countapp / billing
profile_projectstask_counttriggers on tasks
profile_tagslink_countapp

Several tables link “any entity” with:

Examples: profile_tag_links, profile_project_links, goal_links.

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 into tasks.search_blob on save)
  • notes.search_blob
  • finance_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_URL uses PgBouncer (port 6432); MIGRATE_DATABASE_URL uses 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 PingContext with 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#

LayerStyle
Product tablessnake_case plural or domain-prefixed (finance_, health_)
Better AuthcamelCase in quotes ("userId")
FKs{singular_table}_id

Checklist when adding a table#

  • profile_id if 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