Skip to content

Instantly share code, notes, and snippets.

@Ch4s3
Created September 22, 2020 16:24
Show Gist options
  • Save Ch4s3/7c718eee8134f4f820c2a67d8f6e4359 to your computer and use it in GitHub Desktop.
Save Ch4s3/7c718eee8134f4f820c2a67d8f6e4359 to your computer and use it in GitHub Desktop.
@doc """
You can pipe queries into this build_where/3
fn and match on operators like :in, :==,
:ilike, etc and handle the particular
where semantics in each function case.
Pay attention to the positional binding
[...,e] which will capture the last
binding added to the piped in query.
See also: https://hexdocs.pm/ecto/Ecto.Query.html#module-positional-bindings
and. https://hexdocs.pm/ecto/Ecto.Query.API.html#field/2
"""
def build_where(query, :in, from_api) do
query
|> where([..., e],
field(e, ^from_api.field)
in ^from_api.values)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment