Ecto.Rut v1.2.2 Ecto.Rut behaviour
Provides simple, sane and terse shortcuts for Ecto models.
Ecto.Rut is a wrapper around Ecto.Repo
methods that usually require you to pass
the module as the subject and sometimes even require you do extra work before hand,
(as in the case of Repo.insert/3
) to perform operations on your database. Ecto.Rut
tries to reduce code repetition by following the “Convention over Configuration”
ideology.
For example, once set up, it allows you to do this on a model called Post
:
# Create a Post
Post.insert(title: "Introduction to Elixir", categories: ["Elixir", "Programming"])
# Get all Posts
Post.all
# Get a Post with its id
Post.get(3)
# Get a Post with another attribute
Post.get_by(published_date: "2016-02-24")
# Delete a Post
Post.delete(lame_post)
Installation
Once added to your mix dependencies, all you need to do is call use Ecto.Rut
in
your Ecto models:
defmodule YourApp.Post do
use Ecto.Schema
use Ecto.Rut
# Schema, Changeset and other stuff...
end
Phoenix Projects
If you’re using Ecto with a Phoenix project, instead of calling use Ecto.Rut
in all of
your models, you can just call it once in the model/0
method of your web/web.ex
file:
# web/web.ex
def model do
quote do
use Ecto.Schema
use Ecto.Rut
# Other stuff...
end
end
Configuration
You can also pass options to Ecto.Rut when calling use
on it. These values are inferred
automatically by Ecto.Rut, but you can set them yourself in those special cases where it
can’t. The two options are:
:model
You set this when your Ecto Model is different from the module where you are calling
use
:repo
You set this when your app’s Ecto.Repo module is set differently.
defmodule YourApp.OtherNamespace.Post do
use Ecto.Schema
use Ecto.Rut, model: YourApp.Post, repo: YourApp.CustomEcto.Repo
# Other Stuff
end
Export Changeset
Methods like insert/1
or update/2
depend on your model exporting a public function called
changeset(struct, params)
with all your desired validations and constraints applied to
the casted struct.
Ecto.Rut uses this function to convert maps, keyword lists and other types into Ecto.Changeset
,
before updating or inserting them into the database.
Phoenix projects generate them for your models automatically, but for other Elixir projects, you can see an example here.
Summary
Callbacks
Fetches all entries from the Datastore for the Model
Deletes a struct using its primary key
Similar to delete/1
but returns the struct or raises if the changeset is invalid
Deletes all entries of the model
Fetches a single struct from the data store where the primary key matches the given id
Similar to get/1
but raises Ecto.NoResultsError
if no record was found
Fetches a single struct from the data store that matches the passed clauses
Similar to get_by/1
but raises Ecto.NoResultsError
if no record was found
Inserts a new record (Can be a struct, changeset, keyword list or a map)
Similar to insert/1
but returns the struct or raises if the changeset is invalid
Updates the database record using a modified struct or a changeset
Updates the database record using a Keyword list or a Map and a Struct for comparison
Similar to update/1
but returns the struct or raises if the changeset is invalid
Similar to update/2
but returns the struct or raises if the changeset is invalid
Callbacks
Fetches all entries from the Datastore for the Model
See Ecto.Repo.all/2
.
Example
Post.all
delete(struct_or_changeset :: Ecto.Schema.t | Ecto.Changeset.t) :: {:ok, Ecto.Schema.t} | {:error, Ecto.Changeset.t}
Deletes a struct using its primary key.
Returns {:ok, struct}
if the struct was successfully deleted or {:error, changeset}
if there was a validation or a known constraint error.
See Ecto.Repo.delete/2
.
Example
case Post.delete(post) do
{:ok, struct} -> # Deleted with success
{:error, changeset} -> # Something went wrong
end
delete!(struct_or_changeset :: Ecto.Schema.t | Ecto.Changeset.t) :: Ecto.Schema.t | no_return
Similar to delete/1
but returns the struct or raises if the changeset is invalid.
Also see Ecto.Repo.delete!/2
.
Deletes all entries of the model
Returns a tuple containing the number of items deleted. Also see Ecto.Repo.delete_all/2
.
Example
Post.delete_all
# => {34, nil}
Fetches a single struct from the data store where the primary key matches the given id.
Returns nil if no result was found. If the struct in the queryable has no or more than one
primary key, it will raise an argument error. See Ecto.Repo.get/3
.
Example
Post.get(3)
Post.get("0e531047-6bd2-4ab1-94c3-817fba988dbe")
Similar to get/1
but raises Ecto.NoResultsError
if no record was found.
Also see Ecto.Repo.get!/3
.
Fetches a single struct from the data store that matches the passed clauses.
Returns nil
if no result was found. See Ecto.Repo.get_by/3
.
Example
Post.get_by(title: "Introduction to Elixir")
Post.get_by(published_date: "2015-10-15")
Similar to get_by/1
but raises Ecto.NoResultsError
if no record was found.
Also see Ecto.Repo.get_by!/3
.
insert(struct :: Ecto.Schema.t | Ecto.Changeset.t | Map.t | Keyword.t) :: {:ok, Ecto.Schema.t} | {:error, Ecto.Changeset.t}
Inserts a new record (Can be a struct, changeset, keyword list or a map).
In case a changeset is given, the changes in the changeset are merges with the struct fields and all of them are sent to the database.
In case a struct, keyword list or a map is given, they are first converted to a changeset, with all non-nil fields as part of the changeset and inserted into the database if it’s valid.
Returns a {:ok, struct} if it was successfully inserted, or a {:error, changeset} is there was a validation or a known constraint error.
Also see Ecto.Repo.insert/2
.
Requires a changeset method
This method depends on your model exporting a public changeset function. See this for more details.
Example
Post.insert(title: "Introduction to Elixir")
Post.insert(%{title: "Building your first Phoenix app"})
Post.insert(%Post{title: "Concurrency in Elixir", categories: ["programming", "elixir"]})
Post.changeset(%Post{}, %{title: "Ecto for dummies"}) |> Post.insert
insert!(struct :: Ecto.Schema.t | Ecto.Changeset.t | Map.t | Keyword.t) :: Ecto.Schema.t | no_return
Similar to insert/1
but returns the struct or raises if the changeset is invalid.
Also see Ecto.Repo.insert!/2
.
update(modified_struct_or_changeset :: Ecto.Schema.t | Ecto.Changeset.t) :: {:ok, Ecto.Schema.t} | {:error, Ecto.Changeset.t}
Updates the database record using a modified struct or a changeset.
This method only accepts one argument; either a modified struct or a changeset. It uses the
struct or changeset’s primary key to update the correct record in the database. If no primary
key is found, Ecto.NoPrimaryKeyFieldError
will be raised.
Returns {:ok, struct}
if the struct has been successfully updated or {:error, changeset}
if there was a validation or a known constraint error.
Requires a changeset method
This method depends on your model exporting a public changeset function. See this for more details.
Example
post = Post.get_by!(id: 3)
post = %{ post | title: "Updated post title"}
Post.update(post)
update(struct :: Ecto.Schema.t, params :: Map.t | Keyword.t) :: {:ok, Ecto.Schema.t} | {:error, Ecto.Changeset.t}
Updates the database record using a Keyword list or a Map and a Struct for comparison.
This method accepts two arguments, the first being the struct that has to be updated, and the second being a Map or a Keyword List of the new values.
Returns {:ok, struct}
if the struct has been successfully updated or {:error, changeset}
if there was a validation or a known constraint error. Also see Ecto.Repo.update/2
.
Requires a changeset method
This method depends on your model exporting a public changeset function. See this for more details.
Example
post = Post.get_by!(id: 3)
Post.update(post, title: "New post title", author_id: new_author_id)
update!(modified_struct_or_changeset :: Ecto.Schema.t | Ecto.Changeset.t) :: Ecto.Schema.t | no_return
Similar to update/1
but returns the struct or raises if the changeset is invalid.
update!(struct :: Ecto.Schema.t, params :: Map.t | Keyword.t) :: Ecto.Schema.t | no_return
Similar to update/2
but returns the struct or raises if the changeset is invalid.