Skip to content

Commit

Permalink
Merge pull request #64 from sliit-foss/master
Browse files Browse the repository at this point in the history
Fixes the sorting issue (#63) and failing test cases
  • Loading branch information
aravindnc authored Jul 23, 2024
2 parents d4ecf92 + 6faedec commit ea7a9d1
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 18 deletions.
28 changes: 15 additions & 13 deletions lib/mongoose-aggregate-paginate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* @returns {Promise}
*/

const { parseSort } = require("./util");

const defaultOptions = {
customLabels: {
totalDocs: "totalDocs",
Expand Down Expand Up @@ -86,6 +88,7 @@ function aggregatePaginate(query, options, callback) {
}

const sort = options.sort;

const allowDiskUse = options.allowDiskUse || false;
const isPaginationEnabled = options.pagination === false ? false : true;

Expand All @@ -98,7 +101,7 @@ function aggregatePaginate(query, options, callback) {
}

if (sort) {
pipeline.push({ $sort: sort });
pipeline.push({ $sort: parseSort(sort) });
}

function constructPipelines() {
Expand Down Expand Up @@ -142,10 +145,19 @@ function aggregatePaginate(query, options, callback) {
} else {
const [pipeline, countPipeline] = constructPipelines();

const countQuery = options.countQuery
let countQuery = options.countQuery
? options.countQuery
: this.aggregate(countPipeline);

if (options.countQuery) {
countQuery = countQuery.group({
_id: null,
count: {
$sum: 1,
},
});
}

if (allowDiskUse) {
countQuery.allowDiskUse(true);
}
Expand All @@ -154,17 +166,7 @@ function aggregatePaginate(query, options, callback) {

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

promise = Promise.all([
q.exec(),
countQuery
.group({
_id: null,
count: {
$sum: 1,
},
})
.exec(),
]);
promise = Promise.all([q.exec(), countQuery.exec()]);
}

return promise
Expand Down
3 changes: 3 additions & 0 deletions lib/util/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
...require("./sort"),
};
35 changes: 35 additions & 0 deletions lib/util/sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function convertSortStringToObject(str) {
const sortObject = {};
str.split(" ").forEach((field) => {
if (field.startsWith("-")) {
sortObject[field.substring(1)] = -1;
} else {
sortObject[field] = 1;
}
});
return sortObject;
}

function convertSortArrayToObject(arr) {
const sortObject = {};
arr.forEach(([field, direction]) => {
sortObject[field] = direction === "asc" || direction === 1 ? 1 : -1;
});
return sortObject;
}

function parseSort(sort) {
if (typeof sort === "string") {
return convertSortStringToObject(sort);
}
if (Array.isArray(sort)) {
return convertSortArrayToObject(sort);
}
const sortObject = {};
for (const [field, direction] of Object.entries(sort)) {
sortObject[field] = direction === "asc" || direction === 1 ? 1 : -1;
}
return sortObject;
}

exports.parseSort = parseSort;
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.1",
"version": "1.1.2",
"description": "A cursor based custom aggregate pagination library for Mongoose with customizable labels.",
"main": "index.js",
"types": "types/index.d.ts",
Expand Down
76 changes: 74 additions & 2 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,11 @@ describe("mongoose-paginate", function () {
};
return Book.aggregatePaginate(aggregate, options).then((result) => {
expect(result.docs).to.have.length(10);
expect(result.docs[0].title).to.equal("Book #41");
expect(result.docs[0].title).to.equal("Book #11");
expect(result.totalDocs).to.equal(100);
expect(result.limit).to.equal(10);
expect(result.page).to.equal(2);
expect(result.pagingCounter).to.equal(41);
expect(result.pagingCounter).to.equal(11);
expect(result.hasPrevPage).to.equal(true);
expect(result.hasNextPage).to.equal(true);
expect(result.prevPage).to.equal(1);
Expand All @@ -308,6 +308,78 @@ describe("mongoose-paginate", function () {
});
});

describe("sorting", function () {
var aggregate = Book.aggregate([
{
$match: {
title: {
$in: [/Book/i],
},
},
},
]);
it("with object ascending", function () {
return Book.aggregatePaginate(aggregate, {
sort: { date: "asc" },
limit: 40,
}).then((result) => {
expect(result.docs).to.have.length(40);
expect(result.docs[0].title).to.equal("Book #1");
expect(result.docs[result.docs.length - 1].title).to.equal("Book #40");
});
});
it("with object descending", function () {
return Book.aggregatePaginate(aggregate, {
sort: { date: -1 },
limit: 50,
}).then((result) => {
expect(result.docs).to.have.length(50);
expect(result.docs[0].title).to.equal("Book #100");
expect(result.docs[result.docs.length - 1].title).to.equal("Book #51");
});
});
it("with string ascending", function () {
return Book.aggregatePaginate(aggregate, {
sort: "date",
limit: 10,
}).then((result) => {
expect(result.docs).to.have.length(10);
expect(result.docs[0].title).to.equal("Book #1");
expect(result.docs[result.docs.length - 1].title).to.equal("Book #10");
});
});
it("with string descending", function () {
return Book.aggregatePaginate(aggregate, {
sort: "-date",
limit: 10,
}).then((result) => {
expect(result.docs).to.have.length(10);
expect(result.docs[0].title).to.equal("Book #100");
expect(result.docs[result.docs.length - 1].title).to.equal("Book #91");
});
});
it("with array ascending", function () {
return Book.aggregatePaginate(aggregate, {
sort: [["date", "asc"]],
limit: 10,
}).then((result) => {
expect(result.docs).to.have.length(10);
expect(result.docs[0].title).to.equal("Book #1");
expect(result.docs[result.docs.length - 1].title).to.equal("Book #10");
});
});
it("with array descending", function () {
return Book.aggregatePaginate(aggregate, {
sort: [["date", "desc"]],
limit: 10,
}).then((result) => {
expect(result.docs).to.have.length(10);
expect(result.docs[0].title).to.equal("Book #100");
expect(result.docs[result.docs.length - 1].title).to.equal("Book #91");
});
});
});

after(async function () {
await mongoose.connection.db.dropDatabase();
});
Expand Down

0 comments on commit ea7a9d1

Please sign in to comment.