Pagination
How to use cursor based pagination with database.
Offset pagination
VitNode doesn't support the offset
pagination. You can create your own
offset pagination.
Backend
Query (DTO)
Create a new file for your DTO. You need to extend the PaginationQuery
class to show first
, last
and cursor
fields.
Object (DTO)
Our second DTO will be for the return object. You need to extend the PaginationObj
class and add edges
field. The PaginationObj
will add total_count
, end_cursor
, count
, has_next_page
, has_previous_page
and start_cursor
fields.
Import DTO to Service
Now you need to import the DTO to your service.
Query to Database
In your service, you need to initialize the database using constructor()
from the DatabaseService
and use the paginationCursor
method to get data with cursor pagination. As an example, we will use the core_languages
table.
Advanced Usage
Where Arguments
If you want to use where
argument you can pass it to the findMany()
method using and()
method from drizzle-orm
.
Custom Query
If you want to use a custom query, you can pass it to the query
argument.
Where argument in query
Do not change where
argument in the query
method. It will couse an issue
with total_count
field.
Primary cursor
As default primaryCursor
is set to id
column. If you want to change it, you can pass the primaryCursor
argument.
Frontent integration
Frontend API
You can read how to implement Data Table with pagination API here.