ecto_resource v1.0.2 EctoResource
This module represents the generic CRUD functionality that is boilerplate within Phoenix context files. It provides a DSL to easily generate the basic functions for a schema. This allows the context to focus on interesting, atypical implementations rather than the redundent, drifting crud functions.
Link to this section Summary
Functions
Returns a list of all the records for a given Schema.
Creates a changeset for a given Schema
Creates a new record with the given changeset
Same as create/3 but returns the struct or raises if the changeset is invalid.
Deletes a given Schema struct
Same as delete/2 but returns the struct or raises if the changeset is invalid.
Gets a Schema struct by the given id. Can also take options to alter the resulting query.
Same as get/4 but raises Ecto.NoResultsError if no record was found.
Get an underscored module name for use in generating functions.
Updates a given Schema with the given attributes
Same as update/4 but returns the struct or raises if the changeset is invalid.
Link to this section Functions
all(repo, schema, options \\ [])
all(Ecto.Repo.t(), module(), term()) :: [Ecto.Schema.t()]
Returns a list of all the records for a given Schema.
Examples
iex> all(Repo, User)
[
%User{
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
id: 1,
inserted_at: ~N[2019-08-15 18:52:50],
email: "example@example.com",
updated_at: ~N[2019-08-15 18:52:50]
}
]
change(schema, changable)
change(module(), Ecto.Schema.t()) :: Ecto.Changeset.t()
Creates a changeset for a given Schema
iex> change(%User{email: "user@example.com"}, %{email: "updated-email@example.com"})
#Ecto.Changeset<
action: nil,
changes: %{},
errors: [],
data: #User<>,
valid?: true
>
create(repo, schema, attributes)
create(Ecto.Repo.t(), module(), map()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Creates a new record with the given changeset
Examples
iex> create(Repo, User, %{email: "user@example.com"})
{:ok,
%User{
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
inserted_at: ~N[2019-08-17 00:41:41],
email: "example@example.com",
updated_at: ~N[2019-08-17 00:41:41]
}}
create!(repo, schema, attributes)
create!(Ecto.Repo.t(), module(), map()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Same as create/3 but returns the struct or raises if the changeset is invalid.
delete(repo, deletable)
delete(Ecto.Repo.t(), Ecto.Schema.t()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Deletes a given Schema struct
Examples
iex> delete(Repo, %User{id: 1})
{:ok,
%User{
__meta__: #Ecto.Schema.Metadata<:deleted, "users">,
id: 1,
inserted_at: ~N[2019-08-17 00:41:41],
email: "example@example.com",
updated_at: ~N[2019-08-17 00:41:41]
}}
delete!(repo, deletable)
delete!(Ecto.Repo.t(), Ecto.Schema.t()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Same as delete/2 but returns the struct or raises if the changeset is invalid.
get(repo, schema, id, options \\ [])
get(Ecto.Repo.t(), module(), term(), term()) :: Ecto.Schema.t() | nil
Gets a Schema struct by the given id. Can also take options to alter the resulting query.
Examples
iex> get(Repo, User, 1)
%User{
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
id: 1,
inserted_at: ~N[2019-08-15 18:52:50],
email: "example@example.com",
updated_at: ~N[2019-08-15 18:52:50]
}
get!(repo, schema, id, options \\ [])
get!(Ecto.Repo.t(), module(), term(), term()) :: Ecto.Schema.t() | nil
Same as get/4 but raises Ecto.NoResultsError if no record was found.
resource_descriptions(resources)
Get an underscored module name for use in generating functions.
Examples
iex> underscore_module_name(User)
"user"
update(repo, schema, updateable, attributes)
update(Ecto.Repo.t(), module(), Ecto.Schema.t(), map()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Updates a given Schema with the given attributes
Examples
iex> update(Repo, User, %User{id: 1}, %{email: "updated@example.com"})
{:ok,
%User{
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
id: 1,
inserted_at: ~N[2019-08-15 18:52:50],
email: "updated@example.com",
updated_at: ~N[2019-08-17 01:16:01]
}}
update!(repo, schema, updateable, attributes)
update!(Ecto.Repo.t(), module(), Ecto.Schema.t(), map()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Same as update/4 but returns the struct or raises if the changeset is invalid.