Skip to content

Database

71 tables defined in ordered SQL migrations under packages/database/migrations/. PostgreSQL with PostGIS for geographic data; @repo/database is a Kysely client (on the pg driver) with generated types — there is no ORM.

GroupTables
Users & AuthUser, Session, Account, Verification, UserAddress
Agents & TeamsAgentProfile, AgentServiceArea, Team, TeamMember, TeamInvitation
PropertiesProperty, PropertyFeature, PropertyImage, PropertyVideo, FloorPlan, PropertyUnit, UnitInventory, PropertyAgent, PropertyList, PropertyListItem
ListingsListingVerification, Disclosure, RentalTerms
Leads & CommsInquiry, Tour, Note, Task, Notification
TransactionsOffer, Transaction, Commission, Document, Payment
EngagementFavorite, SavedSearch, PropertyView, Review
Market DataComparable, PriceHistory, MarketReport
ConfigRedisConfig

account_deletion_requests is one open row per user (partial unique index on userId where canceledAt and purgedAt are null). Its snapshot jsonb holds the restore data plus purge.externalsDone once the worker finishes external cleanup. users.deletedAt means hidden; users.purgedAt means the PII is gone. failed_account_purges is the worker DLQ. users is never hard-deleted — about fifteen foreign keys are ON DELETE RESTRICT — so a purge anonymizes the row in place. See packages/api/src/services/account-deletion.FEATURE.md.

Read packages/database/src/enums.ts (const-object values) and packages/database/src/generated/db.ts (column types) before using any enum value — guessing is the most common bug class in this repo.

EnumValues
UserRoleINDIVIDUAL, AGENCY_ADMIN, ADMIN
PropertyTypeHOUSE, APARTMENT, SUITE_ROOM, LAND
PropertyStatusDRAFT, ACTIVE, PAUSED, SOLD, RENTED, ARCHIVED
ListingTypeSALE, RENT
TeamTypeAGENCY, GROUP, CONSTRUCTION_COMPANY
TeamRoleLEAD, ADMIN, MEMBER
Tour.statusREQUESTED (not PENDING), CONFIRMED, RESCHEDULED, COMPLETED, CANCELLED, DENIED
InquiryStatusNEW, CONTACTED, IN_PROGRESS, CLOSED, CONVERTED

The Tour.status = REQUESTED vs PENDING mismatch has caused multiple bugs — call it out explicitly in PR review.

erDiagram
    User ||--o| AgentProfile : "optional"
    AgentProfile ||--o{ TeamMember : "joins"
    TeamMember }o--|| Team : "belongs to"
    User ||--o{ Property : "owns"
    Property ||--o{ PropertyAgent : "assigned to"
    AgentProfile ||--o{ PropertyAgent : "manages"
    Property ||--o{ PropertyImage : "has"
    Property ||--o{ PropertyUnit : "has units"
    PropertyUnit ||--o{ UnitInventory : "has inventory"
    Property ||--o{ Inquiry : "receives"
    Property ||--o{ RentalTerms : "has terms"
    User ||--o{ Favorite : "saves"
    User ||--o{ SavedSearch : "creates"
    Property ||--o{ Transaction : "results in"
    PropertyList ||--o{ PropertyListItem : "contains"
    AgentProfile ||--o{ PropertyList : "owns"
TypeRequired fieldsOptional fields
HOUSEbedrooms, bathrooms, constructedArea, pricelotSize, yearBuilt, parkingSpaces
APARTMENTbedrooms, bathrooms, constructedArea, priceyearBuilt, parkingSpaces
SUITE_ROOMbedrooms (≥1), bathrooms, constructedArea, priceyearBuilt, parkingSpaces
LANDlotSize, priceyearBuilt, parkingSpaces

Validation rules live in @repo/validation (Zod), shared by web form actions and tRPC inputs.

DRAFT → ACTIVE → PAUSED → ACTIVE (resume)
→ SOLD / RENTED (terminal)
DRAFT → ARCHIVED (soft delete)

The price_history trigger fires only when the property is in ACTIVE status — INITIAL on publish/reactivate, PRICE_REDUCTION / PRICE_INCREASE on edits. Price-drop badges on property cards are computed from this table during worker re-indexing (30-day window).

There are no Postgres row-level security policies in the migration chain (no CREATE POLICY anywhere under packages/database/migrations/), and every host connects through one pg.Pool role. Isolation is enforced in the application layer: SvelteKit locals.user gates on load / form actions / +server.ts, and protectedProcedure / agentProcedure plus per-service ownership checks (resolveProperty, team role checks) in the tRPC layer. The mechanics are documented once in packages/auth/FEATURE.md § Authorization; the decision and its consequences are ADR-0026. See also Conventions → Monorepo.

Most tables have IDs defaulted to generate_*_id(). The generator functions are created by the 0001_extensions_and_id_functions.ts migration and must exist before the 0002_baseline_schema.ts table-create migration runs, which is why the migration chain is strictly ordered (0001 functions → 0002 schema → 0003 triggers) and the db:reset script replays it instead of dropping and recreating in one atomic step. See Conventions → Monorepo.

Terminal window
# Postgres only: refuses under NODE_ENV=production or for a non-localhost DATABASE_URL
# (add --allow-remote-db for the dev DB), prints the target host, drops + recreates the
# schema (PostGIS comes back via 0001), replays the chain to latest
# (0001 functions → 0002 schema → 0003 triggers → incrementals). No verification step.
(cd packages/database && doppler run -- bun run db:reset)
# Whole environment (runs db:reset as its Postgres step; dry run until --execute):
doppler run -- bun run dev:refresh

packages/database/scripts/db-reset.ts is the source of truth for the Postgres step. On its own it leaves Valkey (psearch:*, cache, the BullMQ queues on db1), the Meilisearch agents index, S3 uploads and the Stripe / FacturAPI / Knock / GetStream objects keyed to the dropped rows in place — dev:refresh is what resets those; see Operations → Scripts. After either, restart api, web and worker: the recreated enum types have new OIDs and a running pg pool keeps reading users.roles as a raw literal until it reconnects.