CRUD Template
Summary
Every resource follows the same four-file slice. Repetition is intentional: it makes the codebase predictable and generatable.
The slice
models.py
A framework-free Pydantic model describing the business shape.
ports/db.py
The persistence model extends the business model and adds database concerns. The repository factory selects the right repository flavour.
ports/service.py
The service is built through the DI library, injecting the repository. The
generic CRUD base provides create/read/update/patch/delete/search/....
Naming conventions
| Concept | Convention |
|---|---|
| collection | plural snake case (items) |
| document | <Resource>Document |
| repository factory | create_<resource>_repo |
| service | <Resource>Service |
| service factory | get_<resource>_service |
| resource key | <resource>_svc |
| operationId | list_items, create_item, ... |
Optional files
converters.py— pure functions mapping between domain types.ports/enrichment.py— adapts a pipeline into an async callable the service can invoke (used when a resource is populated by a pipeline).