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

Ecto: the easy and the hard

Ecto: the easy and the hard

Sobolev Nikita

March 02, 2018
Tweet

More Decks by Sobolev Nikita

Other Decks in Programming

Transcript

  1. DSL для коммуникации с базами данных оболочка, которая предоставляет возможность

    коммуникации с базой данных The No- Compromise Database Wrapper predominant library for working with databases popular database wrapper and integrated query language
  2. use Mix.Config config :ecto_is_easy, EctoIsEasy.Repo, adapter: Ecto.Adapters.Postgres, database: "ecto_is_easy_repo", username:

    "user", password: "pass", hostname: "localhost" config :ecto_is_easy, ecto_repos: [EctoIsEasy.Repo]
  3. defmodule EctoIsEasy.Repo.Migrations.CreateArticls do use Ecto.Migration def change do create table(:articles)

    do add :link, :uuid add :body, :text add :visits, :integer timestamps end create index(:articles, [:link]) end end
  4. defmodule EctoIsEasy.Article do use Ecto.Schema import Ecto.Changeset schema "articles" do

    field :link, Ecto.UUID field :body, :string field :visits, :integer, default: 0 timestamps() end @fields ~w(link body visits) def changeset(data, params \\ %{}) do data |> cast(params, @fields) |> validate_required([:link, :body]) |> validate_length(:body, min: 100) end end
  5. iex(2)> article = EctoIsEasy.Article.changeset(%Article{}, %{ link: Ecto.UUID.generate(), body: "Some long

    body", }) #Ecto.Changeset< action: nil, changes: %{ body: "Some long body", link: "c44a6196-7c33-4483-b1aa-53820d58bf2d" }, errors: [], data: #EctoIsEasy.Article<>, valid?: true >
  6. iex(4)> EctoIsEasy.Repo.insert article {:ok, %EctoIsEasy.Article{ __meta__: #Ecto.Schema.Metadata<:loaded, "articles">, body: "Some

    long body", id: 1, inserted_at: ~N[2018-03-02 14:20:38.776091], link: "c44a6196-7c33-4483- b1aa-53820d58bf2d", updated_at: ~N[2018-03-02 14:20:38.778340], visits: 0 }}
  7. iex(1)> import Ecto.Query iex(2)> query = from(a in Article, where:

    a.visits == 0) iex(3)> Repo.all query [ %EctoIsEasy.Article{ __meta__: #Ecto.Schema.Metadata<"articles">, body: "Some long body", id: 1, inserted_at: ~N[2018-03-02 14:20:38.776091], link: "c44a6196-7c33-4483-b1aa-53820d58bf2d", updated_at: ~N[2018-03-02 14:20:38.778340], visits: 0 } ]
  8. Ecto.Multi defmodule EctoIsEasy.BanHammer do alias Ecto.Multi def ban(article) do Multi.new

    |> Multi.update(:article, ...) |> Multi.insert(:log, ...) |> Multi.update(:author, ...) end end iex(1)> Repo.transaction(BanHammer.ban(article))
  9. defmodule EctoAutoslugField.Type do @moduledoc """ This module represents a simple

    wrapper around ':string'. """ @behaviour Ecto.Type def type, do: :string def cast(string) when is_binary(string), do: {:ok, string} def cast(_), do: :error def load(string) when is_binary(string), do: {:ok, string} def load(_), do: :error def dump(string) when is_binary(string), do: {:ok, string} def dump(_), do: :error end
  10. defmodule Person do use Ecto.Model schema "people" do field :name

    embeds_many :addresses, Address end end defmodule Address do use Ecto.Model embedded_schema do field :street_name field :city field :state field :zip_code end end
  11. Полезные материалы • Учебник на русском: https:// elixirschool.com/ru/lessons/specifics/ecto • Книга

    "What's new in Ecto 2.0" на русском: https://github.com/wunsh/ecto-book-ru • ecto_autoslug_field: https://github.com/ sobolevn/ecto_autoslug_field • Ecto.Multi: https://medium.com/heresy-dev/ a-brief-guide-to-ecto-multi-9c8ea0c729f0