mongoose-paginate-v2
Advanced tools
Comparing version 1.3.3 to 1.3.4
@@ -56,3 +56,4 @@ "use strict"; | ||
select: '', | ||
options: {} | ||
options: {}, | ||
pagination: true | ||
}; | ||
@@ -70,7 +71,8 @@ | ||
select = _options.select, | ||
sort = _options.sort; | ||
sort = _options.sort, | ||
pagination = _options.pagination; | ||
var customLabels = _objectSpread({}, defaultOptions.customLabels, {}, options.customLabels); | ||
var limit = parseInt(options.limit, 10) || 0; | ||
var limit = parseInt(options.limit, 10) > 0 ? parseInt(options.limit, 10) : 0; | ||
var isCallbackSpecified = typeof callback === 'function'; | ||
@@ -124,4 +126,7 @@ var findOptions = options.options; | ||
mQuery.skip(skip); | ||
mQuery.limit(limit); | ||
if (pagination) { | ||
mQuery.skip(skip); | ||
mQuery.limit(limit); | ||
} | ||
docsPromise = mQuery.exec(); | ||
@@ -145,4 +150,3 @@ | ||
var meta = { | ||
[labelTotal]: count, | ||
[labelLimit]: limit | ||
[labelTotal]: count | ||
}; | ||
@@ -155,10 +159,17 @@ var result = {}; | ||
if (typeof page !== 'undefined') { | ||
var pages = limit > 0 ? Math.ceil(count / limit) || 1 : null; | ||
meta[labelHasPrevPage] = false; | ||
meta[labelHasNextPage] = false; | ||
meta[labelPage] = page; | ||
meta[labelTotalPages] = pages; | ||
meta[labelPagingCounter] = (page - 1) * limit + 1; // Set prev page | ||
var pages = limit > 0 ? Math.ceil(count / limit) || 1 : null; // Setting default values | ||
meta[labelLimit] = count; | ||
meta[labelTotalPages] = 1; | ||
meta[labelPage] = page; | ||
meta[labelPagingCounter] = (page - 1) * limit + 1; | ||
meta[labelHasPrevPage] = false; | ||
meta[labelHasNextPage] = false; | ||
meta[labelPrevPage] = null; | ||
meta[labelNextPage] = null; | ||
if (pagination) { | ||
meta[labelLimit] = limit; | ||
meta[labelTotalPages] = pages; // Set prev page | ||
if (page > 1) { | ||
@@ -165,0 +176,0 @@ meta[labelHasPrevPage] = true; |
{ | ||
"name": "mongoose-paginate-v2", | ||
"version": "1.3.3", | ||
"version": "1.3.4", | ||
"description": "A cursor based custom pagination library for Mongoose with customizable labels.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -0,1 +1,3 @@ | ||
![Banner](static/banner.jpg) | ||
# mongoose-paginate-v2 | ||
@@ -58,2 +60,3 @@ [![npm version](https://img.shields.io/npm/v/mongoose-paginate-v2.svg)](https://www.npmjs.com/package/mongoose-paginate-v2) | ||
- `[customLabels]` {Object} - Developers can provide custom labels for manipulating the response data. | ||
- `[pagination]` {Boolean} - If `pagination` is set to false, it will return all docs without adding limit condition. (Default: True) | ||
* `[callback(err, result)]` - If specified the callback is called once pagination results are retrieved or when an error has occurred | ||
@@ -80,2 +83,9 @@ | ||
### Note | ||
There are few operators that this plugin does not support, below are the list and suggested replacements | ||
* $where: $expr | ||
* $near: $geoWithin with $center | ||
* $nearSphere: $geoWithin with $centerSphere | ||
### Sample Usage | ||
@@ -232,4 +242,26 @@ | ||
#### Fetch all docs without pagination. | ||
If you need to fetch all the documents in the collection without applying a limit. Then set `pagination` as false, | ||
```javascript | ||
const options = { | ||
pagination: false | ||
}; | ||
Model.paginate({}, options, function(err, result) { | ||
// result.docs | ||
// result.totalDocs = 100 | ||
// result.limit = 100 | ||
// result.page = 1 | ||
// result.totalPages = 1 | ||
// result.hasNextPage = false | ||
// result.nextPage = null | ||
// result.hasPrevPage = false | ||
// result.prevPage = null | ||
// result.pagingCounter = 1 | ||
}); | ||
``` | ||
## License | ||
[MIT](LICENSE) |
@@ -42,2 +42,3 @@ /** | ||
options: {}, | ||
pagination: true | ||
}; | ||
@@ -60,3 +61,4 @@ | ||
select, | ||
sort | ||
sort, | ||
pagination | ||
} = options; | ||
@@ -69,3 +71,3 @@ | ||
const limit = parseInt(options.limit, 10) || 0; | ||
const limit = parseInt(options.limit, 10) > 0 ? parseInt(options.limit, 10) : 0; | ||
@@ -123,6 +125,8 @@ const isCallbackSpecified = typeof callback === 'function'; | ||
} | ||
mQuery.skip(skip); | ||
mQuery.limit(limit); | ||
if (pagination) { | ||
mQuery.skip(skip); | ||
mQuery.limit(limit); | ||
} | ||
docsPromise = mQuery.exec(); | ||
@@ -138,2 +142,3 @@ | ||
} | ||
} | ||
@@ -145,7 +150,6 @@ | ||
const [count, docs] = values; | ||
const meta = { | ||
[labelTotal]: count, | ||
[labelLimit]: limit | ||
[labelTotal]: count | ||
}; | ||
let result = {}; | ||
@@ -157,11 +161,19 @@ | ||
if (typeof page !== 'undefined') { | ||
const pages = (limit > 0) ? (Math.ceil(count / limit) || 1) : null; | ||
const pages = (limit > 0) ? (Math.ceil(count / limit) || 1) : null; | ||
// Setting default values | ||
meta[labelLimit] = count; | ||
meta[labelTotalPages] = 1; | ||
meta[labelPage] = page; | ||
meta[labelPagingCounter] = ((page - 1) * limit) + 1; | ||
meta[labelHasPrevPage] = false; | ||
meta[labelHasNextPage] = false; | ||
meta[labelPage] = page; | ||
meta[labelHasPrevPage] = false; | ||
meta[labelHasNextPage] = false; | ||
meta[labelPrevPage] = null; | ||
meta[labelNextPage] = null; | ||
if (pagination) { | ||
meta[labelLimit] = limit; | ||
meta[labelTotalPages] = pages; | ||
meta[labelPagingCounter] = ((page - 1) * limit) + 1; | ||
@@ -183,2 +195,3 @@ // Set prev page | ||
} | ||
} | ||
@@ -214,2 +227,2 @@ | ||
module.exports.paginate = paginate; | ||
module.exports.paginate = paginate; |
@@ -276,3 +276,9 @@ 'use strict'; | ||
var query = { | ||
loc: { $geoWithin: { $center: [ [-10, 20], 999 ] } } | ||
loc: { | ||
$geoWithin: { | ||
$center: [ | ||
[-10, 20], 999 | ||
] | ||
} | ||
} | ||
}; | ||
@@ -303,2 +309,28 @@ | ||
it('all data (without pagination)', function () { | ||
var query = { | ||
title: { | ||
$in: [/Book/i] | ||
} | ||
}; | ||
var options = { | ||
pagination: false | ||
}; | ||
return Book.paginate(query, options).then((result) => { | ||
expect(result.docs).to.have.length(100); | ||
expect(result.totalDocs).to.equal(100); | ||
expect(result.limit).to.equal(100); | ||
expect(result.page).to.equal(1); | ||
expect(result.pagingCounter).to.equal(1); | ||
expect(result.hasPrevPage).to.equal(false); | ||
expect(result.hasNextPage).to.equal(false); | ||
expect(result.prevPage).to.equal(null); | ||
expect(result.nextPage).to.equal(null); | ||
expect(result.totalPages).to.equal(1); | ||
}); | ||
}); | ||
after(function (done) { | ||
@@ -305,0 +337,0 @@ mongoose.connection.db.dropDatabase(done); |
274782
12
661
265