View Source SwissSchema behaviour (swiss_schema v0.4.0)
SwissSchema
is a query toolkit for Ecto schemas. It makes it easy to manipulate data using Ecto schemas by implementing relevant Ecto.Repo Query API and Schema API functions, pre-configured to work specifically with the given Ecto schema.
Setup
Add swiss_schema
as a dependency in mix.exs
:
def deps do
[
# ...
{:swiss_schema, "~> 0.4"}
]
end
Then, use SwissSchema
in your Ecto schemas:
# lib/my_app/accounts/user.ex
defmodule MyApp.Accounts.User do
use Ecto.Schema
use SwissSchema, repo: MyApp.Repo
end
That's it, you should be good to go.
Usage
After setting up SwissSchema in your Ecto schema, there isn't much else to do. Just use the available functions now available in your own schema module:
iex> alias MyApp.Accounts.User
iex> User.all()
[]
iex> User.create(%{name: "John Smith", email: "john@smiths.net"})
{:ok, %User%{name: "John Smith", ...}}
iex> User.all()
[%User%{name: "John Smith", ...}]
SwissSchema
functions tries to mimic the Ecto.Repo's Query and Repo APIs by acting as a thin, pre-configured interface for Ecto.Repo callbacks.
Note
To keep examples short and simple, we use the example module from the previous section,
MyApp.Accounts.User
, in the functions documentation. So, when reading the docs for SwissSchema functions, assume the following alias was previously set up:alias MyApp.Accounts.User
So, the module
MyApp.Accounts.User
itself will be referred to just asUser
.
Summary
Callbacks
@callback all(opts :: Keyword.t()) :: [Ecto.Schema.t() | term()]
@callback create( params :: %{required(atom()) => term()}, opts :: Keyword.t() ) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
@callback create!( params :: %{required(atom()) => term()}, opts :: Keyword.t() ) :: Ecto.Schema.t()
@callback delete( schema :: Ecto.Schema.t(), opts :: Keyword.t() ) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
@callback delete!( schema :: Ecto.Schema.t(), opts :: Keyword.t() ) :: Ecto.Schema.t()
@callback delete_all(opts :: Keyword.t()) :: {non_neg_integer(), nil | [term()]}
@callback get( id :: term(), opts :: Keyword.t() ) :: {:ok, Ecto.Schema.t()} | {:ok, term()} | {:error, :not_found}
@callback get!( id :: term(), opts :: Keyword.t() ) :: Ecto.Schema.t() | term()
@callback get_by( clauses :: Keyword.t() | map(), opts :: Keyword.t() ) :: {:ok, Ecto.Schema.t()} | {:ok, term()} | {:error, :not_found}
@callback get_by!( clauses :: Keyword.t() | map(), opts :: Keyword.t() ) :: Ecto.Schema.t() | term()
@callback insert( params :: %{required(atom()) => term()}, opts :: Keyword.t() ) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
@callback insert!( params :: %{required(atom()) => term()}, opts :: Keyword.t() ) :: Ecto.Schema.t()
@callback insert_or_update( changeset :: Ecto.Changeset.t(), opts :: Keyword.t() ) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
@callback insert_or_update!( changeset :: Ecto.Changeset.t(), opts :: Keyword.t() ) :: Ecto.Schema.t()
@callback update( schema :: Ecto.Schema.t(), params :: %{required(atom()) => term()}, opts :: Keyword.t() ) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
@callback update!( schema :: Ecto.Schema.t(), params :: %{required(atom()) => term()}, opts :: Keyword.t() ) :: Ecto.Schema.t()
@callback update_all( updates :: Keyword.t(), opts :: Keyword.t() ) :: {non_neg_integer(), nil | [term()]}