Provides utility to easy reduce filters on query
def deps do
[
{:with_filters, git: "https://github.com/senconscious/with_filters.git", tag: "0.0.1"}
]
end
Provides utility functions to reduce boilerplate when introducing filtering from map/struct.
defmodule Acme.Deals.DealQuery do
use WithFilters
def list_deals do
Deal
|> with_filters(%{client_budget: 1_000, status: :finished})
|> Repo.all()
end
def with_filter(query, {:status, status}) when is_deal_status(status) do
where(query, [deal], deal.status == ^status)
end
def with_filter(query, {:client_budget, client_budget}) when is_integer(client_budget) do
where(query, [deal], deal.client_budget == ^client_budget)
end
...
If there is no clause for filter than Logger.warning/1
macro will be invoked.
You can ignore specific filters to avoid receiving warning from Logger like that:
use WithFilters, ignored: [:page, :page_size]
You can automatically skip filter if it's value is nil
with :optional
option:
use WithFilters, optional: [:action]
Be advised that library is intended to be used only with atom keys. That's is on purpose. Never trust user input.