SlideShare a Scribd company logo
@vanstee
operable.io
Ectoan ORM without objects
Ruby and ActiveRecord
Elixir and Ecto
=
ActiveRecord refresher
# Create an unpersisted record
user = User.new(name: "Caroline Holbrook")
# Modify the user
user.name = "Caroline Van Stee"
# Persist a record
user.save
# Retrieve a record
User.find(1)
# Run a complex query
User.where(admin: true).count
Calling methods on
objects, that have
methods which
return objects…
I love Ruby,
but what if we wanted
to build an ORM
without objects.
Data structures Functions
Models
Queries Attribute Mapping
Validations
Executing Queries
User.where(name: "Patrick")
query = { table: :users,
where: { name: "Patrick" } }
results = execute(connection, query)
users = results.map(&:to_model)
with objects
without objects
Wait, you just turned
1 line into 3.
How could that be
any better?
Queries are more flexible1
Connections can be
swapped out
2
Results are more flexible
and are just simple maps
3
Well, that's
how Ecto
works!
query = from u in User,
where: u.name == "Patrick"
results = Repo.all(query)
Integrated
Query
Language
Repo
Model
Changeset
Query
Repos are just
connections to
the database
user = %User{name: "Patrick"}
WriterRepo.insert!(user)
ReaderRepo.all(User)
Multiple Repos
Models
defmodule App.User do
use Ecto.Model
schema "users" do
field :name
end
end
Fields have types
defmodule JSON do
alias Postgres.TypeInfo
@behaviour Postgrex.Extension
def init(_parameters, opts),
do: Keyword.fetch!(opts, :library)
def matching(_library),
do: [type: "json"]
def format(_library),
do: :binary
def encode(%TypeInfo{type: "json"}, map, _state, library),
do: library.encode!(map)
def decode(%TypeInfo{type: "json"}, json, _state, library),
do: library.decode!(json)
end
Changesets
defmodule User do
use Ecto.Model
def changeset(user, params  :empty) do
user
|> cast(params, ~w(name email), ~w(age))
|> unique_constraint(:email)
end
end
Changesets
params = %{email: "patrick@vanstee.me"}
changeset = User.changeset(%User{}, params)
{:error, changeset} = Repo.insert(changeset)
changeset.errors
#=> [email: "has already been taken"]
Queries
from u in User,
join: p in Post, on: u.id == p.user_id,
where: u.name == "Patrick" or p.views > 500,
select: {u.name, p.title}
Everything else
• Migrations
• Mix tasks (like rake)
• Callbacks
• Validations
• PostgreSQL, MySQL, Mongo
⚡
elixir-lang.org
elixir-lang/ecto

More Related Content

PPTX
JSON Processing and mule
PPTX
Mule json transformers
PPTX
Introduction Big Data and Hadoop
PPTX
Javascript: The good parts for humans (part 6)
PPTX
Constructor overloading & method overloading
PPTX
Next Generation of Javascript
PPTX
Get docs from sp doc library
JSON Processing and mule
Mule json transformers
Introduction Big Data and Hadoop
Javascript: The good parts for humans (part 6)
Constructor overloading & method overloading
Next Generation of Javascript
Get docs from sp doc library

More from jasnow (20)

PDF
app/assets/stylesheets - How to not make a mess
ODP
ATLRUG Community Announcements for December 2016
PDF
Commitment
ODP
ATLRUG Community/Giveback Announcments
ODP
ATLRUG Announcements - October 2016
PDF
Seamless Migration
ODP
ATLRUG Announcements - Septemer 2016
PDF
Hacking Aaway with ionic
ODP
ATLRUG Announcements/Upgrade News - August 2016
ODP
ATLRUG Announcements - July 2016
PDF
Ruby on Docker
PDF
Getting Answers to Your Testing Questions
ODP
ATLRUG Announcments - May 2016
ODP
ATLRUG Announcements and Fun Facts - April 2016
ODP
ATLRUG Announcements - March 2016
ODP
Our ATLRUG.com Web Site
PDF
WebRTC: Real Time Video/Audio For Your App ...
ODP
ATLRUG Announcements for Feb. 2016
ODP
ATLRUG December 2015
PDF
Convox introduction by Matt Manning
app/assets/stylesheets - How to not make a mess
ATLRUG Community Announcements for December 2016
Commitment
ATLRUG Community/Giveback Announcments
ATLRUG Announcements - October 2016
Seamless Migration
ATLRUG Announcements - Septemer 2016
Hacking Aaway with ionic
ATLRUG Announcements/Upgrade News - August 2016
ATLRUG Announcements - July 2016
Ruby on Docker
Getting Answers to Your Testing Questions
ATLRUG Announcments - May 2016
ATLRUG Announcements and Fun Facts - April 2016
ATLRUG Announcements - March 2016
Our ATLRUG.com Web Site
WebRTC: Real Time Video/Audio For Your App ...
ATLRUG Announcements for Feb. 2016
ATLRUG December 2015
Convox introduction by Matt Manning
Ad

Recently uploaded (20)

PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Nekopoi APK 2025 free lastest update
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
System and Network Administration Chapter 2
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
ai tools demonstartion for schools and inter college
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
Operating system designcfffgfgggggggvggggggggg
PTS Company Brochure 2025 (1).pdf.......
Nekopoi APK 2025 free lastest update
How to Migrate SBCGlobal Email to Yahoo Easily
System and Network Administration Chapter 2
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Wondershare Filmora 15 Crack With Activation Key [2025
ai tools demonstartion for schools and inter college
How to Choose the Right IT Partner for Your Business in Malaysia
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
How Creative Agencies Leverage Project Management Software.pdf
Understanding Forklifts - TECH EHS Solution
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
2025 Textile ERP Trends: SAP, Odoo & Oracle
Ad

Ecto Introduction by Patrick Vanstee