Include Loyalty/Communication/Sports Center backends and registry updates alongside production nginx and compose wiring. Co-authored-by: Cursor <cursoragent@cursor.com>
3.4 KiB
3.4 KiB
Entity Template
Standards for domain entities / ORM models in every service.
Entities
| Rule | Detail |
|---|---|
| Naming | PascalCase class; snake_case table |
| Primary key | UUID (preferred) or documented exception |
| Tenant | Business entities include non-null tenant_id (ADR-003) |
| Timestamps | created_at / updated_at (timezone-aware) |
| Identity | No business meaning in surrogate keys |
| Mapping | SQLAlchemy models in app/models/; no business workflows in model methods beyond trivial helpers |
Relationships
- Relationships only within the same database.
- No cross-service foreign keys.
- Cross-service association: store external ref IDs (
*_ref, UUID strings) without FK. - Prefer explicit relationship directions; avoid implicit cascade deletes across aggregates unless documented.
- Owning aggregate controls lifecycle of child rows.
Indexes
- Index
tenant_id(alone or composite) for tenant-scoped lists. - Composite indexes for common filters:
(tenant_id, status),(tenant_id, created_at), unique business numbers per tenant. - Unique constraints that are tenant-scoped must include
tenant_id. - Document unusual indexes in the phase/migration notes.
Constraints
- DB constraints enforce integrity the domain relies on (unique, check, non-null).
- Do not rely only on application validation for critical invariants.
- Monetary / quantity fields use precise types (Numeric) where applicable.
- Enums: DB + Python enums kept in sync; prefer string enums for readability.
Soft Delete
| Rule | Detail |
|---|---|
| Default for master/business records | Prefer deleted_at / is_deleted pattern used by sibling modules |
| Queries | Default repositories exclude soft-deleted rows |
| Uniqueness | Unique constraints must consider soft-delete strategy (partial unique indexes when required) |
| Hard delete | Only when phase explicitly allows (e.g. ephemeral logs with retention policy) |
Audit
- Every business action must be auditable (project-principles.md).
- Prefer
created_by/updated_by(actor ids) on mutable entities when the service pattern uses them. - Sensitive domains keep append-only audit tables (CRM/Loyalty/Accounting patterns).
- Audit rows are tenant-scoped and immutable after insert.
Validation
- Structural validation: Pydantic schemas at API boundary.
- Domain validation: service-layer validators (
app/validators/). - DB constraints: last line of defense.
- Never trust client-provided
tenant_idover resolved tenant context. - Normalize phones/codes via shared helpers where applicable.
Entity Checklist (per new table)
- Table name + model documented in phase
tenant_id(if business data)- Indexes / uniques defined
- Soft delete policy stated
- Audit fields or audit table linkage
- Migration included
- Schema reference updated when public/shared docs require it (database-schema.md)