Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ecto: a database wrapper and language integrate...

Ecto: a database wrapper and language integrated query for Elixir

Arjan van der Gaag

October 28, 2015
Tweet

More Decks by Arjan van der Gaag

Other Decks in Programming

Transcript

  1. 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)
  2. “If you want Ecto to get something, you have to

    explicitly ask for it. This feature will probably seem a little tedious to you…”
  3. “…but it is the only way to guarantee your application

    has predictable performance when the amount of data grows.”
  4. 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)
  5. 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)
  6. 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)
  7. 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)
  8. 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)
  9. ‣ data mapper ‣ query language ‣ associations ‣ filtering

    ‣ validations ‣ lifecycle callbacks ‣ changesets ‣ migrations ‣ custom types ‣ embedded structs
  10. # Create a query query = from w in Weather,

    where: w.prcp > 0 # Extend the query query = from w in query, select: w.city
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. def registration_changeset(model, params) do model |> changeset(params) |> cast(params, ~w(password),

    []) |> validate_length(:password, min: 6, max: 100) |> put_pass_hash() end
  21. 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