Database Schema Designer
Tier: POWERFUL
Category: Engineering
Domain: Data Architecture / Backend
Overview
Design relational database schemas from requirements and generate migrations, TypeScript/Python types, seed data, RLS policies, and indexes. Handles multi-tenancy, soft deletes, audit trails, versioning, and polymorphic associations.
Core Capabilities
- Schema design — normalize requirements into tables, relationships, constraints
- Migration generation — Drizzle, Prisma, TypeORM, Alembic
- Type generation — TypeScript interfaces, Python dataclasses/Pydantic models
- RLS policies — Row-Level Security for multi-tenant apps
- Index strategy — composite indexes, partial indexes, covering indexes
- Seed data — realistic test data generation
- ERD generation — Mermaid diagram from schema
When to Use
- Designing a new feature that needs database tables
- Reviewing a schema for performance or normalization issues
- Adding multi-tenancy to an existing schema
- Generating TypeScript types from a Prisma schema
- Planning a schema migration for a breaking change
Schema Design Process
Step 1: Requirements → Entities
Given requirements:
"Users can create projects. Each project has tasks. Tasks can have labels. Tasks can be assigned to users. We need a full audit trail."
Extract entities:
Step 2: Identify Relationships
Step 3: Add Cross-cutting Concerns
- Multi-tenancy: add
organization_idto all tenant-scoped tables - Soft deletes: add
deleted_at TIMESTAMPTZinstead of hard deletes - Audit trail: add
created_by,updated_by,created_at,updated_at - Versioning: add
version INTEGERfor optimistic locking
Full Schema Example (Task Management SaaS)
→ See references/full-schema-examples.md for details
Row-Level Security (RLS) Policies
Seed Data Generation
ERD Generation (Mermaid)
Generate from Prisma:
Common Pitfalls
- Soft delete without index —
WHERE deleted_at IS NULLwithout index = full scan - Missing composite indexes —
WHERE org_id = ? AND status = ?needs a composite index - Mutable surrogate keys — never use email or slug as PK; use UUID/CUID
- Non-nullable without default — adding a NOT NULL column to existing table requires default or migration plan
- No optimistic locking — concurrent updates overwrite each other; add
versioncolumn - RLS not tested — always test RLS with a non-superuser role
Best Practices
- Timestamps everywhere —
created_at,updated_aton every table - Soft deletes for auditable data —
deleted_atinstead of DELETE - Audit log for compliance — log before/after JSON for regulated domains
- UUIDs or CUIDs as PKs — avoid sequential integer leakage
- Index foreign keys — every FK column should have an index
- Partial indexes — use
WHERE deleted_at IS NULLfor active-only queries - RLS over application-level filtering — database enforces tenancy, not just app code


