Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 2x 2x 2x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* Cursor-based Pagination Utilities
*
* Provides efficient cursor-based pagination for large datasets,
* which is more performant than offset-based pagination for large tables.
*/
import { prisma } from '@/lib/prisma';
import { Prisma } from '@prisma/client';
export interface CursorPaginationParams {
cursor?: number;
limit: number;
direction?: 'forward' | 'backward';
}
export interface CursorPaginationResult<T> {
items: T[];
nextCursor: number | null;
prevCursor: number | null;
hasMore: boolean;
hasPrev: boolean;
}
/**
* Generic cursor pagination helper for Prisma queries
*
* @example
* const result = await cursorPaginate(
* prisma.product,
* { cursor: 100, limit: 20 },
* { where: { status: 'ACTIVE' } },
* { createdAt: 'desc' }
* );
*/
export async function cursorPaginate<
T extends { id: number },
WhereInput,
OrderByInput,
>(
model: {
findMany: (args: {
where?: WhereInput;
orderBy?: OrderByInput;
cursor?: { id: number };
skip?: number;
take: number;
}) => Promise<T[]>;
},
params: CursorPaginationParams,
where?: WhereInput,
orderBy?: OrderByInput,
include?: unknown
): Promise<CursorPaginationResult<T>> {
const { cursor, limit, direction = 'forward' } = params;
// Fetch one extra item to determine if there are more results
const take = direction === 'forward' ? limit + 1 : -(limit + 1);
// Build query args object
const queryArgs: {
where?: WhereInput;
orderBy?: OrderByInput;
take: number;
cursor?: { id: number };
skip?: number;
include?: unknown;
} = {
where,
orderBy,
take};
if (cursor) {
queryArgs.cursor = { id: cursor };
queryArgs.skip = 1; // Skip the cursor itself
}
if (include) {
queryArgs.include = include;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const items = await model.findMany(queryArgs as any);
// Determine if there are more results
const hasMore = direction === 'forward'
? items.length > limit
: items.length > limit;
// Remove the extra item used for hasMore check
const trimmedItems = hasMore
? (direction === 'forward' ? items.slice(0, -1) : items.slice(1))
: items;
// For backward pagination, reverse the order
const finalItems = direction === 'backward'
? trimmedItems.reverse()
: trimmedItems;
// Calculate cursors
const nextCursor = finalItems.length > 0 && hasMore
? finalItems[finalItems.length - 1].id
: null;
const prevCursor = cursor && finalItems.length > 0
? finalItems[0].id
: null;
return {
items: finalItems,
nextCursor,
prevCursor,
hasMore: direction === 'forward' ? hasMore : !!cursor,
hasPrev: direction === 'forward' ? !!cursor : hasMore};
}
/**
* Specialized cursor pagination for products
*/
export async function paginateProducts(
params: CursorPaginationParams,
filters?: {
categoryId?: number;
minPrice?: number;
maxPrice?: number;
search?: string;
}
): Promise<CursorPaginationResult<{
id: number;
title: string;
price: number;
discountedPrice: number;
category: { id: number; title: string };
}>> {
const where: Prisma.ProductWhereInput = {};
if (filters?.categoryId) {
where.categoryId = filters.categoryId;
}
if (filters?.minPrice !== undefined || filters?.maxPrice !== undefined) {
where.discountedPrice = {};
if (filters.minPrice !== undefined) {
where.discountedPrice.gte = filters.minPrice;
}
if (filters.maxPrice !== undefined) {
where.discountedPrice.lte = filters.maxPrice;
}
}
if (filters?.search) {
where.OR = [
{ title: { contains: filters.search } },
{ description: { contains: filters.search } },
];
}
return cursorPaginate(
prisma.product as unknown as {
findMany: (args: {
where?: Prisma.ProductWhereInput;
orderBy?: Prisma.ProductOrderByWithRelationInput;
cursor?: { id: number };
skip?: number;
take: number;
}) => Promise<{ id: number; title: string; price: number; discountedPrice: number; category: { id: number; title: string } }[]>;
},
params,
where,
{ id: 'desc' } as Prisma.ProductOrderByWithRelationInput,
{ category: { select: { id: true, title: true } } }
);
}
/**
* Specialized cursor pagination for reviews
*/
export async function paginateReviews(
productId: number,
params: CursorPaginationParams
): Promise<CursorPaginationResult<{
id: number;
rating: number;
comment: string | null;
createdAt: Date;
user: { id: number; name: string | null };
}>> {
return cursorPaginate(
prisma.review as unknown as {
findMany: (args: {
where?: Prisma.ReviewWhereInput;
orderBy?: Prisma.ReviewOrderByWithRelationInput;
cursor?: { id: number };
skip?: number;
take: number;
}) => Promise<{ id: number; rating: number; comment: string | null; createdAt: Date; user: { id: number; name: string | null } }[]>;
},
params,
{ productId } as Prisma.ReviewWhereInput,
{ createdAt: 'desc' } as Prisma.ReviewOrderByWithRelationInput,
{ user: { select: { id: true, name: true } } }
);
}
|