Slide 1

Slide 1 text

ecto a database wrapper and language integrated query for Elixir

Slide 2

Slide 2 text

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)

Slide 3

Slide 3 text

simplicity over convenience

Slide 4

Slide 4 text

“If you want Ecto to get something, you have to explicitly ask for it. This feature will probably seem a little tedious to you…”

Slide 5

Slide 5 text

“…but it is the only way to guarantee your application has predictable performance when the amount of data grows.”

Slide 6

Slide 6 text

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)

Slide 7

Slide 7 text

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)

Slide 8

Slide 8 text

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)

Slide 9

Slide 9 text

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)

Slide 10

Slide 10 text

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)

Slide 11

Slide 11 text

‣ data mapper ‣ query language ‣ associations ‣ filtering ‣ validations ‣ lifecycle callbacks ‣ changesets ‣ migrations ‣ custom types ‣ embedded structs

Slide 12

Slide 12 text

compose queries

Slide 13

Slide 13 text

# Create a query query = from w in Weather, where: w.prcp > 0 # Extend the query query = from w in query, select: w.city

Slide 14

Slide 14 text

repository

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

changesets

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

def registration_changeset(model, params) do model |> changeset(params) |> cast(params, ~w(password), []) |> validate_length(:password, min: 6, max: 100) |> put_pass_hash() end

Slide 26

Slide 26 text

associations

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

familiar different pragmatic fun