Skip to main content
Pagination allows you to split large datasets into manageable chunks, improving performance and user experience. The NestJS CRUD framework supports multiple pagination strategies.

Pagination Methods

There are two primary ways to paginate results:
  1. Limit/Offset: Specify the number of items and how many to skip
  2. Limit/Page: Specify the number of items per page and the page number

Limit Parameter

The limit parameter controls how many results to return:
This returns the first 10 users.

Aliases

The limit parameter has an alias:
  • limit
  • per_page

Examples

Without a limit parameter, the API may return all results or use a server-configured default limit.

Offset-Based Pagination

Offset-based pagination uses limit and offset parameters:
  • limit: Number of items to return
  • offset: Number of items to skip

Syntax

Examples

Calculation

To calculate the offset for a specific page:

Use Cases

  • When you need precise control over which items to retrieve
  • When implementing custom pagination logic
  • When you want to skip to specific positions in the dataset

Page-Based Pagination

Page-based pagination uses limit and page parameters:
  • limit: Number of items per page
  • page: Page number (1-indexed)

Syntax

Examples

Page numbers start at 1, not 0. page=1 is the first page.

Use Cases

  • When building traditional paginated UIs with page numbers
  • When implementing “Previous” and “Next” navigation
  • When the user needs to jump to a specific page

Offset vs Page

Cannot Use Both

You cannot use offset and page together. Choose one pagination method.

Pagination with Sorting

Always use the same sort order across paginated requests:
Inconsistent sorting across paginated requests can cause items to appear on multiple pages or be skipped entirely.

Best Practice

Pagination with Filtering

Combine pagination with filters to paginate through filtered results:

Important Considerations

  1. Keep filters consistent: Use the same filters across all pages
  2. Total count may change: If data is added/removed, total pages may change
  3. Filter before paginating: Pagination applies to filtered results

Complete Pagination Examples

Basic Pagination

With Sorting

With Filtering and Sorting

With Field Selection

Implementing Pagination in Frontend

React Example

JavaScript with RequestQueryBuilder

Infinite Scroll

For infinite scroll UIs, use offset-based pagination:

Cursor-Based Pagination

For datasets that frequently change, consider cursor-based pagination using filters:
This approach is more stable when data is being added or removed.

Performance Considerations

Always Use Limits

Avoid Large Offsets

Large offsets can be slow because the database still has to scan through skipped rows.

Index Sorted Fields

Always create database indexes on fields used for sorting in pagination:

Common Pagination Patterns

Standard Table Pagination

Mobile Feed

Search Results

Error Handling

Invalid pagination parameters will throw a RequestQueryException:

Best Practices

  1. Always set a limit: Never rely on default limits
  2. Use consistent sorting: Keep the same sort order across pages
  3. Choose the right method: Use page for UI, offset for precise control
  4. Consider cursor pagination: For frequently changing datasets
  5. Index sorted fields: Add database indexes to improve performance
  6. Document limits: Let API consumers know the maximum allowed limit
  7. Return metadata: Include total count, total pages, current page in responses
  8. Handle edge cases: Validate page numbers and offsets

Response Metadata

Consider including pagination metadata in your API responses:
This helps clients build better pagination UIs.

Next Steps

Sorting

Learn how to sort paginated results

Filtering

Combine pagination with filters

Relations

Paginate results with joined relations

Query Parameters

Overview of all query parameters