How to implement pagination with FT4
This recipe demonstrates how to implement cursor-based pagination using FT4 utilities in Rell and consume it from a
client. This approach is ideal for projects already using FT4, as it leverages pagination helpers from lib.ft4.utils
for consistent cursor handling.
- 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:
- Cursor-based pagination - Using FT4's cursor system for efficient data traversal
- Rell implementation - Server-side pagination logic using
lib.ft4.utils
- Client consumption - JavaScript client that handles pagination requests
- Consistent pagination - Following FT4 patterns for standardized pagination across your dapp
Architecture Overview
The recipe includes both server and client components:
Rell Module (Server-side)
- Entity:
user
withname
field for demonstration - Query:
get_users_paginated
withpage_size
andpage_cursor
parameters returning FT4-style result withdata
andnext_cursor
- Operation:
add_user
for seeding test data
JavaScript Client
- First page request - Fetches initial data with
page_cursor
set to null - Subsequent pages - Uses
next_cursor
from previous response - Page size control - Configurable page size per request
FT4 Pagination Pattern
The Rell implementation uses FT4 utilities for consistent pagination:
- Uses
ft4_utils.before_rowid
to handle cursor positioning - Employs
ft4_utils.pagination_result
for data formatting - Utilizes
ft4_utils.fetch_data_size
for proper page sizing - Returns results via
ft4_utils.make_page
for standardized response format
Client Usage Pattern
The JavaScript client demonstrates the standard pagination flow:
- Initial request sets
page_cursor
to null for the first page - Subsequent requests use the
next_cursor
value from previous responses - Page size can be adjusted per request for flexible data loading
Use Cases
This pagination approach is ideal for:
- Large datasets - Efficiently browsing through extensive records
- User interfaces - Implementing paginated lists and tables
- API endpoints - Providing paginated data to frontend applications
- Performance optimization - Reducing memory usage and response times
Learn more
- FT4 client documentation — covers FT4 operations and utilities.
- Rell language — understanding how to create queries and operations in Rell.
- JavaScript/TypeScript client — covers client setup and usage.