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

Ecto is not your ORM

2c5f73ce08b339317d69892fd6db84db?s=47 Randson
April 13, 2021

Ecto is not your ORM



April 13, 2021

More Decks by Randson

Other Decks in Programming


  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 is a way to work with structured data.

  11. 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!
  12. Separating struct from function definitions.

  13. 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()
  14. Elixir does not have methods, it has functions.

  15. Relational Mappers 02 Do we have a data type that

    maps to Elixir code? Yes, structs.
  16. WTH is that? Technique that converts data between incompatible systems,

    commonly databases, to objects, and back.
  17. 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?
  18. Definition of an User schema

  19. 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!
  20. Saying Ecto is not your ORM does not automatically save

    Ecto schemas from some of the downsides many developers associate ORMs with.
  21. 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!
  22. 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
  23. Schemaless Queries 03 You do not need all the mind-blowing

    things that were presented before.
  24. 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)
  25. 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.
  26. 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
  27. 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.
  28. Schemas are mappers An Ecto schema is used to map

    any data source into an Elixir struct.
  29. 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!
  30. 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
  31. Ecto can go beyond. Schemaless Changesets can also be an

    way to achieve more decouple.
  32. 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.
  33. You can use Ecto with or without it’s standards.

  34. 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.
  35. 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? orandson@gmail.com https://rands0n.com