$30 off During Our Annual Pro Sale. View Details »

Ecto is not your ORM

Randson
April 13, 2021

Ecto is not your ORM

Randson

April 13, 2021
Tweet

More Decks by Randson

Other Decks in Programming

Transcript

  1. ECTO IS NOT YOUR ORM After all, Elixir is not

    an Objected Oriented Language.
  2. Who am I? Randson! I’m a Software Engineer, interested in

    Computer Science and the craft of programming. • Back-end with Elixir; • Increasingly learning Erlang/OTP. Trying to make the world a better place to live, through technology.
  3. What we’ll going to see 01 O is for Objects

    02 Relational Mappers 03 Schemaless Queries
  4. O is for Objects 01 After all, Elixir is not

    an Objected-Oriented language.
  5. URP Uniform Resource Principle. All services offered by a module

    should be available together. It’s different State and Behaviour should be syntactically spoke differently. At it’s core, Objects couple State and Behaviour together.
  6. “All services by a module should be available through a

    uniform notation, which does not betray weather they are implemented through storage or computation.” — Bertrand Meyer
  7. Eiffel Lang Language created by Bertrand Meyer where it’s known

    by being cheaper, better, short time-to-market and easier.
  8. State In Ruby, you can access a property of an

    object like: user.name Behaviour Also in Ruby, you can call a behaviour(could be storage or computation) like: user.confirm() How does that work?
  9. State We work with different data structure such as Lists,

    Map, Tuples, etc... Behaviour Behaviour cannot be attached to data structures. Always added to modules through functions. Elixir fails in the “coupling state and behaviour”.
  10. Structs are only data. We can simply access the user

    email through user.email. We cannot call user.confirm(). We can add behaviour through functions. BOOM!
  11. Struct Instead, the User prefix is required and the user

    struct must be explicitly given as an argument. Behaviour There is not structural coupling between the user and the functions in the User module. It’s impossible to call user.confirm()
  12. Relational Mappers 02 Do we have a data type that

    maps to Elixir code? Yes, structs.
  13. Schemas Greate way to achieve a data structure on the

    language. Also, take a look at Embedded Schemas. Relational While Ecto is not Relational Mapper, it contains a Relational Mapper. How it is achieved using Ecto?
  14. Relying on schema information. Ecto knows how to read and

    write input from the developer. The data is coupled the way we want. In small applications it can be good, large ones can be a problem. BOOM!
  15. Saying Ecto is not your ORM does not automatically save

    Ecto schemas from some of the downsides many developers associate ORMs with.
  16. Some issues associated with ORMs that Ecto may run Projects

    may end up with “God Schemas”, “Fat Models”, etc... Excessively rely on schemas when sometimes the best way is using a map, tuple, etc... Using the same Schema for all operations that may end up with point one. BOOM!
  17. What we saw until now? Structs Good way to map

    your relations to Elixir data Schemas Like structs, but a really relation mapper from DB State Modules does not have behaviour behaviour Usually attached through functions to modules
  18. Schema is an easy way to write Queries using Ecto

    Most queries in Ecto are written using schemas. To retrieve all users from a database for example: MyApp.Repo.all(User) As Ecto knows all the fields that a schema has, it is simple to use: query = from p in Post, select: %Post{title: p.title, body: p.body} MyApp.Repo.all(query)
  19. Ecto also adds the ability to write our queries without

    schemas We can select all desired fields without any conformation with any schema. from “posts”, select: [:title, :body] With that, Ecto will automatically convert a list of fields to a struct or a map.
  20. You can perform many operations without relying on schemas Let’s

    take a look on a example where we increment a page view from a website: def increment_page_view(post) do query = from “posts”, where: [id: ^post.id], update: [inc: [page_views: 1]] MyApp.Repo.update_all(query, []) end
  21. Notice how we write our update function without setting up

    a schema and rely on pre-defined shape of data. Ecto make query with and without schemas more accessible. The function above doesn’t rely on schemas It enable developers to write dynamic queries.
  22. Schemas are mappers An Ecto schema is used to map

    any data source into an Elixir struct.
  23. Emphasis on any Database <-> Ecto <-> API/Forms In many

    cases, it’s better to break into two schemas An example we’ll use is as a registration form with first_name and last_name BOOM!
  24. Embedded schemas can save a party We use embedded schema

    because it is not our intent to persist it anywhere. defmodule Registration do use Ecto.Schema embedded_schema do field :first_name field :last_name field :email end end
  25. What is the most important lesson? Using schemas depends mostly

    if you want to use the same mapping on different places. Understand when a big problem can be broken into smaller parts. Using schemas didn’t affect the solution at all.
  26. Remember The most important part of this guide is to

    know when to break the application into smaller parts to decide or not use schemas.
  27. CREDITS: This presentation template was created by Slidesgo, including icons

    by Flaticon, and infographics & images by Freepik and illustrations by Stories THANKS Do you have any questions? [email protected] https://rands0n.com