Skip to content

Commit

Permalink
Merge pull request #15 from mrbontor/dev
Browse files Browse the repository at this point in the history
fix: correct pagination totalRecord calculation after applying filters
  • Loading branch information
mrbontor authored Oct 22, 2024
2 parents c162106 + 8afedda commit 4b3fb38
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 17 deletions.
22 changes: 22 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:
1. put your query or payload
2. Version [e.g. 1.1.1]

**Expected behavior**
A clear and concise description of what you expected to happen.

**Additional context**
Add any other context about the problem here.
20 changes: 20 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''

---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [1.3.0](https://github.com/mrbontor/mongo-pagination/compare/v1.2.0...v1.3.0) (2024-10-22)


### Features

* added validator to check mongodb opertaor ([35ed020](https://github.com/mrbontor/mongo-pagination/commit/35ed020b9b070e7eb84d05ccdf9e21d501c4c66b))
* create issue templates ([666335e](https://github.com/mrbontor/mongo-pagination/commit/666335ed5cdf02caa3bf642e0759633c5166c50f))


### Bug Fixes

* correct pagination totalRecord calculation after applying filters ([2454f8a](https://github.com/mrbontor/mongo-pagination/commit/2454f8a34ac280c0cc831eeca3fb9f0cb837e4cf))
* set default value while generating query ([7039407](https://github.com/mrbontor/mongo-pagination/commit/7039407513da33c40781a4b02f2b679bd7304a5e))

### [1.2.2](https://github.com/mrbontor/mongo-pagination/compare/v1.2.1...v1.2.2) (2024-10-14)


Expand Down
32 changes: 22 additions & 10 deletions libs/mongoPagination.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
const Config = require('../configs/index.json');
const { ValidateMongoQuery } = require('./validation');
const { GenerateQueryAggregation } = require('./generateAgregation');
const { BuildQueryMongoPagination, SetSorting } = require('./queryBuilder');
const { BuildQueryMongoPagination, SetSorting, BuildFilterQuery } = require('./queryBuilder');

/**
* Running query without functionality of Pagination
* @param {{ client: any, collection: string }} mongo
* @param {Array<any>} query
* @returns {Promise<any>}
*/
const runQuery = async ({ client, collection }, query) => {
const runQuery = async ({ client, collection }, query, payloadFilter = []) => {
try {
const queryAggregate = [
{
$facet: {
data: query,
count: [{ $group: { _id: 1, totalRecord: { $sum: 1 } } }]
if (!payloadFilter?.length) {
const queryAggregate = [
{
$facet: {
data: query,
count: [{ $group: { _id: 1, totalRecord: { $sum: 1 } } }]
}
}
];
return await client.collection(collection).aggregate(queryAggregate).toArray();
}
//re-generate query filter to count documet
const queryFilter = BuildFilterQuery(payloadFilter);
const totalRecord = await client.collection(collection).countDocuments(queryFilter);
const result = await client.collection(collection).aggregate(query).toArray();

return [
{
data: result,
count: [{ totalRecord }]
}
];
const result = await client.collection(collection).aggregate(queryAggregate).toArray();
return result;
} catch (error) {
throw error;
}
Expand Down Expand Up @@ -96,7 +108,7 @@ const buildPagination = async (mongoConfig, payload, fieldToSearch, projection,

const queryAgregate = GenerateQueryAggregation(aggregate);
const queryMongo = BuildQueryMongoPagination(newPayload, newFieldToSearch, newProjection, queryAgregate);
const results = await runQuery(mongoConfig, queryMongo);
const results = await runQuery(mongoConfig, queryMongo, payload?.filter);

delete newPayload.search;
return {
Expand Down
9 changes: 5 additions & 4 deletions libs/queryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ const buildQueryMongoPagination = (payload, fieldToSearch = [], projection = nul
Object.assign(query, buildFilterQuery(payload.filter));
}

// Susun baseQuery pipeline awal
// Set up the initial baseQuery pipeline
const baseQuery = [
{ $match: query },
...aggregate, // Masukkan agregat jika ada
...aggregate,
{ $sort: payload.sort || {} },
{ $skip: Math.max(0, (payload.page - 1) * payload.size) },
{ $limit: payload.size || 10 } // Default limit jika tidak diberikan
{ $limit: payload.size || 10 }
];

if (projection) {
Expand Down Expand Up @@ -210,5 +210,6 @@ module.exports = {
BuildQueryMongoPagination: buildQueryMongoPagination,
BuildProjectionSubQuery: buildProjectionSubQuery,
BuildSubQueryAndProjection: buildSubQueryAndProjection,
BuildSearchSubQuery: buildSearchSubQuery
BuildSearchSubQuery: buildSearchSubQuery,
BuildFilterQuery: buildFilterQuery
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongodb-pagination",
"version": "1.2.2",
"version": "1.3.0",
"description": "an advance Node.js library to handle pagination of MongoDb Native Driver",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 4b3fb38

Please sign in to comment.