EctoFilters

Provides a consistent way to transform request params into ecto query expressions.

Installation

def deps do
  [
    {:ecto_filters, "~> 0.1.1"}
  ]
end

Usage

defmodule Posts do
  use EctoFilters
  alias MyProject.{Post, Repo}

  def filter({:comment_body, value}, query) do
    query
    |> join(:left, [p], c in assoc(p, :comments), as: :comments)
    |> where([comments: comments], ilike(comments.body, ^value))
  end

  @doc """
  Returns the list of posts

  ## Examples

      iex> list_posts(%{"q" => %{"comment_body" => "ecto_filters"}})
      [%Post{}, ...]

  """
  def list_posts(params) do
    Post
    |> apply_filters(params)
    |> Repo.all()
  end
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/ecto_filters.