# {Feature Name} Data Models **Spec:** {NNN}-{feature_slug} **Database:** {PostgreSQL/MySQL/etc.} --- ## Entity Relationship Diagram ``` ┌─────────────────┐ ┌─────────────────┐ │ {Entity1} │ │ {Entity2} │ ├─────────────────┤ ├─────────────────┤ │ id (PK) │──────<│ id (PK) │ │ field1 │ │ entity1_id (FK) │ │ field2 │ │ field1 │ │ created_at │ │ created_at │ └─────────────────┘ └─────────────────┘ ``` --- ## Enums | Enum Name | Database Type | Values | Description | |-----------|---------------|--------|-------------| | `{enum_name}` | {VARCHAR/ENUM} | `VAL_A`, `VAL_B`, `VAL_C` | {What this enum represents} | **Note:** {Strategy for adding new enum values: migration required / append-only / etc.} --- ## Entities ### {EntityName} **Table:** `{table_name}` **Description:** {What this entity represents} | Column | Type | Nullable | Default | Description | |--------|------|----------|---------|-------------| | `id` | UUID | No | gen_random_uuid() | Primary key | | `{column}` | {type} | Yes/No | {default} | {Description} | | `created_at` | TIMESTAMP | No | NOW() | Creation timestamp | | `updated_at` | TIMESTAMP | No | NOW() | Last update timestamp | **Indexes:** - `idx_{table}_{column}` on `{column}` - {Purpose} **Constraints:** - `{constraint_name}`: {Description} **Relationships:** - Has many `{OtherEntity}` via `{foreign_key}` - Belongs to `{OtherEntity}` via `{foreign_key}` --- ## Migration Strategy **Migration Tool:** {Flyway / Liquibase / Alembic / Prisma / etc.} **Naming Convention:** `V{NNN}__{description}.sql` | Order | Migration File | Description | Reversible | |-------|----------------|-------------|------------| | 1 | `V001__{name}.sql` | {What this migration does} | Yes/No | --- ## Soft Delete Strategy {Include if any entities use soft delete, otherwise remove this section} | Entity | Strategy | Delete Column | Cascade Behavior | |--------|----------|---------------|------------------| | `{entity}` | soft_delete | `deleted_at TIMESTAMP` | {child records also soft-deleted / orphaned / etc.} | **Query Pattern:** All queries must include `WHERE deleted_at IS NULL` unless explicitly querying deleted records. --- ## Audit Fields {Standard audit columns used across entities} | Column | Type | Purpose | Auto-populated | |--------|------|---------|----------------| | `created_at` | TIMESTAMP | Record creation time | Yes (DEFAULT NOW()) | | `updated_at` | TIMESTAMP | Last modification | Yes (trigger/app) | | `created_by` | UUID/VARCHAR | User who created | App-level | | `updated_by` | UUID/VARCHAR | User who last modified | App-level | --- ## Queries ### Common Query Patterns **Get {entity} by ID:** ```sql SELECT * FROM {table} WHERE id = :id; ``` **List with pagination:** ```sql SELECT * FROM {table} ORDER BY created_at DESC LIMIT :limit OFFSET :offset; ``` --- ## Seed Data **Development seed script:** `{path/to/seed.sql}` or `{path/to/seed.ts}` **Seed data includes:** - {Entity 1}: {N} sample records - {Entity 2}: {N} sample records - {Reference data}: {description} **Run seed:** ```bash {command to run seed, e.g., "npm run db:seed" or "psql -f seed.sql"} ```