Use Cursor Pagination
Why Cursor Pagination?
Basic Cursor Pagination
def fetch_posts(after : Int64?, limit : Int32 = 20)
query = Post.published.order(id: :desc).limit(limit + 1)
if after
query = query.where { id < after }
end
items = query.all
# Check if there are more items
has_more = items.size > limit
items = items.first(limit) if has_more
{
items: items,
next_cursor: has_more ? items.last?.try(&.id) : nil,
has_more: has_more
}
endUsage
Bidirectional Cursors
Timestamp-Based Cursor
Compound Cursor
API Response Format
Encode/Decode Cursors
Verify Cursor Pagination Works
Related
Last updated
Was this helpful?