How to implement custom pagination
This recipe demonstrates how to implement cursor-based pagination using custom pagination utilities in Rell and consume it from a client. This approach provides full control over pagination logic and is ideal for projects that need customized pagination behavior.
- JS/TS client
Prerequisites
- Chromia CLI
- Docker (for local blockchain)
- Node.js
- FT4 client
- Postchain client
Demo script
Key Features
This recipe includes examples for:
- Custom pagination utilities - Custom-built pagination logic with full control over cursor behavior
- Offset-based pagination - Uses offset and limit for data retrieval with page tracking
- Custom cursor encoding - Base64-encoded cursors with page number tracking
- Flexible page sizing - Configurable page sizes with maximum limits
Architecture Overview
The recipe includes both server and client components:
Rell Module (Server-side)
- Entity:
userwithnamefield for demonstration - Query:
get_users_paginatedwithpage_cursoranddata_sizeparameters returning custompaged_result - Operation:
add_userfor seeding test data - Custom utilities:
paginator.rellmodule with pagination helper functions
JavaScript Client
- First page request - Fetches initial data with
page_cursorset to null - Subsequent pages - Uses
next_cursorfrom previous response - Page size control - Uses
data_sizeparameter for configurable page sizes
Custom Pagination Pattern
The Rell implementation uses custom pagination utilities:
Pagination Structures
- page_cursor: Tracks current page position with
after_rowidandafter_page - pagination_result: Contains data and rowid for each result item
- paged_result: Standard response format with
dataandnext_cursor
Cursor Management
- encode_cursor: Converts cursor to base64-encoded string
- decode_cursor: Parses cursor from client requests
- Page tracking: Maintains current page number for offset calculations
Query Processing
- fetch_data_size: Fetches one extra item to determine if more pages exist
- query_offset_for_page_size: Calculates offset based on current page
- make_page: Constructs response with appropriate next cursor
Client Usage Pattern
The JavaScript client demonstrates the pagination flow:
- Initial request sets
page_cursorto null and specifiesdata_size - Each response includes
dataarray and optionalnext_cursor - Subsequent requests use the
next_cursorto fetch following pages - Pagination continues until
next_cursoris null
Customization Benefits
This custom approach offers several advantages:
- Full control - Complete control over pagination logic and cursor format
- Flexible implementation - Easy to modify pagination behavior for specific needs
- Performance optimization - Can implement custom indexing and query optimizations
- Custom metadata - Can include additional pagination metadata in responses
Use Cases
This custom pagination approach is ideal for:
- Custom sorting requirements - Complex sorting that doesn't fit standard patterns
- Specialized cursor logic - Need for custom cursor encoding or metadata
- Performance optimization - Projects requiring specific query optimization strategies
- Legacy integration - Matching existing API pagination formats
Learn more
- Rell language — understanding how to create custom queries and utilities in Rell.
- JavaScript/TypeScript client — covers client setup and usage.