struct PgORM::PaginatedResult(T)

Overview

Result wrapper for offset-based pagination containing records and metadata.

Records are loaded lazily - the database query doesn't execute until you access the records. This allows you to pass pagination results around without loading data unnecessarily.

Metadata Available

Example

result = User.where(active: true).paginate(page: 2, limit: 20)

result.total       # => 150
result.page        # => 2
result.total_pages # => 8
result.has_next?   # => true
result.from        # => 21
result.to          # => 40

# Records loaded only when accessed
result.records.each do |user|
  puts user.name
end

# Or iterate directly
result.each do |user|
  puts user.name
end

Defined in:

pg-orm/pagination.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(query : Collection(T) | Relation(T), total : Int64, limit : Int32, offset : Int32) #

[View source]

Instance Method Detail

def each(&block : T -> ) #

Iterate over records without loading all into memory at once This allows streaming/processing records one at a time


[View source]
def from : Int32 #

Starting record number (1-indexed)


[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_page : Int32 | Nil #

Next page number (nil if no next page)


[View source]
def offset : Int32 #

[View source]
def page : Int32 #

[View source]
def prev_page : Int32 | Nil #

Previous page number (nil if no previous page)


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

Lazily load records only when accessed Records are cached after first access to avoid multiple DB queries


[View source]
def to : Int32 #

Ending record number (1-indexed)


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

Convert to JSON with pagination metadata Note: This will load all records into memory for serialization


[View source]
def total : Int64 #

[View source]
def total_pages : Int32 #

Total number of pages


[View source]