Skip navigation

bafonins.xyz
↵ All articles

Supabase pagination via REST API

Table of contents

Introduction

Supabase is a powerful tool that offers everything developers need to build apps quickly, including managing databases, user sign-ins, and real-time updates. A key feature for those dealing with a lot of data is pagination, which helps break down data into smaller, easier-to-handle pieces. This article explains how to use Supabase’s REST API for pagination, making it simpler to work with large datasets.

Data Pagination

Pagination is crucial in app development, especially with big datasets. It splits the data into pages, so users only load a small amount at a time. This makes apps faster and easier to use, improving the overall experience.

Offset-based pagination

Offset-based pagination is a common method used in web and application development to manage and navigate through large sets of data by dividing the data into smaller, manageable chunks or pages. This technique operates on the principle of skipping a certain number of records (the offset) and then returning a defined number of records (the limit) from the dataset.

Cursor-based pagination

Cursor-based pagination works by returning a pointer (the cursor) along with the data fetched for a page. This cursor points to the position in the dataset immediately after the last record of the current page. When the next set of data is requested, the query uses this cursor as a reference point to fetch records following it. Cursors can be based on record IDs, timestamps, or any other field that guarantees a stable sort order.

Supabase REST API

The Supabase platform extends its robust backend services with a powerful REST API, facilitated by PostgREST. PostgREST is an open-source tool that turns a PostgreSQL database directly into a RESTful API (available on the /rest/v1/{table-name} route), allowing for seamless interaction with your database using simple HTTP requests. This integration offers developers a straightforward way to execute queries, including complex operations like filtering, sorting, and, crucially, pagination, directly against their Supabase databases.

Using HTTP Headers for offset-based Pagination

Using HTTP headers for pagination involves setting specific header fields to guide data segmentation:

Request
  GET /rest/v1/{table_name}
  apikey ...supabase access key...
  Range 10-19
  Prefer count=exact
Response
  Content-Range: 10-19/100

Using Query Parameters for offset-based Pagination

Traditional query parameter-based pagination is the simplest way to acquire paginated data via REST API. When making a request to a PostgREST endpoint, simply append the offset and limit parameters to the URL to paginate the results. Here is a basic example of how to structure such a request:

Request
  GET /rest/v1/{table-name}
    ?offset=10
    &limit=10
  apikey ...supabase access key...
  Prefer count=exact
Response
  Content-Range: 10-19/100

Using Query Parameters for cursor-based Pagination

While Supabase does not directly offer built-in support for cursor-based pagination, you can effectively achieve it by leveraging query parameters. This approach involves using a specific column in your table as a cursor, combined with PostgREST operators, to simulate cursor-based navigation through your data.

Request
  GET /rest/v1/{table-name}
    ?id=gt.10
    &order=id.asc
    &limit=10
  apikey ...supabase access key...
  Prefer count=exact
Response
  Content-Range: 0-9/100

This example assumes that the id field is unique and auto-incremented by the database, making it a suitable candidate for cursor-based pagination. The same pagination logic can be applied when using a created_at timestamp or any other sequential and unique column.

Note: In this setup, the Content-Range header might not accurately reflect the current range of records fetched; however, the total count of records remains correctly indicated.