Socket
Socket
Sign inDemoInstall

mongoose-paginate-v2

Package Overview
Dependencies
0
Maintainers
1
Versions
58
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.4.3 to 1.5.0

dist/pagination-parameters.js

6

CHANGELOG.md
# Changelog
## v1.5.0
[2022-01-09]
- Added feature: PaginationParameters helper class
## v1.4.3

@@ -4,0 +10,0 @@

3

dist/index.js

@@ -43,2 +43,4 @@ "use strict";

*/
var PaginationParametersHelper = require('./pagination-parameters');
var defaultOptions = {

@@ -294,2 +296,3 @@ customLabels: {

module.exports.PaginationParameters = PaginationParametersHelper;
module.exports.paginate = paginate;

5

package.json
{
"name": "mongoose-paginate-v2",
"version": "1.4.3",
"version": "1.5.0",
"description": "A custom pagination library for Mongoose with customizable labels.",

@@ -45,3 +45,4 @@ "main": "dist/index.js",

"paginator",
"plugin"
"plugin",
"helper"
],

@@ -48,0 +49,0 @@ "author": "Aravind NC <aravind_n_c@yahoo.co.in> (https://aravindnc.com)",

@@ -68,4 +68,4 @@ ![Banner](static/banner.jpg)

- `[pagination]` {Boolean} - If `pagination` is set to false, it will return all docs without adding limit condition. (Default: True)
- `[useEstimatedCount]` - Enable [estimatedDocumentCount](https://docs.mongodb.com/manual/reference/method/db.collection.estimatedDocumentCount/) for larger datasets. Does not count based on given query, so the count will match entire collection size. (Default: False)
- `[useCustomCountFn]` - Enable custom function for count datasets. (Default: False)
- `[useEstimatedCount]` {Boolean} - Enable [estimatedDocumentCount](https://docs.mongodb.com/manual/reference/method/db.collection.estimatedDocumentCount/) for larger datasets. Does not count based on given query, so the count will match entire collection size. (Default: False)
- `[useCustomCountFn]` {Boolean} - Enable custom function for count datasets. (Default: False)
- `[forceCountFn]` {Boolean} - Set this to true, if you need to support \$geo queries. (Default: False)

@@ -178,2 +178,27 @@ - `[allowDiskUse]` {Boolean} - Set this to true, which allows the MongoDB server to use more than 100 MB for query. This option can let you work around QueryExceededMemoryLimitNoDiskUseAllowed errors from the MongoDB server. (Default: False)

### Use helper-class for passing query object into Model.paginate()
For conveniently passing on all request query parameters into paginate(), without having to specify them all in the controller, you can use the PaginationParameters-class. The example below is with express.js code, but can be applied to any request, where the query string has been parsed into an object.
```javascript
const { PaginationParameters } = require('mongoose-paginate-v2');
// req.query = {
// page: 1,
// limit: 10,
// query: {"color": "blue", "published": true}
// projection: {"color": 1}
// }
req.get('/route', (req, res) => {
Model.paginate(...new PaginationParameters(req).get()).then((result) => {
// process the paginated result.
});
console.log(new PaginationParameters(req).get()); // [{ color: "blue", published: true }, { page: 1, limit: 10, projection: { color: 1 } }]
});
```
**Note**: `req.query.projection` and `req.query.query` have to be valid JSON. The same goes for any option which is passed into Model.paginate() as an array or object.
### Other Examples

@@ -364,3 +389,3 @@

- Ensure all tests pass before you commit by running `npm run test`
- Ensure all tests pass before you commit by running `npm run test`. In order to run the tests, you need to have the Mongo Deamon running locally.
- There are pre-commit hooks that run to ensure the _files you've changed_ are formatted correctly.

@@ -367,0 +392,0 @@ - Optionally you can manually run `npm run lint && npm run prettier` to lint and format every relevant file

@@ -23,2 +23,3 @@ /**

*/
const PaginationParametersHelper = require('./pagination-parameters');

@@ -293,2 +294,3 @@ const defaultOptions = {

module.exports.PaginationParameters = PaginationParametersHelper;
module.exports.paginate = paginate;

@@ -7,2 +7,3 @@ 'use strict';

let mongoosePaginate = require('../dist/index');
let PaginationParameters = require('../dist/pagination-parameters');

@@ -31,2 +32,41 @@ let MONGO_URI = 'mongodb://localhost/mongoose_paginate_test';

/**
* Test a collection of documents, and expect it to not be paginated
*
* @param {object} result
* */
const testUnpaginatedCollection = (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);
};
/**
* Test that the result.docs are sorted after their 'price' in descending order
*
* @param {object} result
* */
const testDescendingPrice = (result) => {
let isSorted = true;
// Skip the first index of the collection, but then make sure that
// every following document.price is lower than the last one.
// If it is not, the sorting is broken, thus failing the test
for (let i = 1; i < result.docs.length; i++) {
if (result.docs[i].price > result.docs[i - 1].price) {
isSorted = false;
break;
}
}
expect(isSorted).to.be.true;
};
BookSchema.plugin(mongoosePaginate);

@@ -514,12 +554,3 @@

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);
testUnpaginatedCollection(result);
});

@@ -600,2 +631,79 @@ });

it('PaginationParameters-helper -> number type options work', () => {
const request = {
query: {
limit: '20',
page: '2',
// mocks how Frameworks such as Express and Nestjs would parse a JSON object in the query string
query: '{"title": {"$in": [/Book/i]}}',
},
};
return Book.paginate(...new PaginationParameters(request).get()).then(
(result) => {
expect(result.docs).to.have.length(20);
expect(result.limit).to.equal(20);
expect(result.page).to.equal(2);
expect(result.hasPrevPage).to.equal(true);
expect(result.hasNextPage).to.equal(true);
expect(result.prevPage).to.equal(1);
expect(result.nextPage).to.equal(3);
expect(result.totalPages).to.equal(5);
}
);
});
it('PaginationParameters-helper -> (JSON) object type options work', () => {
const request = {
query: {
limit: '5',
page: '4',
// mocks how Frameworks such as Express and Nestjs would read a JSON object as a part of the query string
query: '{"price": {"$gt": 100}}',
sort: '{"price": "desc"}',
},
};
return Book.paginate(...new PaginationParameters(request).get()).then(
(result) => {
testDescendingPrice(result);
expect(result.docs).to.have.length(5);
expect(result.limit).to.equal(5);
expect(result.page).to.equal(4);
expect(result.hasPrevPage).to.equal(true);
expect(result.hasNextPage).to.equal(true);
expect(result.prevPage).to.equal(3);
expect(result.nextPage).to.equal(5);
}
);
});
it('PaginationParameters-helper -> boolean options work', () => {
const request = {
query: {
pagination: false,
},
};
return Book.paginate(...new PaginationParameters(request).get()).then(
(result) => {
testUnpaginatedCollection(result);
}
);
});
it('PaginationParameters-helper -> string options work', () => {
const request = {
query: {
sort: '-price',
},
};
return Book.paginate(...new PaginationParameters(request).get()).then(
(result) => {
testDescendingPrice(result);
}
);
});
after(function (done) {

@@ -602,0 +710,0 @@ mongoose.connection.db.dropDatabase(done);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc