Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Elixir and Ecto
Search
Patrick Van Stee
October 03, 2013
Technology
5
950
Elixir and Ecto
Patrick Van Stee
October 03, 2013
Tweet
Share
More Decks by Patrick Van Stee
See All by Patrick Van Stee
Raft: Consensus for Rubyists
vanstee
139
7.1k
Bootstrap
vanstee
8
790
HTTP API Design for iOS Applications
vanstee
11
640
Consensus: An Introduction to Raft
vanstee
21
3k
Convergent Replicated Data Types
vanstee
4
770
Pour Over Brewing Method
vanstee
1
360
Celluloid & DCell
vanstee
4
560
Map Reduce & Ruby
vanstee
10
820
Other Decks in Technology
See All in Technology
AIが書いたコードをAIが検証する!自律的なモバイルアプリ開発の実現
henteko
1
300
What is BigQuery?
aizack_harks
0
120
ACA でMAGI システムを社内で展開しようとした話
mappie_kochi
0
160
GC25 Recap+: Advancing Go Garbage Collection with Green Tea
logica0419
1
350
From Prompt to Product @ How to Web 2025, Bucharest, Romania
janwerner
0
110
ユニットテストに対する考え方の変遷 / Everyone should watch his live coding
mdstoy
0
120
Geospatialの世界最前線を探る [2025年版]
dayjournal
3
470
Pure Goで体験するWasmの未来
askua
1
170
Why React!?? Next.jsそしてReactを改めてイチから選ぶ
ypresto
10
4.2k
FastAPIの魔法をgRPC/Connect RPCへ
monotaro
PRO
1
660
「技術負債にならない・間違えない」 権限管理の設計と実装
naro143
35
10k
"複雑なデータ処理 × 静的サイト" を両立させる、楽をするRails運用 / A low-effort Rails workflow that combines “Complex Data Processing × Static Sites”
hogelog
3
1.7k
Featured
See All Featured
KATA
mclloyd
32
14k
The Language of Interfaces
destraynor
162
25k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
4 Signs Your Business is Dying
shpigford
185
22k
Making Projects Easy
brettharned
118
6.4k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
BBQ
matthewcrist
89
9.8k
The Straight Up "How To Draw Better" Workshop
denniskardys
237
140k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
19
1.2k
Code Reviewing Like a Champion
maltzj
525
40k
Why Our Code Smells
bkeepers
PRO
339
57k
VelocityConf: Rendering Performance Case Studies
addyosmani
332
24k
Transcript
elixir atlanta meetup
meetup.com/ atlantaelixir @vanstee
None
• speakers • sponsors • twitter • website • github
org • [your great idea]
elixir and ecto
Elixir is a functional, meta-programming aware language built on top
of the Erlang VM.
elixir-lang/elixir @josevalim
defmodule Hello do IO.puts "Before world defined" def world do
IO.puts "Hello World" end IO.puts "After world defined" end Hello.world HELLO.EX
None
Types
# tuple { :a, :b, :c } # list [1,
2, 3] [a: 1, b: 2, c: 3] # record defrecord User, name: "", age: nil User.new(name: "Patrick", age: 25) TYPE.EX
Pattern Matching
# assignment a = 1 # => 1 # matching
1 = a # => 1 2 = a # => ** (MatchError) ... { ^a, b } = { 1, 2 } # b => 2 [head | _] = [1, 2, 3] # head => 1 PATTERN.EX
case { 1, 2, 3 } do { 4, 5,
6 } -> "This won't match" { 1, x, 3 } when x > 0 -> "This will match and assign x" _ -> "No match" end GUARD.EX
Processes
current_pid = self spawn fn -> current_pid <- :hello end
receive do :hello -> IO.puts "Hello World" end PROCESS.EX
defmodule Stacker.Supervisor do use Supervisor.Behaviour def start_link(stack) do :supervisor.start_link(__MODULE__, stack)
end def init(stack) do children = [worker(Stacker.Server, [stack])] supervise children, strategy: :one_for_one end end SUPERVISOR.EX
Protocols Macros DocTest and lots more
elixir-lang.org #elixir-lang
None
None
ecto database query DSL
elixir-lang/ecto @ericmj
defmodule User do use Ecto.Model queryable "user" do field :name,
:string end end MODEL.EX
defmodule FindUser do import Ecto.Query def find_by_name(name) do query =
from u in User, where: u.name == ^name, limit: 1 Repo.all(query) end end QUERY.EX
None
Repo
• wrapper • holds connections • executes queries • supervised
worker
defmodule Repo do use Ecto.Repo, adapter: Ecto.Adapters.Postgres def url do
"ecto://user:pass@localhost/db" end end Repo.all(...) Repo.create(...) Repo.update(...) Repo.delete(...) REPO.EX
Entity
• fields • associations • elixir record
defmodule User do use Ecto.Model queryable "user" do field :name,
:string field :password, :string, default: "secret" has_many :projects, Project end end MODEL.EX
Query
• relational algebra • extendable • macros • keyword lists
def paginate(query, page, size) do extend query, limit: size, offset:
(page - 1) * size end query = FindUser.find_by_state("GA") query |> paginate(1, 50) |> Repo.all PAGINATE.EX
from u in User, where: u.name == ^name, limit: 1
QUERY.EX
from(u in User) |> where([u], u.name == ^name) |> limit(1)
|> select([u], u) QUERY.EX
select( limit( where( from(u in User), [u], u.name == ^name
), 1 ), [u], u ) QUERY.EX
Gotchas
• error messages • validations • callbacks • type conversions
• SQL migrations • missing mix tasks
elixir-lang/ecto examples/simple
Thanks! @josevalim @ericmj #elixir-lang
?