Query With Repo
Interact with DB/schemas usually in public context like Accounts
alias ReactPhoenix.Repo[Commands](Repo commands)
Get
Repo.get(User, id) #nil or %User{}
Repo.get!(User, id) #error or %User
MyRepo.get_by(Post, title: "My post") #error if not one result
Repo.all(User)
Repo.all(from u in User, select: u.email, where: u.age == ^age) #[...]
Repo.one(from u in User, where: ilike(u.email, "%1%"), select: count(u.id)) # of users with 1 in their email
Repo.one(from ea in EventAnalytics, where: ea.email == "j@g" and ea.type == "RSVP") #will error if > one response, nil if noneSee query for more complex stuff
If variable is nil, don't just compare use is_nil
Supports literal Integers, Floats, Bools, Binary(<<1, 2 >>), Strings, and arrays, other external variables must be pinned^. Using with Schema(User instead of "users") lets it know what type to cast to and automatically retrieves all fields
Alt syntax
Testing in IEX
Join
:inner_join, :left_join, :right_join, :cross_join, :full_join, :inner_lateral_join or :left_lateral_join. :join is equivalent to :inner_join(match both returned)
Preload
Can be used very similarly to a join, and keeps proper select and structure
I think both do two queries: in query
Aggregate
Last updated