Goal: end-to-end type safety from schema to query result.
Use for:
- modeling tables, relations, and constraints in TypeScript
- generating and applying migrations safely
- writing queries with inferred result types
Workflow:
- Define tables and columns in the schema with explicit types.
- Declare relations for type-safe joins and nested reads.
- Generate migrations from the schema; review the SQL.
- Apply migrations through the migrator, not ad hoc edits.
- Query with the typed builder; let result types infer.
- Derive insert/select types from the schema for app code.
Patterns:
- single source of truth: the schema drives types and migrations
- relations for typed joins instead of manual mapping
- prepared statements for hot queries
- transactions for multi-step writes
Rules:
- never hand-edit applied migrations; create a new one
- review generated SQL before running it
- derive types from the schema; do not duplicate them
- scope transactions tightly around the related writes