Created
June 27, 2018 01:50
-
-
Save Ninigi/ad2e7869385ef8166d3eb3b32de64fa7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Without EctoDripper | |
defmodule QueryModule do | |
def query_status(query, args) | |
def query_status(query, %{username: username}) do | |
# Create query when username key is in args | |
# Since we have the key already in the function name, | |
# I find this quite pointless in terms of readability | |
from( | |
thing in query, | |
where: thing.username == ^username | |
) | |
end | |
def query_status(query, _args) do | |
# Let the query "fall through" if no username key in args | |
query | |
end | |
end | |
# With EctoDripper | |
defmodule QueryModule do | |
use EctoDripper, | |
composable_queries: [ | |
[:username, :my_username_func] | |
] | |
def my_username_func(queryable, args) do | |
# args only include username, because I specified that in my queries. | |
# No need to pattern match | |
from( | |
thing in query, | |
where: thing.username == ^username | |
) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment