923 B
923 B
Bidirectional Pagination
Support both forward and backward navigation in cursor-based pagination.
Pattern
async function getBidirectionalPosts(
cursor?: string,
direction: 'forward' | 'backward' = 'forward',
pageSize: number = 20
) {
const posts = await prisma.post.findMany({
take: direction === 'forward' ? pageSize : -pageSize,
skip: cursor ? 1 : 0,
cursor: cursor ? { id: cursor } : undefined,
orderBy: { id: 'asc' },
});
const data = direction === 'backward' ? posts.reverse() : posts;
return {
data,
nextCursor: data.length === pageSize ? data[data.length - 1].id : null,
prevCursor: data.length > 0 ? data[0].id : null,
};
}
Key Points
- Use negative
takevalue for backward pagination - Reverse results when paginating backward
- Return both
nextCursorandprevCursorfor navigation - Maintain consistent ordering across directions