struct

PgORM::CursorPaginatedResult(T)

Inherits Struct < Value < Object

Result wrapper for cursor-based pagination.

Cursor pagination is more efficient than offset pagination for large datasets because it doesn't require counting all records or skipping rows. Instead, it uses the primary key (or another column) as a cursor to fetch the next or previous page.

Advantages over Offset Pagination

  • Performance: No OFFSET clause, which gets slower with large offsets
  • Consistency: New records don't shift pages during pagination
  • Scalability: Works well with millions of records

Limitations

  • Can't jump to arbitrary pages (only next/previous)
  • No total count or page numbers
  • Requires a sortable cursor column (usually primary key)

Example

# First page
result = Article.order(:id).paginate_cursor(limit: 20)
result.records.each { |article| puts article.title }

# Next page (using cursor from previous result)
if result.has_next?
  next_result = Article.order(:id).paginate_cursor(
    after: result.next_cursor,
    limit: 20
  )
end

# Previous page
if result.has_prev?
  prev_result = Article.order(:id).paginate_cursor(
    before: result.prev_cursor,
    limit: 20
  )
end

Constructors

new(records_array : Array(T), limit : Int32, next_cursor : String | Nil = nil, prev_cursor : String | Nil = nil)
Source

Instance methods

each

Iterate over records

Source
has_next?

Whether there is a next page

Source
has_prev?

Whether there is a previous page

Source
limit
Source
next_cursor
Source
prev_cursor
Source
records

Access records (already loaded for cursor determination)

Source
to_json(json : JSON::Builder)

Convert to JSON with cursor pagination metadata

Source