TorbatYar/docs/ai-framework/repository-template.md
Mortezakoohjani 5c6a2e78cf feat(platform): seed service registry, deploy all modules, and fix homepage catalog.
Register all active platform services and base features in core DB so admin can manage them, add delivery/hospitality/sports-center backend phases, update apps catalog and production deploy tooling.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-27 12:39:51 +03:30

65 lines
2.2 KiB
Markdown

# Repository Template
Standards for the persistence layer.
## Responsibility
Repositories:
- Load/save aggregates and rows
- Apply query filters (including **mandatory** `tenant_id` for tenant-owned entities)
- Encapsulate SQLAlchemy queries
Repositories must **not**:
- Enforce entitlement or workflow decisions
- Call external HTTP APIs
- Publish events (services own outbox writes)
- Map HTTP status codes
- Contain business branching beyond trivial persistence concerns
## Conventions
| Topic | Rule |
| --- | --- |
| Location | `app/repositories/` |
| Naming | `{Aggregate}Repository` or grouped foundation repos matching service pattern |
| Constructor | Accept DB session (or unit-of-work) explicitly |
| Tenant | Methods that touch tenant data accept/filter `tenant_id` |
| Soft delete | Default reads exclude soft-deleted unless `include_deleted=True` |
| Return type | Models / rows; services map to DTOs |
| Transactions | Participate in caller-managed session/transaction; do not silently commit policy that breaks outbox atomicity |
## Required Patterns
```text
get_by_id(tenant_id, id) -> Model | None
list(tenant_id, *, filters, sort, search, pagination) -> Sequence[Model] + total/meta
add(entity) / save(entity)
soft_delete(tenant_id, id) # when soft-delete policy applies
# optional: update with version check for optimistic locking
```
Follow existing service repository base classes when present (`repositories/base.py`).
Compose reusable predicates via specifications when the service uses `app/specifications/`.
## Tenant Isolation
1. Every query for tenant-owned tables includes `tenant_id`.
2. Updates/deletes must be tenant-scoped (no id-only mutation).
3. Repository tests must include cross-tenant denial cases.
## Testing
See [testing-template.md](testing-template.md) — Repository section.
## Related Documents
- [Service Architecture](../architecture/service-architecture.md)
- [Entity Template](entity-template.md)
- [Service Layer Template](service-layer-template.md)
- [Mandatory Phase Artifacts](mandatory-phase-artifacts.md)
- [Coding Standards](../development/coding-standards.md)
- [Project Principles](../development/project-principles.md)