mm-ninja-turtles / turtle-express

My attempt of making Express.JS a typesafe router with Zod.
3 stars 0 forks source link

[feature]: pagination helper function #39

Closed wai-lin closed 2 years ago

wai-lin commented 2 years ago

generating pagination can be simple but a quite tedious job. what if turtle-express expose a helper function that will accept totalCount, limit, page as arguments and calculate the totalPage, prevPage, nextPage, currentPage for the api consumers and also for the backend it could provide the prevOffset, nextOffset, currentOffset, lastOffset and so on.

const result = await db.findAndCount({ take: 10, skip: 0 })

const pagination = getPagination({
  totalCount: result.count,
  limit: 10,
  page: 1,
})

console.log(pagination)
// totalPage: 10
// prevPage: 1
// nextPage: 3
// currentPage: 2
// prevOffset: 0
// nextOffset: 21
// currentOffset: 10
// lastOffset: 41