Skip to content

Commit ea7a9d1

Browse files
authored
Merge pull request #64 from sliit-foss/master
Fixes the sorting issue (#63) and failing test cases
2 parents d4ecf92 + 6faedec commit ea7a9d1

File tree

6 files changed

+130
-18
lines changed

6 files changed

+130
-18
lines changed

lib/mongoose-aggregate-paginate.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* @returns {Promise}
77
*/
88

9+
const { parseSort } = require("./util");
10+
911
const defaultOptions = {
1012
customLabels: {
1113
totalDocs: "totalDocs",
@@ -86,6 +88,7 @@ function aggregatePaginate(query, options, callback) {
8688
}
8789

8890
const sort = options.sort;
91+
8992
const allowDiskUse = options.allowDiskUse || false;
9093
const isPaginationEnabled = options.pagination === false ? false : true;
9194

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

100103
if (sort) {
101-
pipeline.push({ $sort: sort });
104+
pipeline.push({ $sort: parseSort(sort) });
102105
}
103106

104107
function constructPipelines() {
@@ -142,10 +145,19 @@ function aggregatePaginate(query, options, callback) {
142145
} else {
143146
const [pipeline, countPipeline] = constructPipelines();
144147

145-
const countQuery = options.countQuery
148+
let countQuery = options.countQuery
146149
? options.countQuery
147150
: this.aggregate(countPipeline);
148151

152+
if (options.countQuery) {
153+
countQuery = countQuery.group({
154+
_id: null,
155+
count: {
156+
$sum: 1,
157+
},
158+
});
159+
}
160+
149161
if (allowDiskUse) {
150162
countQuery.allowDiskUse(true);
151163
}
@@ -154,17 +166,7 @@ function aggregatePaginate(query, options, callback) {
154166

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

157-
promise = Promise.all([
158-
q.exec(),
159-
countQuery
160-
.group({
161-
_id: null,
162-
count: {
163-
$sum: 1,
164-
},
165-
})
166-
.exec(),
167-
]);
169+
promise = Promise.all([q.exec(), countQuery.exec()]);
168170
}
169171

170172
return promise

lib/util/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
...require("./sort"),
3+
};

lib/util/sort.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function convertSortStringToObject(str) {
2+
const sortObject = {};
3+
str.split(" ").forEach((field) => {
4+
if (field.startsWith("-")) {
5+
sortObject[field.substring(1)] = -1;
6+
} else {
7+
sortObject[field] = 1;
8+
}
9+
});
10+
return sortObject;
11+
}
12+
13+
function convertSortArrayToObject(arr) {
14+
const sortObject = {};
15+
arr.forEach(([field, direction]) => {
16+
sortObject[field] = direction === "asc" || direction === 1 ? 1 : -1;
17+
});
18+
return sortObject;
19+
}
20+
21+
function parseSort(sort) {
22+
if (typeof sort === "string") {
23+
return convertSortStringToObject(sort);
24+
}
25+
if (Array.isArray(sort)) {
26+
return convertSortArrayToObject(sort);
27+
}
28+
const sortObject = {};
29+
for (const [field, direction] of Object.entries(sort)) {
30+
sortObject[field] = direction === "asc" || direction === 1 ? 1 : -1;
31+
}
32+
return sortObject;
33+
}
34+
35+
exports.parseSort = parseSort;

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mongoose-aggregate-paginate-v2",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "A cursor based custom aggregate pagination library for Mongoose with customizable labels.",
55
"main": "index.js",
66
"types": "types/index.d.ts",

tests/index.js

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,11 @@ describe("mongoose-paginate", function () {
294294
};
295295
return Book.aggregatePaginate(aggregate, options).then((result) => {
296296
expect(result.docs).to.have.length(10);
297-
expect(result.docs[0].title).to.equal("Book #41");
297+
expect(result.docs[0].title).to.equal("Book #11");
298298
expect(result.totalDocs).to.equal(100);
299299
expect(result.limit).to.equal(10);
300300
expect(result.page).to.equal(2);
301-
expect(result.pagingCounter).to.equal(41);
301+
expect(result.pagingCounter).to.equal(11);
302302
expect(result.hasPrevPage).to.equal(true);
303303
expect(result.hasNextPage).to.equal(true);
304304
expect(result.prevPage).to.equal(1);
@@ -308,6 +308,78 @@ describe("mongoose-paginate", function () {
308308
});
309309
});
310310

311+
describe("sorting", function () {
312+
var aggregate = Book.aggregate([
313+
{
314+
$match: {
315+
title: {
316+
$in: [/Book/i],
317+
},
318+
},
319+
},
320+
]);
321+
it("with object ascending", function () {
322+
return Book.aggregatePaginate(aggregate, {
323+
sort: { date: "asc" },
324+
limit: 40,
325+
}).then((result) => {
326+
expect(result.docs).to.have.length(40);
327+
expect(result.docs[0].title).to.equal("Book #1");
328+
expect(result.docs[result.docs.length - 1].title).to.equal("Book #40");
329+
});
330+
});
331+
it("with object descending", function () {
332+
return Book.aggregatePaginate(aggregate, {
333+
sort: { date: -1 },
334+
limit: 50,
335+
}).then((result) => {
336+
expect(result.docs).to.have.length(50);
337+
expect(result.docs[0].title).to.equal("Book #100");
338+
expect(result.docs[result.docs.length - 1].title).to.equal("Book #51");
339+
});
340+
});
341+
it("with string ascending", function () {
342+
return Book.aggregatePaginate(aggregate, {
343+
sort: "date",
344+
limit: 10,
345+
}).then((result) => {
346+
expect(result.docs).to.have.length(10);
347+
expect(result.docs[0].title).to.equal("Book #1");
348+
expect(result.docs[result.docs.length - 1].title).to.equal("Book #10");
349+
});
350+
});
351+
it("with string descending", function () {
352+
return Book.aggregatePaginate(aggregate, {
353+
sort: "-date",
354+
limit: 10,
355+
}).then((result) => {
356+
expect(result.docs).to.have.length(10);
357+
expect(result.docs[0].title).to.equal("Book #100");
358+
expect(result.docs[result.docs.length - 1].title).to.equal("Book #91");
359+
});
360+
});
361+
it("with array ascending", function () {
362+
return Book.aggregatePaginate(aggregate, {
363+
sort: [["date", "asc"]],
364+
limit: 10,
365+
}).then((result) => {
366+
expect(result.docs).to.have.length(10);
367+
expect(result.docs[0].title).to.equal("Book #1");
368+
expect(result.docs[result.docs.length - 1].title).to.equal("Book #10");
369+
});
370+
});
371+
it("with array descending", function () {
372+
return Book.aggregatePaginate(aggregate, {
373+
sort: [["date", "desc"]],
374+
limit: 10,
375+
}).then((result) => {
376+
expect(result.docs).to.have.length(10);
377+
expect(result.docs[0].title).to.equal("Book #100");
378+
expect(result.docs[result.docs.length - 1].title).to.equal("Book #91");
379+
});
380+
});
381+
});
382+
311383
after(async function () {
312384
await mongoose.connection.db.dropDatabase();
313385
});

0 commit comments

Comments
 (0)