AshHooks.Keyset (AshHooks v3.2.0)

Copy Markdown View Source

Shared keyset pagination over a stable (time, id) ordering: newest first, opaque cursors, and {:ok, %{records:, next_cursor:}} pages.

The cursor is the last row's {time, id} pair as base64url-encoded JSON. It is stable across engines because the page query filters by tuple order (time < cursor.time OR (time == cursor.time AND id < cursor.id) under the matching DESC, DESC sort), never by offset. id is the tiebreak for rows sharing one timestamp.

Summary

Types

A page result: the records and the cursor for the next page (nil on a shorter page; a full page carries a cursor even if it is the final nonempty page).

Functions

The page query's cursor predicate: everything strictly older than the cursor under the (time DESC, id DESC) ordering. read_page/5 skips this for the first page (no cursor).

Decodes a page cursor into %{time: DateTime.t(), id: term()}. nil (the first page) decodes to {:ok, nil}; anything malformed is the named {:error, :invalid_cursor}; attacker-shaped cursors never raise.

Encodes a row's (time_field, id) pair as the opaque page cursor.

The page size in force: the default when unset, a validation error outside 1..500.

Reads one page: sorts newest-first, applies the limit, and returns the page/1 result shape. time_field must exist on the resource (the caller's pre-flight checks it; :inserted_at on consumer ledgers is optional).

Types

page(records)

@type page(records) :: {:ok, %{records: records, next_cursor: binary() | nil}}

A page result: the records and the cursor for the next page (nil on a shorter page; a full page carries a cursor even if it is the final nonempty page).

Functions

cursor_filter(cursor, time_field)

@spec cursor_filter(%{time: DateTime.t(), id: term()}, atom()) :: Ash.Expr.t()

The page query's cursor predicate: everything strictly older than the cursor under the (time DESC, id DESC) ordering. read_page/5 skips this for the first page (no cursor).

decode_cursor(encoded)

@spec decode_cursor(binary() | nil) ::
  {:ok, nil | %{time: DateTime.t(), id: term()}} | {:error, :invalid_cursor}

Decodes a page cursor into %{time: DateTime.t(), id: term()}. nil (the first page) decodes to {:ok, nil}; anything malformed is the named {:error, :invalid_cursor}; attacker-shaped cursors never raise.

encode_cursor(record, time_field)

@spec encode_cursor(struct() | nil, atom()) :: binary() | nil

Encodes a row's (time_field, id) pair as the opaque page cursor.

fetch_limit(opts)

@spec fetch_limit(keyword()) :: {:ok, pos_integer()} | {:error, :invalid_limit}

The page size in force: the default when unset, a validation error outside 1..500.

read_page(resource, opts, time_field, filter_fun, tenant)

@spec read_page(module(), keyword(), atom(), (Ash.Query.t() -> Ash.Query.t()), term()) ::
  page([struct()]) | {:error, term()}

Reads one page: sorts newest-first, applies the limit, and returns the page/1 result shape. time_field must exist on the resource (the caller's pre-flight checks it; :inserted_at on consumer ledgers is optional).