# EctoJuno
A simple query sorting library

## Installation

Add `:ecto_juno` to the list of dependencies in `mix.exs`:

def deps do
    {:ecto_juno, git: ""}

## Configuration

You can specify default sorting field and mode by adding these lines into your config.exs:

  config :ecto_juno, sort_by: :id, sort_direction: :desc

The default sorting is by `inserted_at` field with `asc` mode

## Usage

Assuming you have Accounts context with User schema and Repo module all you need is to pass a ecto schema of model you sorting or list of fields by which sorting can be applied

  alias EctoJuno.Query.Sorting

  def list_sorted_users(params) do
    |> Sorting.sort_query(User, params)
    |> Repo.all()

Where `params` structure is 

  %{"sort_by" => "id", "sort_direction" => "desc"}

You can also pass sorting parameters keys as atoms:

  Sorting.sort_query(query, User, %{sort_by: "id", sort_direction: "desc"})

For missing parameters default sorting ones will be used:

  # The default sort_direction will be used
  Sorting.sort_query(query, User, %{sort_by: "id"})

  # The default sort_by will be used
  Sorting.sort_query(query, User, %{sort_direction: "desc"})

  # The default sorting which configurable will be used
  Sorting.sort_query(query, User)

If you'll pass invalid sorting parameters than default sorting ones will be used for your query:
  Sorting.sort_query(query, User, %{sort_by: "invalid_field", sort_direction: "invalid_mode"})
will sort query by `inserted_at` field with `asc` mode if configuration not modified

- Sorting by joint query is not yet supported.
- Sorting with modes different from asc and desc is not supported
- No custom validators for parameters yet supported
- if you pass sort_by and sort_direction values not as strings you'll get exception