-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add pagination in the stock_movement end-point #6960
Changes from all commits
f26c3e2
4c27665
d5e17e2
c65c68c
3f87b8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -289,6 +289,57 @@ class DatabaseConnector { | |
.catch(next) | ||
.done(); | ||
} | ||
|
||
async paginateQuery(sql, params, tables, filters) { | ||
let pager = {}; | ||
let rows = []; | ||
let fetchAllData = false; | ||
|
||
if (!params.limit) { | ||
params.limit = 100; | ||
} else if (params.limit && parseInt(params.limit, 10) === -1) { | ||
fetchAllData = true; | ||
delete params.limit; | ||
} | ||
|
||
if (params.page && parseInt(params.page, 10) === 0) { | ||
delete params.page; | ||
} | ||
|
||
const queryParameters = filters.parameters(); | ||
|
||
if (fetchAllData) { | ||
// fetch all data | ||
const query = filters.applyQuery(sql.concat(' ', tables)); | ||
rows = await this.exec(query, queryParameters); | ||
} else { | ||
// paginated data | ||
|
||
// FIXME: Performance issue, use SQL COUNT in a better way | ||
const total = (await this.exec(filters.getAllResultQuery(sql.concat(' ', tables)), queryParameters)).length; | ||
const page = params.page ? parseInt(params.page, 10) : 1; | ||
const limit = params.limit ? parseInt(params.limit, 10) : 100; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest computing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have to set |
||
const pageCount = Math.ceil(total / limit); | ||
pager = { | ||
total, | ||
page, | ||
page_size : limit, | ||
page_min : (page - 1) * limit, | ||
page_max : (page) * limit, | ||
page_count : pageCount, | ||
}; | ||
const paginatedQuery = filters.applyPaginationQuery(sql.concat(' ', tables), pager.page_size, pager.page_min); | ||
rows = await this.exec(paginatedQuery, queryParameters); | ||
if (rows.length === 0) { | ||
// update page_min and page_max after the query | ||
// in case of empty result | ||
pager.page_min = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
pager.page_max = null; | ||
} | ||
} | ||
|
||
return { rows, pager }; | ||
} | ||
} | ||
|
||
module.exports = new DatabaseConnector(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍