Skip to content
Discussion options

You must be logged in to vote

The standard approach is to use $transaction to run both queries in parallel — one for the paginated items, one for the filtered total count — in a single round-trip:

const where = { /* your filter conditions */ };
const page = 1;
const pageSize = 20;

const [items, total] = await prisma.$transaction([
  prisma.post.findMany({
    where,
    skip: (page - 1) * pageSize,
    take: pageSize,
    orderBy: { createdAt: 'desc' },
  }),
  prisma.post.count({ where }),
]);

return {
  items,
  total,          // filtered total (your "second number")
  totalPages: Math.ceil(total / pageSize),
};

prisma.post.count({ where }) gives you exactly the filtered item count across all pages — not the tota…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@TokugawaTakeshi
Comment options

Answer selected by TokugawaTakeshi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants