Skip to content

Commit

Permalink
PR Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
aravindnc committed Feb 18, 2025
1 parent 12050ec commit bd1ea9c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v1.1.4

[2025-02-17]

- Optimizes the aggregation pipeline generated while using facet by moving the shared pipeline stages above the facet stage which were earlier duplicated

## v1.1.3

[2025-01-23]
Expand Down
52 changes: 30 additions & 22 deletions lib/mongoose-aggregate-paginate.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,35 @@ function aggregatePaginate(query, options, callback) {
pipeline.push({ $sort: parseSort(sort) });
}

function constructPipelines() {
let promise;

if (options.useFacet && !options.countQuery) {
const prepaginationIndex = pipeline.findIndex(
(stage) => stage === PREPAGINATION_PLACEHOLDER
);
if (prepaginationIndex !== -1) {
promise = q
.append(pipeline.slice(0, prepaginationIndex))
.facet({
docs: [
...(isPaginationEnabled
? [{ $skip: skip }, { $limit: limit }]
: []),
...pipeline.slice(prepaginationIndex + 1),
],
count: [{ $count: "count" }],
})
.then(([{ docs, count }]) => [docs, count]);
} else {
promise = q
.append(pipeline)
.facet({
docs: isPaginationEnabled ? [{ $skip: skip }, { $limit: limit }] : [],
count: [{ $count: "count" }],
})
.then(([{ docs, count }]) => [docs, count]);
}
} else {
let cleanedPipeline = pipeline.filter(
(stage) => stage !== PREPAGINATION_PLACEHOLDER
);
Expand All @@ -124,26 +152,6 @@ function aggregatePaginate(query, options, callback) {
cleanedPipeline.push({ $skip: skip }, { $limit: limit });
}
}
return [cleanedPipeline, countPipeline];
}

let promise;
if (options.useFacet && !options.countQuery) {
let [pipeline, countPipeline] = constructPipelines();
const match = pipeline[0]?.$match;
if (match) {
pipeline.shift();
countPipeline.shift();
q = q.match(match);
}
promise = q
.facet({
docs: pipeline,
count: countPipeline,
})
.then(([{ docs, count }]) => [docs, count]);
} else {
const [pipeline, countPipeline] = constructPipelines();

let countQuery = options.countQuery
? options.countQuery
Expand All @@ -162,7 +170,7 @@ function aggregatePaginate(query, options, callback) {
countQuery.allowDiskUse(true);
}

const q = this.aggregate(pipeline);
const q = this.aggregate(cleanedPipeline);

if (query.options) q.options = query.options;

Expand Down
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": "mongoose-aggregate-paginate-v2",
"version": "1.1.3",
"version": "1.1.4",
"description": "A page based custom aggregate pagination library for Mongoose with customizable labels.",
"main": "index.js",
"types": "types/index.d.ts",
Expand Down

0 comments on commit bd1ea9c

Please sign in to comment.