Skip to content

Commit

Permalink
fix pager.total value for multiple context
Browse files Browse the repository at this point in the history
  • Loading branch information
mbayopanda committed Mar 20, 2023
1 parent 708db35 commit 7c5c7d0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
15 changes: 13 additions & 2 deletions server/lib/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,19 @@ class DatabaseConnector {
rows = await this.exec(query, queryParameters);
} else {
// paginated data
const paginationLimits = filters.paginationLimitQuery(tables, params.limit, params.page);
[pager] = await this.exec(paginationLimits, queryParameters);

// 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;
pager = {
total,
page,
page_size : limit,
page_min : (page - 1) * limit,
page_max : (page) * limit,
page_count : Math.ceil(total / limit),
};
const paginatedQuery = filters.applyPaginationQuery(sql.concat(' ', tables), pager.page_size, pager.page_min);
rows = await this.exec(paginatedQuery, queryParameters);
}
Expand Down
21 changes: 19 additions & 2 deletions server/lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,31 @@ class FilterParser {

return `
SELECT
COUNT(*) AS total, ${page} AS page,
${limit} AS page_size, (${(page - 1) * limit}) AS page_min, (${(page) * limit}) AS page_max,
COUNT(*) AS total,
${page} AS page,
${limit} AS page_size,
(${(page - 1) * limit}) AS page_min,
(${(page) * limit}) AS page_max,
CEIL(COUNT(*) / ${limit}) AS page_count
${table}
WHERE ${conditionStatements}
`;
}

// FIXME: This strategie is temp solution to fix the pager.total compare to the rows.size
// The reason is we have to use COUNT(DISTINCT specific_column) FOR ALL OUR CASES in the above
// query
getAllResultQuery(sql) {
if (this._autoParseStatements) {
this._parseDefaultFilters();
}

const conditionStatements = this._parseStatements();
const group = this._group;

return `${sql} WHERE ${conditionStatements} ${group}`;
}

applyPaginationQuery(sql, limit, page) {
if (this._autoParseStatements) {
this._parseDefaultFilters();
Expand Down

0 comments on commit 7c5c7d0

Please sign in to comment.