Contexto
Zero-boilerplate CRUD functions for your Ecto contexts.
Tired of writing the same list/0, get/1, create/1 boilerplate in every Ecto context? Contexto generates it for you.
Installation
def deps do
[
{:contexto, "~> 0.1.0"}
]
endUsage
defmodule MyApp.Users do
use Contexto, repo: MyApp.Repo, schema: MyApp.Users.User
end
That's it. You now have, e.g., MyApp.Users.list/0, MyApp.Users.get/1, etc. ready to use. Full list of generated functions:
| Function | Description |
|---|---|
list/0 | Returns all records |
get/1 |
Finds by primary key, returns nil if not found |
get!/1 | Finds by primary key, raises if not found |
get_by/1 |
Finds by keyword clauses, returns nil if not found |
create/1 |
Inserts a new record via changeset/2 |
update/2 |
Updates a record via changeset/2 |
update!/2 |
Updates a record via changeset/2, raises on failure |
delete/1 | Deletes a record |
Overriding
All functions are overridable. Add custom logic per-context without losing the defaults:
defmodule MyApp.Users do
use Contexto, repo: MyApp.Repo, schema: MyApp.Users.User
# override list to always sort
def list do
User |> order_by(:inserted_at) |> Repo.all()
end
endAssumptions
-
Your schema module has a
changeset/2function.
License
MIT