ecto-essentials
Installation
SKILL.md
Ecto Essentials
Follow the repository's Ecto and database conventions. Keep business rules in the context or pure domain modules; keep Repo calls at the persistence edge.
Schemas and changesets
- Model persisted data in schemas and validate external input with changesets.
- Use
cast/3with an explicit field allowlist for external maps. Usechange/2for trusted internal updates. - Attach database constraints to changesets with
unique_constraint/3,foreign_key_constraint/3, or the matching constraint helper so expected database errors return changeset errors. - Keep association ownership and delete behavior explicit. Use
cast_assoc/3only when nested input is part of the public operation.
Queries and associations
- Compose
Ecto.Queryfragments with named bindings and parameterized values. Never interpolate user input into SQL. - Keep reusable filters and ordering in query functions; keep policy and orchestration in the context.
- Preload what the caller needs. For lists, inspect query counts and prefer joins or batched preloads over per-row
Repo.get/2calls. - Use
Repo.one/1,Repo.all/1, and bang variants according to the caller's expected absence and error contract.