EctoCrux (EctoCrux v1.2.17)
Crud concern to use in helper's schema implementation with common Repo methods.
You can use crux methods instead of ones generated by mix phx.gen.schema.
Installation
add to deps
def deps do
[
{:ecto_crux, "~> 1.2.15"}
]
endconfigure (in config/config.exs)
config :ecto_crux, repo: MyApp.Repoavailable parameters are:
:repo- specify repo to use to handle this queryable module:page_size[optional] - default page size to use when using pagination ifpage_sizeis not specified:order_by[optional] - default order by expression, will be used infind_byandall:select[optional] - default select expression:read_only[optional] - exclude all write functions:except[optional] - list of methods to exclude
tl;dr; example
defmodule MyApp.Schema.Baguette do
use Ecto.Schema
import Ecto.Changeset
schema "baguettes" do
field(:name, :string)
field(:kind, :string)
field(:type, :string)
field(:secret, :string)
end
def changeset(user, params \\ %{}) do
user
|> cast(params, [:name, :kind, :type])
|> validate_required([:name])
end
enddefmodule MyApp.Schema.Baguettes do
use EctoCrux,
module: MyApp.Schema.Baguette,
order_by: [asc: :name],
select: [:name, :kind, :type]
# tips: this module is also the perfect place to implement
# all your custom accessors/operations arround this schema
# that are not covered by ecto_crux.
endthen you could (not exhaustive):
alias MyApp.Schema.Baguettes
# list all baguettes
baguettes = Baguettes.all()
# count baguettes
count = Baguettes.count()
# create an new baguette
{:ok, baguette} = Baguettes.create(%{kind: "baguepi"})
# get a baguette
baguette = Baguettes.get("01ESRJA5F0MTWH74ZXM9GVW06Y")
# get a baguette with it's secret
baguette = Baguettes.get("01ESRJA5F0MTWH74ZXM9GVW06Y",
select: [:name, :kind, :type, :secret]
)
# update it
{:ok, baguette} = Baguettes.update(baguette, %{kind: "baguepi"})
# delete it
Bachette.delete(baguette)
# find all baguepi baguettes, within Repo prefix "francaise"
baguettes = Baguettes.find_by(%{kind: "baguepi"}, [prefix: "francaise"])
# find all baguepi baguettes, within Repo prefix "francaise", that was not soft deleted
baguettes = Baguettes.find_by(%{kind: "baguepi"}, [prefix: "francaise", exclude_deleted: true])
# find only baguepi baguettes that was soft deleted
baguettes = Baguettes.find_by(%{kind: "baguepi"}, [only_deleted: true])
# find all baguepi baguetes ordered by `kind`, overrides the default `name` ordering
baguettes = Baguettes.find_by(%{type: "foo"}, [order_by: [asc: :kind]])
# find all baguepi baguettes, within Repo prefix "francaise" and paginate
%EctoCrux.Page{} = page = Baguettes.find_by(%{kind: "baguepi"}, [prefix: "francaise", page: 2, page_size: 15])
Functions you can now uses with MyApp.Schema.Baguettes are listed here.
Link to this section Summary
Functions
Checks that a function (atom + arguments count) is not in the list of excluded methods.
Link to this section Functions
Link to this function
excluded?(except, method, args)
Checks that a function (atom + arguments count) is not in the list of excluded methods.
Example: excluded?(@except, :create, 2)