Slide 1

Slide 1 text

ECTO IS NOT YOUR ORM After all, Elixir is not an Objected Oriented Language.

Slide 2

Slide 2 text

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.

Slide 3

Slide 3 text

What we’ll going to see 01 O is for Objects 02 Relational Mappers 03 Schemaless Queries

Slide 4

Slide 4 text

O is for Objects 01 After all, Elixir is not an Objected-Oriented language.

Slide 5

Slide 5 text

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.

Slide 6

Slide 6 text

“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

Slide 7

Slide 7 text

Eiffel Lang Language created by Bertrand Meyer where it’s known by being cheaper, better, short time-to-market and easier.

Slide 8

Slide 8 text

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?

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Structs is a way to work with structured data.

Slide 11

Slide 11 text

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!

Slide 12

Slide 12 text

Separating struct from function definitions.

Slide 13

Slide 13 text

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()

Slide 14

Slide 14 text

Elixir does not have methods, it has functions.

Slide 15

Slide 15 text

Relational Mappers 02 Do we have a data type that maps to Elixir code? Yes, structs.

Slide 16

Slide 16 text

WTH is that? Technique that converts data between incompatible systems, commonly databases, to objects, and back.

Slide 17

Slide 17 text

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?

Slide 18

Slide 18 text

Definition of an User schema

Slide 19

Slide 19 text

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!

Slide 20

Slide 20 text

Saying Ecto is not your ORM does not automatically save Ecto schemas from some of the downsides many developers associate ORMs with.

Slide 21

Slide 21 text

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!

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Schemaless Queries 03 You do not need all the mind-blowing things that were presented before.

Slide 24

Slide 24 text

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)

Slide 25

Slide 25 text

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.

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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.

Slide 28

Slide 28 text

Schemas are mappers An Ecto schema is used to map any data source into an Elixir struct.

Slide 29

Slide 29 text

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!

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Ecto can go beyond. Schemaless Changesets can also be an way to achieve more decouple.

Slide 32

Slide 32 text

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.

Slide 33

Slide 33 text

You can use Ecto with or without it’s standards.

Slide 34

Slide 34 text

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.

Slide 35

Slide 35 text

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