Pagination issue fixed, total added (#227)

Fixes #217.
This commit is contained in:
Leo Giovanetti
2023-01-18 12:49:31 -07:00
committed by GitHub
parent 7aebdb8c96
commit 6cec8620cb
2 changed files with 6 additions and 3 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ const withPage = z.object({
export const withPagination: NextMiddleware = async (req, _, next) => {
const { page, take } = withPage.parse(req.query);
const skip = page * take || 0;
const skip = (page - 1) * take;
req.pagination = {
take,
skip,
+5 -2
View File
@@ -39,9 +39,12 @@ async function getHandler(req: NextApiRequest) {
const where: Prisma.UserWhereInput = {};
// If user is not ADMIN, return only his data.
if (!isAdmin) where.id = userId;
const data = await prisma.user.findMany({ where, take, skip });
const [total, data] = await prisma.$transaction([
prisma.user.count(),
prisma.user.findMany({ where, take, skip }),
]);
const users = schemaUsersReadPublic.parse(data);
return { users };
return { users, total };
}
export default withMiddleware("pagination")(defaultResponder(getHandler));