Reorderex

Build StatusCoverage Statushex.pm versionhex.pm downloads

An elixir helper library for database list reordering functionality.

Installation

The package can be installed by adding reorderex to your list of dependencies in mix.exs:

def deps do
  [
    {:reorderex, "~> 0.2.0-beta"}
  ]
end

You can optionally add Reorderex.Clock to make Reorderer.next_score become strictly monotonic.

children = [
  #...
  Reorderex.Clock
  #...
]

Usage with Ecto

Table-wise ordering

Schema

defmodule Photo do
  @moduledoc false
  use Ecto.Schema

  schema "photos" do
    field(:score, :binary, autogenerate: {Reorderex, :next_score, []})
  end
end

Move and entity to after an entity

import Reorderex.Ecto

photo
|> insert_after(photo1.id, Repo)
|> Repo.update()

Move and entity to the first

import Reorderex.Ecto

photo
|> insert_after(nil, Repo)
|> Repo.update()

Shared-table ordering

defmodule Photo do
  @moduledoc false
  use Ecto.Schema

  schema "photos" do
    field user_id, :binary
    field :score, :binary, autogenerate: {Reorderex, :next_score, []}
  end
end

import Reorderex.Ecto

query = Photo |> where([p], p.user_id = ^user_id)

photo
|> insert_after(photo1.id, Repo, query: query)
|> Repo.update()

The docs can be found at https://hexdocs.pm/reorderex.