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 full-size slide

  2. meetup.com/
    atlantaelixir
    @vanstee

    View full-size slide

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

    View full-size slide

  4. elixir and ecto

    View full-size slide

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

    View full-size slide

  6. elixir-lang/elixir
    @josevalim

    View full-size slide

  7. 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 full-size slide

  8. # 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 full-size slide

  9. Pattern
    Matching

    View full-size slide

  10. # 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 full-size slide

  11. 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 full-size slide

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

    View full-size slide

  13. 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 full-size slide

  14. Protocols
    Macros
    DocTest
    and lots more

    View full-size slide

  15. elixir-lang.org
    #elixir-lang

    View full-size slide

  16. ecto
    database query DSL

    View full-size slide

  17. elixir-lang/ecto
    @ericmj

    View full-size slide

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

    View full-size slide

  19. 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 full-size slide

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

    View full-size slide

  21. 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

    View full-size slide

  22. • fields
    • associations
    • elixir record

    View full-size slide

  23. 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 full-size slide

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

    View full-size slide

  25. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  30. elixir-lang/ecto
    examples/simple

    View full-size slide

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

    View full-size slide