Alembic v3.3.0 Alembic.Fetch.Sort

An individual sort in an Alembic.Fetch.Sorts.t

Summary

Types

The name of an attribute on the primary data or relationship to sort

The direction to sort. Default to :ascending per the JSONAPI spec. Can be :descending when the dot-separated attribute path is prefixed with -

t()
  • :attribute - the name of the attribute to sort
  • :direction - the direction to sort :attribute. Defaults to :ascending. Can also be :descending.
  • :relationship - the path to the relationship :attribute is on. nil means the attribute is on the primary data

Functions

Breaks the (optionally prefixed) attribute path into a t

Converts a sort back to the string format parsed by from_string/1

Types

attribute_name()
attribute_name() :: String.t

The name of an attribute on the primary data or relationship to sort

direction()
direction() :: :ascending | :descending

The direction to sort. Default to :ascending per the JSONAPI spec. Can be :descending when the dot-separated attribute path is prefixed with -.

t()
t() :: %Alembic.Fetch.Sort{attribute: attribute_name, direction: direction, relationship: Alembic.Fetch.Includes.include | nil}
  • :attribute - the name of the attribute to sort
  • :direction - the direction to sort :attribute. Defaults to :ascending. Can also be :descending.
  • :relationship - the path to the relationship :attribute is on. nil means the attribute is on the primary data

Functions

from_string(string)
from_string(String.t) :: t

Breaks the (optionally prefixed) attribute path into a t.

A single attribute name will have the default direction of :ascending and no :relationship

iex> Alembic.Fetch.Sort.from_string("inserted-at")
%Alembic.Fetch.Sort{attribute: "inserted-at", direction: :ascending, relationship: nil}

An attribute name with - before it will have the direction reversed to :descending.

iex> Alembic.Fetch.Sort.from_string("-inserted-at")
%Alembic.Fetch.Sort{attribute: "inserted-at", direction: :descending, relationship: nil}

In a dot-separated sequence of names, the final name is the attribute name and all preceding names are a relationship path in the same format as Alembic.RelationshipPath

iex> Alembic.Fetch.Sort.from_string("author.name")
%Alembic.Fetch.Sort{attribute: "name", direction: :ascending, relationship: "author"}
iex> Alembic.Fetch.Sort.from_string("comments.author.posts.inserted-at")
%Alembic.Fetch.Sort{
  attribute: "inserted-at",
  direction: :ascending,
  relationship: %{
    "comments" => %{
      "author" => "posts"
    }
  }
}
to_string(sort)
to_string(t) :: String.t

Converts a sort back to the string format parsed by from_string/1

A t with nil relationship and the default direction of :ascending is only the attribute

iex> Alembic.Fetch.Sort.to_string(
...>   %Alembic.Fetch.Sort{attribute: "inserted-at", relationship: nil}
...> )
"inserted-at"

A t with direction of :descending will have a "-" prefix

iex> Alembic.Fetch.Sort.to_string(
...>   %Alembic.Fetch.Sort{attribute: "inserted-at", direction: :descending, relationship: nil}
...> )
"-inserted-at"

When there is a relationship, it is converted to a path in front of the attribute name, but after the direction prefix

iex> Alembic.Fetch.Sort.to_string(
...>   %Alembic.Fetch.Sort{attribute: "inserted-at", direction: :descending, relationship: "comments"}
...> )
"-comments.inserted-at"

Farther relationships are dot-separated

iex> Alembic.Fetch.Sort.to_string(
...>   %Alembic.Fetch.Sort{
...>     attribute: "inserted-at",
...>     direction: :descending,
...>     relationship: %{
...>       "comments" => %{
...>         "author" => "posts"
...>       }
...>     }
...>   }
...> )
"-comments.author.posts.inserted-at"