Skip to content

Implement dynamic filtering and sorting API for Ecto queries

License

Notifications You must be signed in to change notification settings

valyukov/ex_sieve

Repository files navigation

ExSieve

CI Hex Version Coverage Status Hex docs

ExSieve is a filtering solution for Phoenix/Ecto. It builds Ecto.Query structs from a ransack inspired query language.

Installation

Add ex_sieve to your list of dependencies in mix.exs:

def deps do
  [
    {:ex_sieve, "~> 0.8.2"},
  ]
end

Nice to have

  • Add advanced search documentation
  • Configure predicate aliases
  • Demo project

Ecto internals currently used

  • %Ecto.Query{from: %{source: {_, module}}} Ecto.query struct internal structure, needed for extracting the main Ecto.Schema of the query

Usage

Setup your application repo, using ExSieve

defmodule MyApp.Repo do
  use Ecto.Repo,
    otp_app: :my_app,
    adapter: Ecto.Adapters.Postgres

  use ExSieve
end

and use the provided c:ExSieve.filter/3 callback for filtering entries based on query parameters

def index(conn, %{"q"=> params}) do
  posts = MyApp.Repo.filter(MyApp.Post, params)

  render conn, :index, posts: posts
end

Examples

For more details on the used query language and query examples see the official documentation.

Contributing

First, you'll need to build the test database.

DB_PASSWORD=<db_password> MIX_ENV=test mix ecto.reset

This task assumes you have postgres installed and that your current user can create / drop databases. If you'd prefer to use a different user, you can specify it with the environment variable DB_USER.

When the database is built, you can run the tests.

mix test

and

mix credo