About ecto-essentials
ecto-essentials is a skill for applying repository conventions to Ecto schemas, queries, and database writes. It guides developers to keep business rules in contexts or domain modules and database access at the persistence edge.
Key features
- 01Validate external input with changesets
- 02Use parameterized, composable queries
- 03Guide association preloading
- 04Choose transaction patterns for writes
- 05Test persistence changes
Use cases
- 01Adding or updating Ecto schemas
- 02Building safe database queries
- 03Implementing multi-step database writes
- 04Changing persistence behavior and its tests
Install
Copy the command and run it in a terminal.
npx skills add https:/ / github.com/ igmarin/ elixir-phoenix-skills --skill ecto-essentialsAfter installing, type/ecto-essentialsin Claude Code to confirm it loaded.
Bundled filesassets/changeset_snippets.exassets/migration_checklist.md
Details
From the project READMEEcto 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.
Writes and transactions
- Return
{:ok, value}/{:error, changeset_or_reason}from expected failures. - Use
Ecto.Multiwhen a transaction has multiple named steps or its failure step matters to the caller. Use a normal transaction for a small atomic block. - Do not put network calls or other irreversible side effects inside a database transaction.
- For upserts, state the conflict target and verify the database constraint matches it.
Verification
Add or update tests for the changed persistence behavior. Run the focused test, then the project checks that cover Ecto and migrations. Use ecto-changeset-patterns, ecto-nested-associations, or ecto-migration only when that narrower behavior is central.