Implementing pagination.

This commit is contained in:
mike12345567 2021-06-17 15:56:41 +01:00
parent 1014260ad5
commit 708df56545
1 changed files with 23 additions and 6 deletions

View File

@ -193,16 +193,21 @@ exports.search = async ctx => {
const appId = ctx.appId const appId = ctx.appId
const tableId = ctx.params.tableId const tableId = ctx.params.tableId
const { paginate, query, ...params } = ctx.request.body const { paginate, query, ...params } = ctx.request.body
let { bookmark, limit } = params
if (!bookmark && paginate) {
bookmark = 1
}
let paginateObj = {} let paginateObj = {}
if (paginate) { if (paginate) {
paginateObj = { paginateObj = {
limit: params.limit, // add one so we can track if there is another page
// todo: need to handle bookmarks limit: limit,
page: params.bookmark, page: bookmark,
} }
} else if (params && params.limit) { } else if (params && limit) {
paginateObj = { paginateObj = {
limit: params.limit, limit: limit,
} }
} }
let sort let sort
@ -220,8 +225,20 @@ exports.search = async ctx => {
sort, sort,
paginate: paginateObj, paginate: paginateObj,
}) })
let hasNextPage = false
if (paginate && rows.length === limit) {
const nextRows = await handleRequest(appId, DataSourceOperation.READ, tableId, {
filters: query,
sort,
paginate: {
limit: 1,
page: bookmark + 1,
}
})
hasNextPage = nextRows.length > 0
}
// need wrapper object for bookmarks etc when paginating // need wrapper object for bookmarks etc when paginating
return { rows } return { rows, hasNextPage, bookmark: bookmark + 1 }
} }
exports.validate = async () => { exports.validate = async () => {