Skip to main content

Module page

Module page 

Source
Expand description

Keyset (cursor) pagination (data.orm, E3.3 “pagination”; ADR 0015 D11).

Repository::find_after and Repository::find_before read one page of rows in a Keyset order — the declared sort columns, then the primary key as the tie-breaker — and return a Paged page with the PageCursors of its neighbours. A page starts after (or ends before) the sort-key tuple of the row a cursor points at, so rows inserted or deleted elsewhere never shift a page the way an OFFSET does: the next page is the next rows. The offset Page (find_page) is kept for numbered pages.

The cursor (D11) is opaque to clients: base64url (no padding) of a small JSON document holding the sort-key tuple and a fingerprint of the request (the Keyset filter and sort, the entity’s table and the repository’s filter mode). It is at most MAX_CURSOR_BYTES long and PageCursor::decode reads it through a bounded Schema. It is not signed: every page query re-applies the repository’s tenant, soft-delete and custom filters, so a forged cursor only moves the page inside the caller’s own rows. A cursor whose fingerprint differs from the request’s is refused (PageCursorError::FilterMismatch), so a cursor taken under one filter cannot page another.

Page size (ADR 0013 R4): DEFAULT_PAGE_SIZE (50) unless the request asks for another, never more than the limit (MAX_PAGE_SIZE, 500, configurable through PageLimits). A larger request is capped, not refused.

Sort columns must be mapped columns of the entity (anything else is refused before a query runs) and should be NOT NULL: a row whose sort key is NULL cannot be a cursor position, so building the cursor of such a row fails the page with SqlError::Unsupported.

use id_effect::Exit;
use id_effect_sql::repository::{Keyset, PageCursor, SortOrder};
use id_effect_sql::{
  Dialect, Entity, FromRow, Repository, SqlClient, SqlClientEnv, SqlRow, SqlValue,
  TestSqlClient, sql_client_env,
};

#[derive(Clone, Debug, PartialEq, FromRow, Entity)]
#[entity(table = "posts")]
struct Post {
  #[entity(id)]
  id: i64,
  score: i64,
}

let test = TestSqlClient::new();
let _ = test.connect().block_on();
let post = |id: i64, score: i64| {
  SqlRow::from_pairs([("id", SqlValue::I64(id)), ("score", SqlValue::I64(score))])
};
test.script_query(
  r#"SELECT "id", "score" FROM "posts" ORDER BY "score" DESC, "id" ASC LIMIT $1"#,
  vec![post(4, 9), post(2, 7), post(3, 7)],
);

let keyset = Keyset::new().order_by("score", SortOrder::Desc).size(2);
let page = Repository::<Post>::new(Dialect::Postgres)
  .find_after::<SqlClientEnv, _>(&keyset, None)
  .provide_all(sql_client_env(test.clone()))
  .block_on();
let Exit::Success(page) = page else { panic!("{page:?}") };
assert_eq!(page.items.iter().map(|p| p.id).collect::<Vec<_>>(), [4, 2]);
assert!(page.prev.is_none(), "the first page has no previous page");

// The next page starts after (score 7, id 2): the token is what a client sends back.
let token = page.next.expect("a third row exists").encode();
let cursor = PageCursor::decode(&token).expect("a cursor this server issued");
assert_eq!(cursor.keys(), [SqlValue::I64(7), SqlValue::I64(2)]);

Structs§

Keyset
A keyset page request: the filter, the sort columns and the page size (Repository::find_after).
PageCursor
A position in a keyset order: the sort-key tuple of one row and the fingerprint of the request it was issued for (D11). encode is the opaque token clients carry; decode reads one back (bounded, Schema-checked).
PageLimits
The page-size bounds of a Keyset (ADR 0013 R4): the size used when none is requested and the largest page served. Both are at least 1; Default is DEFAULT_PAGE_SIZE and MAX_PAGE_SIZE.
Paged
One keyset page (Repository::find_after, Repository::find_before): the rows in the Keyset order and the cursors of the neighbouring pages.

Enums§

PageCursorError
Why a cursor token was refused.
SortOrder
The direction of one sort column.

Constants§

DEFAULT_PAGE_SIZE
The page size when a Keyset does not ask for one.
MAX_CURSOR_BYTES
The longest cursor token accepted or issued, in bytes (D11, ADR 0013 R4).
MAX_PAGE_SIZE
The default largest page (PageLimits::max_size); larger requests are capped to it.
MAX_SORT_KEYS
The most sort columns a Keyset (and so a cursor) has, the primary key included.