struct PgORM::CursorPaginatedResult(T)

Overview

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

Limitations

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

Defined in:

pg-orm/pagination.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(records_array : Array(T), limit : Int32, next_cursor : String | Nil = nil, prev_cursor : String | Nil = nil) #

[View source]

Instance Method Detail

def each(&block : T -> ) #

Iterate over records


[View source]
def has_next? : Bool #

Whether there is a next page


[View source]
def has_prev? : Bool #

Whether there is a previous page


[View source]
def limit : Int32 #

[View source]
def next_cursor : String | Nil #

[View source]
def prev_cursor : String | Nil #

[View source]
def records : Array(T) #

Access records (already loaded for cursor determination)


[View source]
def to_json(json : JSON::Builder) #

Convert to JSON with cursor pagination metadata


[View source]