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

Elixir and Ecto

Elixir and Ecto

Patrick Van Stee

October 03, 2013
Tweet

More Decks by Patrick Van Stee

Other Decks in Technology

Transcript

  1. elixir
    atlanta
    meetup

    View Slide

  2. meetup.com/
    atlantaelixir
    @vanstee

    View Slide

  3. View Slide

  4. • speakers
    • sponsors
    • twitter
    • website
    • github org
    • [your great idea]

    View Slide

  5. elixir and ecto

    View Slide

  6. Elixir is a functional,
    meta-programming
    aware language built on
    top of the Erlang VM.

    View Slide

  7. elixir-lang/elixir
    @josevalim

    View Slide

  8. defmodule Hello do
    IO.puts "Before world defined"
    def world do
    IO.puts "Hello World"
    end
    IO.puts "After world defined"
    end
    Hello.world
    HELLO.EX

    View Slide

  9. View Slide

  10. Types

    View Slide

  11. # tuple
    { :a, :b, :c }
    # list
    [1, 2, 3]
    [a: 1, b: 2, c: 3]
    # record
    defrecord User, name: "", age: nil
    User.new(name: "Patrick", age: 25)
    TYPE.EX

    View Slide

  12. Pattern
    Matching

    View Slide

  13. # assignment
    a = 1 # => 1
    # matching
    1 = a # => 1
    2 = a # => ** (MatchError) ...
    { ^a, b } = { 1, 2 } # b => 2
    [head | _] = [1, 2, 3] # head => 1
    PATTERN.EX

    View Slide

  14. case { 1, 2, 3 } do
    { 4, 5, 6 } ->
    "This won't match"
    { 1, x, 3 } when x > 0 ->
    "This will match and assign x"
    _ ->
    "No match"
    end
    GUARD.EX

    View Slide

  15. Processes

    View Slide

  16. current_pid = self
    spawn fn ->
    current_pid <- :hello
    end
    receive do
    :hello ->
    IO.puts "Hello World"
    end
    PROCESS.EX

    View Slide

  17. defmodule Stacker.Supervisor do
    use Supervisor.Behaviour
    def start_link(stack) do
    :supervisor.start_link(__MODULE__, stack)
    end
    def init(stack) do
    children = [worker(Stacker.Server, [stack])]
    supervise children, strategy: :one_for_one
    end
    end
    SUPERVISOR.EX

    View Slide

  18. Protocols
    Macros
    DocTest
    and lots more

    View Slide

  19. elixir-lang.org
    #elixir-lang

    View Slide

  20. View Slide

  21. View Slide

  22. ecto
    database query DSL

    View Slide

  23. elixir-lang/ecto
    @ericmj

    View Slide

  24. defmodule User do
    use Ecto.Model
    queryable "user" do
    field :name, :string
    end
    end
    MODEL.EX

    View Slide

  25. defmodule FindUser do
    import Ecto.Query
    def find_by_name(name) do
    query = from u in User,
    where: u.name == ^name,
    limit: 1
    Repo.all(query)
    end
    end
    QUERY.EX

    View Slide

  26. View Slide

  27. Repo

    View Slide

  28. • wrapper
    • holds connections
    • executes queries
    • supervised worker

    View Slide

  29. defmodule Repo do
    use Ecto.Repo,
    adapter: Ecto.Adapters.Postgres
    def url do
    "ecto://user:[email protected]/db"
    end
    end
    Repo.all(...)
    Repo.create(...)
    Repo.update(...)
    Repo.delete(...)
    REPO.EX

    View Slide

  30. Entity

    View Slide

  31. • fields
    • associations
    • elixir record

    View Slide

  32. defmodule User do
    use Ecto.Model
    queryable "user" do
    field :name, :string
    field :password, :string,
    default: "secret"
    has_many :projects, Project
    end
    end
    MODEL.EX

    View Slide

  33. Query

    View Slide

  34. • relational algebra
    • extendable
    • macros
    • keyword lists

    View Slide

  35. def paginate(query, page, size) do
    extend query,
    limit: size,
    offset: (page - 1) * size
    end
    query = FindUser.find_by_state("GA")
    query |> paginate(1, 50) |> Repo.all
    PAGINATE.EX

    View Slide

  36. from u in User,
    where: u.name == ^name,
    limit: 1
    QUERY.EX

    View Slide

  37. from(u in User) |>
    where([u], u.name == ^name) |>
    limit(1) |>
    select([u], u)
    QUERY.EX

    View Slide

  38. select(
    limit(
    where(
    from(u in User),
    [u], u.name == ^name
    ), 1
    ), [u], u
    )
    QUERY.EX

    View Slide

  39. Gotchas

    View Slide

  40. • error messages
    • validations
    • callbacks
    • type conversions
    • SQL migrations
    • missing mix tasks

    View Slide

  41. elixir-lang/ecto
    examples/simple

    View Slide

  42. Thanks!
    @josevalim
    @ericmj
    #elixir-lang

    View Slide

  43. ?

    View Slide