Skip to content

Instantly share code, notes, and snippets.

@leikind
Created February 16, 2017 15:48
Show Gist options
  • Save leikind/cc3067ea0491ed1968a9ecee39447e5f to your computer and use it in GitHub Desktop.
Save leikind/cc3067ea0491ed1968a9ecee39447e5f to your computer and use it in GitHub Desktop.
defmodule Efrontback.AuthExamples do
alias Efrontback.Device
alias Efrontback.User
alias Efrontback.Repo
use Efrontback.Web, :model
def no_schema() do
real_device_token = "abcdefg"
query = from device in "devices",
where: device.real_device_token == ^real_device_token,
inner_join: user in "users", on: user.id == device.user_id,
limit: 1,
select: {device.platform, device.model, user.username, user.email}
Repo.one(query)
end
def with_a_schema() do
real_device_token = "abcdefg"
query = from device in Device,
where: device.real_device_token == ^real_device_token,
inner_join: user in "users", on: user.id == device.user_id,
limit: 1,
select: {device.platform, device.model, user.username}
Repo.one(query)
end
# no select will fetch the device
def with_a_schema_without_select() do
real_device_token = "abcdefg"
query = from device in Device,
where: device.real_device_token == ^real_device_token,
inner_join: user in User, on: user.id == device.user_id,
limit: 1
Repo.one(query)
end
def composition() do
real_device_token = "abcdefg"
query = from device in Device,
where: device.real_device_token == ^real_device_token,
limit: 1
query = from device in query,
inner_join: user in "users", on: user.id == device.user_id
Repo.one(query)
end
def composition_plus_join_plus_select() do
real_device_token = "abcdefg"
query = from device in Device,
where: device.real_device_token == ^real_device_token,
inner_join: user in User, on: user.id == device.user_id,
limit: 1
query = from [device, user] in query,
select: {device.platform, user.username}
Repo.one(query)
end
# 1 binding allows to drop bindings
# what is intetersting is that it returs a %Device{}, not a tuple
def one_binding_allows_to_drop_bindings() do
real_device_token = "abcdefg"
query = from Device,
where: [real_device_token: ^real_device_token],
limit: 1,
select: [:platform, :model]
Repo.one(query)
end
# 1 binding allows to drop bindings
# what is intetersting is that it returs a %Device{}, not a tuple
def one_binding_allows_to_drop_bindings2() do
real_device_token = "abcdefg"
where = [real_device_token: real_device_token]
limit = 1
select = [:platform, :model]
query = from Device, where: ^where, limit: ^limit, select: ^select
Repo.one(query)
end
def with_pipe_operator() do
real_device_token = "abcdefg"
query = Device
|> where([device], device.real_device_token == ^real_device_token)
|> join(:inner, [device], user in User, user.id == device.user_id)
|> limit(1)
Repo.one(query)
end
def joining_with_assoc() do
real_device_token = "abcdefg"
query = from device in Device,
inner_join: user in assoc(device, :user),
where: device.real_device_token == ^real_device_token,
limit: 1
Repo.one(query)
end
def joining_with_assoc_and_condition_on_joined_table() do
real_device_token = "abcdefg"
authentication_token = "xyzxyz"
query = from device in Device,
inner_join: user in assoc(device, :user),
where: device.real_device_token == ^real_device_token,
where: user.authentication_token == ^authentication_token,
preload: [:user]
Repo.one(query)
end
def joining_with_assoc_and_condition_on_joined_table_via_pipes() do
real_device_token = "abcdefg"
authentication_token = "xyzxyz"
query = Device
|> join(:inner, [device], user in assoc(device, :user))
|> where([device, user], device.real_device_token == ^real_device_token)
|> where([device, user], user.authentication_token == ^authentication_token)
|> preload([:user])
Repo.one(query)
end
def with_assoc() do
real_device_token = "abcdefg"
query = from device in Device,
where: device.real_device_token == ^real_device_token,
preload: [:user]
Repo.one(query)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment