Skip to content

Commit

Permalink
Extend paginate.ts to support more complex pagination object (#3970)
Browse files Browse the repository at this point in the history
* Extend paginate.ts to support more complex pagination object

* change pagination object based on reviewer suggestion

* changest inclusion

* update paginate test

Co-authored-by: Paul Moss <[email protected]>
  • Loading branch information
paulm17 and Paul Moss authored Nov 18, 2022
1 parent b493c93 commit 942536d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-hounds-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"blitz": major
---

update paginate.ts, return more params for more complex pagination control
7 changes: 7 additions & 0 deletions packages/blitz/src/paginate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,18 @@ export async function paginate<QueryResult>({

const hasMore = skip + take < count
const nextPage = hasMore ? {take, skip: skip + take} : null
const pageCount = Math.floor((count + take - 1) / take)
const from = skip + 1
const to = skip + take

return {
items,
nextPage,
hasMore,
pageCount: pageCount,
pageSize: take,
from,
to,
count,
}
}
8 changes: 8 additions & 0 deletions packages/blitz/tests/paginate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ describe("paginate", () => {
nextPage: null,
hasMore: false,
count: 3,
from: 2,
pageCount: 2,
pageSize: 2,
to: 3,
},
},
{
Expand All @@ -88,6 +92,10 @@ describe("paginate", () => {
nextPage: {skip: 3, take: 2},
hasMore: true,
count: 4,
from: 2,
pageCount: 2,
pageSize: 2,
to: 3,
},
},
]
Expand Down

0 comments on commit 942536d

Please sign in to comment.