struct
PgORM::PaginatedResult(T)
- PgORM::PaginatedResult(T)
- Struct
- Value
- Object
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
#total: Total number of records matching the query#limit: Number of records per page#offset: Number of records skipped#page: Current page number (1-indexed)#total_pages: Total number of pages#has_next?: Whether there's a next page#has_prev?: Whether there's a previous page#next_page: Next page number (or nil)#prev_page: Previous page number (or nil)#from: Starting record number (1-indexed)#to: Ending record number (1-indexed)
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.crConstructors
Instance Method Summary
-
#each(&block : T -> )
Iterate over records without loading all into memory at once This allows streaming/processing records one at a time
-
#from : Int32
Starting record number (1-indexed)
-
#has_next? : Bool
Whether there is a next page
-
#has_prev? : Bool
Whether there is a previous page
- #limit : Int32
-
#next_page : Int32 | Nil
Next page number (nil if no next page)
- #offset : Int32
- #page : Int32
-
#prev_page : Int32 | Nil
Previous page number (nil if no previous page)
-
#records : Array(T)
Lazily load records only when accessed Records are cached after first access to avoid multiple DB queries
-
#to : Int32
Ending record number (1-indexed)
-
#to_json(json : JSON::Builder)
Convert to JSON with pagination metadata Note: This will load all records into memory for serialization
- #total : Int64
-
#total_pages : Int32
Total number of pages
Constructor Detail
Instance Method Detail
Iterate over records without loading all into memory at once This allows streaming/processing records one at a time
Lazily load records only when accessed Records are cached after first access to avoid multiple DB queries
Convert to JSON with pagination metadata Note: This will load all records into memory for serialization