Principle:APIJSON APIJSON Pagination
| Knowledge Sources | |
|---|---|
| Domains | ORM, Pagination, API_Design |
| Last Updated | 2026-10-11 00:00 GMT |
Overview
A result-limiting pattern that splits large query results into pages using count/page parameters with optional total count queries, generating dialect-specific LIMIT/OFFSET clauses and pagination metadata.
Description
Pagination in APIJSON is controlled by three parameters in the array ([]) config:
- count: Number of items per page (default from getDefaultQueryCount(), max from getMaxQueryCount())
- page: Zero-based page number (max from getMaxQueryPage())
- query: What to return — 0 (TABLE, data only), 1 (TOTAL, count only), 2 (ALL, data + count + pagination info)
The query parameter determines whether an additional count query (SELECT count(*)) is executed. When query=2 (ALL), the response includes:
- total: Total matching rows across all pages
- info: Pagination metadata (total, count, page, max, more, first, last)
The LIMIT/OFFSET syntax is dialect-specific:
- MySQL/PostgreSQL: LIMIT count OFFSET page*count
- SQL Server/Kingbase: OFFSET page*count ROWS FETCH NEXT count ROWS ONLY
- Oracle (pre-12): Subquery wrapping for rownum
The problem solved is preventing unbounded query results from overwhelming the server or client. Pagination enforces a maximum result size per request while providing metadata for navigating the full result set.
Usage
Use this principle when:
- An API returns potentially large result sets
- Clients need to navigate through pages of results
- Total count and pagination metadata are needed for UI rendering
Theoretical Basis
# Abstract algorithm (NOT real implementation):
def parse_array_query(request):
count = request.get("count", default_count)
page = request.get("page", 0)
query = request.get("query", "TABLE")
# Validate limits
if count < 0 or count > max_count:
raise ValueError("count out of range")
if page < 0 or page > max_page:
raise ValueError("page out of range")
offset = page * count
# Execute count query if requested
if query in ("TOTAL", "ALL"):
total = execute_count_query(main_config)
# Execute data query
if query != "TOTAL":
results = []
for i in range(count):
item = parse_object(request, position=i)
if item is None: break
results.append(item)
# Build pagination info
if query == "ALL":
info = {
"total": total,
"count": count,
"page": page + min_page,
"max": (total - 1) // count + min_page,
"more": page < max_page,
"first": page == min_page,
"last": page == max_page
}
return results, info