Share
Embed
Ecto: a database wrapper and language integrated query for Elixir
Arjan van der Gaag
October 28, 2015
Transcript
28 slides
-
defmodule Weather do use Ecto.Model schema "weather" do field :city
# Defaults to type :string field :temp_lo, :integer field :temp_hi, :integer field :prcp, :float, default: 0.0 end end query = from w in Weather, where: w.prcp > 0 or is_nil(w.prcp), select: w Repo.all(query) -
“If you want Ecto to get something, you have to
explicitly ask for it. This feature will probably seem a little tedious to you…” -
“…but it is the only way to guarantee your application
has predictable performance when the amount of data grows.” -
defmodule Weather do use Ecto.Model schema "weather" do field :city
# Defaults to type :string field :temp_lo, :integer field :temp_hi, :integer field :prcp, :float, default: 0.0 end end query = from w in Weather, where: w.prcp > 0 or is_nil(w.prcp), select: w Repo.all(query) -
defmodule Weather do use Ecto.Model schema "weather" do field :city
# Defaults to type :string field :temp_lo, :integer field :temp_hi, :integer field :prcp, :float, default: 0.0 end end query = from w in Weather, where: w.prcp > 0 or is_nil(w.prcp), select: w Repo.all(query) -
defmodule Weather do use Ecto.Model schema "weather" do field :city
# Defaults to type :string field :temp_lo, :integer field :temp_hi, :integer field :prcp, :float, default: 0.0 end end query = from w in Weather, where: w.prcp > 0 or is_nil(w.prcp), select: w Repo.all(query) -
defmodule Weather do use Ecto.Model schema "weather" do field :city
# Defaults to type :string field :temp_lo, :integer field :temp_hi, :integer field :prcp, :float, default: 0.0 end end query = from w in Weather, where: w.prcp > 0 or is_nil(w.prcp), select: w Repo.all(query) -
defmodule Weather do use Ecto.Model schema "weather" do field :city
# Defaults to type :string field :temp_lo, :integer field :temp_hi, :integer field :prcp, :float, default: 0.0 end end query = from w in Weather, where: w.prcp > 0 or is_nil(w.prcp), select: w Repo.all(query) -
‣ data mapper ‣ query language ‣ associations ‣ filtering
‣ validations ‣ lifecycle callbacks ‣ changesets ‣ migrations ‣ custom types ‣ embedded structs -
# Create a query query = from w in Weather,
where: w.prcp > 0 # Extend the query query = from w in query, select: w.city -
Repo.get_by(Post, title: "My post") Repo.transaction(fn -> Repo.update!(%{alice | balance: alice.balance
- 10}) Repo.update!(%{bob | balance: bob.balance + 10}) end) post = Repo.get!(Post, 42) post = %{post | title: "New title"} case Repo.update post do {:ok, model} -> # Updated with success {:error, changeset} -> # Something went wrong end -
Repo.get_by(Post, title: "My post") Repo.transaction(fn -> Repo.update!(%{alice | balance: alice.balance
- 10}) Repo.update!(%{bob | balance: bob.balance + 10}) end) post = Repo.get!(Post, 42) post = %{post | title: "New title"} case Repo.update post do {:ok, model} -> # Updated with success {:error, changeset} -> # Something went wrong end -
Repo.get_by(Post, title: "My post") Repo.transaction(fn -> Repo.update!(%{alice | balance: alice.balance
- 10}) Repo.update!(%{bob | balance: bob.balance + 10}) end) post = Repo.get!(Post, 42) post = %{post | title: "New title"} case Repo.update post do {:ok, model} -> # Updated with success {:error, changeset} -> # Something went wrong end -
defmodule User do use Ecto.Model import Ecto.Changeset schema "users" do
field :name field :email field :age, :integer end def changeset(user, params \\ :empty) do user |> cast(params, ~w(name email), ~w(age)) |> validate_format(:email, ~r/@/) |> unique_constraint(:email) |> validate_inclusion(:age, 18..100) end end -
defmodule User do use Ecto.Model import Ecto.Changeset schema "users" do
field :name field :email field :age, :integer end def changeset(user, params \\ :empty) do user |> cast(params, ~w(name email), ~w(age)) |> validate_format(:email, ~r/@/) |> unique_constraint(:email) |> validate_inclusion(:age, 18..100) end end -
defmodule User do use Ecto.Model import Ecto.Changeset schema "users" do
field :name field :email field :age, :integer end def changeset(user, params \\ :empty) do user |> cast(params, ~w(name email), ~w(age)) |> validate_format(:email, ~r/@/) |> unique_constraint(:email) |> validate_inclusion(:age, 18..100) end end -
defmodule User do use Ecto.Model import Ecto.Changeset schema "users" do
field :name field :email field :age, :integer end def changeset(user, params \\ :empty) do user |> cast(params, ~w(name email), ~w(age)) |> validate_format(:email, ~r/@/) |> unique_constraint(:email) |> validate_inclusion(:age, 18..100) end end -
defmodule User do use Ecto.Model import Ecto.Changeset schema "users" do
field :name field :email field :age, :integer end def changeset(user, params \\ :empty) do user |> cast(params, ~w(name email), ~w(age)) |> validate_format(:email, ~r/@/) |> unique_constraint(:email) |> validate_inclusion(:age, 18..100) end end -
defmodule User do use Ecto.Model import Ecto.Changeset schema "users" do
field :name field :email field :age, :integer end def changeset(user, params \\ :empty) do user |> cast(params, ~w(name email), ~w(age)) |> validate_format(:email, ~r/@/) |> unique_constraint(:email) |> validate_inclusion(:age, 18..100) end end -
def registration_changeset(model, params) do model |> changeset(params) |> cast(params, ~w(password),
[]) |> validate_length(:password, min: 6, max: 100) |> put_pass_hash() end -
query = from p in Post, preload: [:comments] query =
from p in Post, join: c in assoc(p, :comments), where: c.published_at > p.updated_at, preload: [comments: c] query |> Repo.all