Elixir and Ecto

Elixir and Ecto

B6a8f005f39d23ffc930508ac9da68b9?s=128

Patrick Van Stee

October 03, 2013
Tweet

Transcript

  1. elixir atlanta meetup

  2. meetup.com/ atlantaelixir @vanstee

  3. None
  4. • speakers • sponsors • twitter • website • github

    org • [your great idea]
  5. elixir and ecto

  6. Elixir is a functional, meta-programming aware language built on top

    of the Erlang VM.
  7. elixir-lang/elixir @josevalim

  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
  9. None
  10. Types

  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
  12. Pattern Matching

  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
  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
  15. Processes

  16. current_pid = self spawn fn -> current_pid <- :hello end

    receive do :hello -> IO.puts "Hello World" end PROCESS.EX
  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
  18. Protocols Macros DocTest and lots more

  19. elixir-lang.org #elixir-lang

  20. None
  21. None
  22. ecto database query DSL

  23. elixir-lang/ecto @ericmj

  24. defmodule User do use Ecto.Model queryable "user" do field :name,

    :string end end MODEL.EX
  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
  26. None
  27. Repo

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

    worker
  29. defmodule Repo do use Ecto.Repo, adapter: Ecto.Adapters.Postgres def url do

    "ecto://user:pass@localhost/db" end end Repo.all(...) Repo.create(...) Repo.update(...) Repo.delete(...) REPO.EX
  30. Entity

  31. • fields • associations • elixir record

  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
  33. Query

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

  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
  36. from u in User, where: u.name == ^name, limit: 1

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

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

    ), 1 ), [u], u ) QUERY.EX
  39. Gotchas

  40. • error messages • validations • callbacks • type conversions

    • SQL migrations • missing mix tasks
  41. elixir-lang/ecto examples/simple

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

  43. ?