Generates the write functions of BoxTurtle.Schema.Change.API for an Ecto schema.
Attribute maps go through the schema's changeset functions, so the schema must define them:
changeset/2 by default, for both inserts and updates.
defmodule MyApp.Blog.Post do
use Ecto.Schema
import Ecto.Changeset
schema "posts" do
field :title, :string
timestamps()
end
def changeset(post, attrs), do: post |> cast(attrs, [:title]) |> validate_required([:title])
end
# delete/1 by primary key looks rows up through the sibling Query module.
defmodule MyApp.Blog.Post.Query do
use BoxTurtle.Schema.Query, repo: MyApp.Repo
end
defmodule MyApp.Blog.Post.Change do
use BoxTurtle.Schema.Change, repo: MyApp.Repo
end
alias MyApp.Blog.Post
{:ok, post} = Post.Change.create(%{title: "Hello"})
{:error, %Ecto.Changeset{}} = Post.Change.create(%{title: nil})
{:ok, post} = Post.Change.update(post, %{title: "Hello again"})
{:ok, _deleted} = Post.Change.delete(post.id)
{:error, :not_found} = Post.Change.delete(post.id)Options
:repo- theEcto.Repoto write to. Required.:schema- the schema module. Defaults to the parent of the using module (MyApp.Blog.Postabove).:query- theBoxTurtle.Schema.Querymodule used to look rows up by key indelete/1. Defaults to<schema>.Query.:create_changeset- name of the schema'sfun(struct, attrs)used bycreate. Defaults to:changeset.:update_changeset- name of the schema'sfun(struct, attrs)used byupdate. Defaults to:changeset.:create_opts- options passed toEcto.Repo.insert/2, e.g.[on_conflict: :nothing].