TorbatYar/docs/ai-framework/repository-template.md
Mortezakoohjani e41ecfad4c Sync platform docs, infra, and module services with Accounting integration.
Include Loyalty/Communication/Sports Center backends and registry updates alongside production nginx and compose wiring.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-25 22:35:23 +03:30

1.9 KiB

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

get_by_id(tenant_id, id) -> Model | None
list(tenant_id, *, filters, pagination) -> Sequence[Model]
add(entity) / save(entity)
# optional: soft_delete(tenant_id, id)

Follow existing service repository base classes when present (repositories/base.py).

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 — Repository section.