mongoose-paginate-v2
Advanced tools
Comparing version 1.0.22 to 1.0.23
@@ -71,3 +71,5 @@ "use strict"; | ||
var page; | ||
var skip; // Labels | ||
var skip; | ||
var docsPromise = []; | ||
var docs = []; // Labels | ||
@@ -96,29 +98,30 @@ var labelDocs = customLabels.docs; | ||
var countPromise = this.countDocuments(query).exec(); | ||
var mQuery = this.find(query, projection, findOptions); | ||
mQuery.select(select); | ||
mQuery.sort(sort); | ||
mQuery.lean(lean); // Hack for mongo < v3.4 | ||
if (Object.keys(collation).length > 0) { | ||
mQuery.collation(collation); | ||
} | ||
if (limit) { | ||
var mQuery = this.find(query, projection, findOptions); | ||
mQuery.select(select); | ||
mQuery.sort(sort); | ||
mQuery.lean(lean); // Hack for mongo < v3.4 | ||
if (limit) { | ||
if (Object.keys(collation).length > 0) { | ||
mQuery.collation(collation); | ||
} | ||
mQuery.skip(skip); | ||
mQuery.limit(limit); | ||
} | ||
if (populate) { | ||
mQuery.populate(populate); | ||
} | ||
if (populate) { | ||
mQuery.populate(populate); | ||
} | ||
var docsPromise = mQuery.exec(); | ||
docsPromise = mQuery.exec(); | ||
if (lean && leanWithId) { | ||
docsPromise = docsPromise.then(function (docs) { | ||
docs.forEach(function (doc) { | ||
doc.id = String(doc._id); | ||
if (lean && leanWithId) { | ||
docsPromise = docsPromise.then(function (docs) { | ||
docs.forEach(function (doc) { | ||
doc.id = String(doc._id); | ||
}); | ||
return docs; | ||
}); | ||
return docs; | ||
}); | ||
} | ||
} | ||
@@ -142,3 +145,3 @@ | ||
if (typeof page !== 'undefined') { | ||
var pages = Math.ceil(count / limit) || 1; | ||
var pages = limit > 0 ? Math.ceil(count / limit) || 1 : null; | ||
result.hasPrevPage = false; | ||
@@ -145,0 +148,0 @@ result.hasNextPage = false; |
{ | ||
"name": "mongoose-paginate-v2", | ||
"version": "1.0.22", | ||
"version": "1.0.23", | ||
"description": "A cursor based custom pagination library for Mongoose with customizable labels.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -195,7 +195,6 @@ # mongoose-paginate-v2 | ||
```javascript | ||
Model.paginate({}, { offset: 100, limit: 0 }).then(function(result) { | ||
Model.paginate({}, { limit: 0 }).then(function(result) { | ||
// result.docs - empty array | ||
// result.totalDocs | ||
// result.limit - 0 | ||
// result.offset - 100 | ||
// result.limit - 0 | ||
}); | ||
@@ -202,0 +201,0 @@ ``` |
@@ -42,3 +42,6 @@ /** | ||
function paginate(query, options, callback) { | ||
options = { ...deafultOptions, ...options }; | ||
options = { | ||
...deafultOptions, | ||
...options | ||
}; | ||
query = query || {}; | ||
@@ -65,2 +68,5 @@ | ||
let docsPromise = []; | ||
let docs = []; | ||
// Labels | ||
@@ -90,30 +96,33 @@ const labelDocs = customLabels.docs; | ||
const mQuery = this.find(query, projection, findOptions); | ||
mQuery.select(select); | ||
mQuery.sort(sort); | ||
mQuery.lean(lean); | ||
if (limit) { | ||
const mQuery = this.find(query, projection, findOptions); | ||
mQuery.select(select); | ||
mQuery.sort(sort); | ||
mQuery.lean(lean); | ||
// Hack for mongo < v3.4 | ||
if (Object.keys(collation).length > 0) { | ||
mQuery.collation(collation); | ||
} | ||
// Hack for mongo < v3.4 | ||
if (Object.keys(collation).length > 0) { | ||
mQuery.collation(collation); | ||
} | ||
if (limit) { | ||
mQuery.skip(skip); | ||
mQuery.limit(limit); | ||
} | ||
if (populate) { | ||
mQuery.populate(populate); | ||
} | ||
let docsPromise = mQuery.exec(); | ||
if (populate) { | ||
mQuery.populate(populate); | ||
} | ||
if (lean && leanWithId) { | ||
docsPromise = docsPromise.then((docs) => { | ||
docs.forEach((doc) => { | ||
doc.id = String(doc._id); | ||
docsPromise = mQuery.exec(); | ||
if (lean && leanWithId) { | ||
docsPromise = docsPromise.then((docs) => { | ||
docs.forEach((doc) => { | ||
doc.id = String(doc._id); | ||
}); | ||
return docs; | ||
}); | ||
return docs; | ||
}); | ||
} | ||
} | ||
@@ -135,4 +144,5 @@ | ||
if (typeof page !== 'undefined') { | ||
const pages = Math.ceil(count / limit) || 1; | ||
const pages = (limit > 0) ? (Math.ceil(count / limit) || 1) : null; | ||
result.hasPrevPage = false; | ||
@@ -139,0 +149,0 @@ result.hasNextPage = false; |
@@ -78,3 +78,3 @@ 'use strict'; | ||
title: { | ||
$in: [/Book/i] | ||
$in: [/Book #1/i] | ||
} | ||
@@ -84,2 +84,3 @@ }; | ||
var options = { | ||
limit:0, | ||
sort: { _id: 1 }, | ||
@@ -94,13 +95,14 @@ collation: { | ||
return Book.paginate(query, options).then((result) => { | ||
expect(result.docs).to.have.length(10); | ||
expect(result.docs[0].title).to.equal('Book #1'); | ||
expect(result.totalDocs).to.equal(100); | ||
expect(result.limit).to.equal(10); | ||
console.log(result); | ||
expect(result.docs).to.have.length(0); | ||
expect(result.totalDocs).to.equal(12); | ||
expect(result.limit).to.equal(0); | ||
expect(result.page).to.equal(1); | ||
expect(result.pagingCounter).to.equal(1); | ||
expect(result.hasPrevPage).to.equal(false); | ||
expect(result.hasNextPage).to.equal(true); | ||
expect(result.hasNextPage).to.equal(false); | ||
expect(result.prevPage).to.equal(null); | ||
expect(result.nextPage).to.equal(2); | ||
expect(result.totalPages).to.equal(10); | ||
expect(result.nextPage).to.equal(null); | ||
expect(result.totalPages).to.equal(null); | ||
}); | ||
@@ -165,3 +167,3 @@ }); | ||
return Book.paginate(query, options).then((result) => { | ||
console.log(result); | ||
expect(result.itemsList).to.have.length(10); | ||
@@ -168,0 +170,0 @@ expect(result.itemsList[0].title).to.equal('Book #41'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
31393
457
0
224