FatEcto.Sort.Sortable behaviour (FatEcto v1.2.0)

View Source

Provides functionality to define sortable fields with override support.

Example Usage

defmodule MyApp.UserSort do
  use FatEcto.Sort.Sortable,
    sortable: [email: "*", name: ["$ASC", "$DESC"]],
    overrideable: ["custom_field"]

  @impl true
  def override_sortable("custom_field", "$ASC") do
    {:asc, dynamic([u], fragment("?->>'custom_field'", u.metadata))}
  end

  def override_sortable("custom_field", "$DESC") do
    {:desc, dynamic([u], fragment("?->>'custom_field'", u.metadata))}
  end

  def override_sortable(_field, _operator) do
    nil
  end
end

# Usage:
order_by = MyApp.UserSort.build(%{"email" => "$DESC", "custom_field" => "$ASC"})
query = from(u in User, order_by: ^order_by)

Summary

Callbacks

Callback for handling custom sorting logic.

Callbacks

override_sortable(field, operator)

@callback override_sortable(field :: String.t(), operator :: String.t()) ::
  FatEcto.Sort.Sorter.order_expr() | nil

Callback for handling custom sorting logic.

Should return {direction, dynamic} tuple or nil if not handling the field.