This guide demonstrates how to implement limit/offset pagination in Drizzle:
index.ts
schema.ts
Limit is the number of rows to return (page size) and offset is the number of rows to skip ((page number - 1) * page size).
For consistent pagination, ensure ordering by a unique column. Otherwise, the results can be inconsistent.
If you need to order by a non-unique column, you should also append a unique column to the ordering.
This is how you can implement limit/offset pagination with 2 columns:
Drizzle has useful relational queries API, that lets you easily implement limit/offset pagination:
Drizzle has simple and flexible API, which lets you easily create custom solutions. This is how you can create custom function for pagination using .$dynamic() function:
You can improve performance of limit/offset pagination by using deferred join technique. This method performs the pagination on a subset of the data instead of the entire table.
To implement it you can do like this:
Benefits of limit/offset pagination: it’s simple to implement and pages are easily reachable, which means that you can navigate to any page without having to save the state of the previous pages.
Drawbacks of limit/offset pagination: degradation in query performance with increasing offset because database has to scan all rows before the offset to skip them, and inconsistency due to data shifts, which can lead to the same row being returned on different pages or rows being skipped.
This is how it works:
So, if your database experiences frequently insert and delete operations in real time or you need high performance to paginate large tables, you should consider using cursor-based pagination instead.