Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongoose-paginate-v2

Package Overview
Dependencies
Maintainers
1
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-paginate-v2 - npm Package Compare versions

Comparing version 1.3.4 to 1.3.6

29

dist/index.js

@@ -32,2 +32,3 @@ "use strict";

* @param {Number} [options.limit=10]
* @param {Object} [options.read={}] - Determines the MongoDB nodes from which to read.
* @param {Function} [callback]

@@ -70,2 +71,3 @@ *

projection = _options.projection,
read = _options.read,
select = _options.select,

@@ -83,4 +85,3 @@ sort = _options.sort,

var skip;
var docsPromise = [];
var docs = []; // Labels
var docsPromise = []; // Labels

@@ -117,4 +118,14 @@ var labelDocs = customLabels.docs;

mQuery.sort(sort);
mQuery.lean(lean); // Hack for mongo < v3.4
mQuery.lean(lean);
if (read && read.pref) {
/**
* Determines the MongoDB nodes from which to read.
* @param read.pref one of the listed preference options or aliases
* @param read.tags optional tags for this query
*/
mQuery.read(read.pref, read.tags);
} // Hack for mongo < v3.4
if (Object.keys(collation).length > 0) {

@@ -157,2 +168,3 @@ mQuery.collation(collation);

meta.offset = offset;
page = Math.ceil((offset + 1) / limit);
}

@@ -194,2 +206,13 @@

if (limit == 0) {
meta[labelLimit] = 0;
meta[labelTotalPages] = null;
meta[labelPage] = null;
meta[labelPagingCounter] = null;
meta[labelPrevPage] = null;
meta[labelNextPage] = null;
meta[labelHasPrevPage] = false;
meta[labelHasNextPage] = false;
}
if (labelMeta) {

@@ -196,0 +219,0 @@ result = {

2

package.json
{
"name": "mongoose-paginate-v2",
"version": "1.3.4",
"version": "1.3.6",
"description": "A cursor based custom pagination library for Mongoose with customizable labels.",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

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

const mySchema = new mongoose.Schema({
/* your schema definition */
const mySchema = new mongoose.Schema({
/* your schema definition */
});

@@ -38,3 +38,3 @@

const myModel = mongoose.model('SampleModel', mySchema);
const myModel = mongoose.model('SampleModel', mySchema);

@@ -52,5 +52,5 @@ myModel.paginate().then({}) // Usage

* `[options]` {Object}
- `[select]` {Object | String} - Fields to return (by default returns all fields). [Documentation](http://mongoosejs.com/docs/api.html#query_Query-select)
- `[select]` {Object | String} - Fields to return (by default returns all fields). [Documentation](http://mongoosejs.com/docs/api.html#query_Query-select)
- `[collation]` {Object} - Specify the collation [Documentation](https://docs.mongodb.com/manual/reference/collation/)
- `[sort]` {Object | String} - Sort order. [Documentation](http://mongoosejs.com/docs/api.html#query_Query-sort)
- `[sort]` {Object | String} - Sort order. [Documentation](http://mongoosejs.com/docs/api.html#query_Query-sort)
- `[populate]` {Array | Object | String} - Paths which should be populated with other documents. [Documentation](http://mongoosejs.com/docs/api.html#query_Query-populate)

@@ -64,2 +64,5 @@ - `[lean=false]` {Boolean} - Should return plain javascript objects instead of Mongoose documents? [Documentation](http://mongoosejs.com/docs/api.html#query_Query-lean)

- `[pagination]` {Boolean} - If `pagination` is set to false, it will return all docs without adding limit condition. (Default: True)
- `[read]` {Object} - Determines the MongoDB nodes from which to read. Below are the available options.
- `[pref]`: One of the listed preference options or aliases.
- `[tags]`: Optional tags for this query. (Must be used with `[pref]`)
* `[callback(err, result)]` - If specified the callback is called once pagination results are retrieved or when an error has occurred

@@ -76,3 +79,3 @@

* `hasNextPage` {Bool} - Availability of next page.
* `page` {Number} - Current page number
* `page` {Number} - Current page number
* `totalPages` {Number} - Total number of pages.

@@ -87,9 +90,2 @@ * `offset` {Number} - Only if specified or default `page`/`offset` values were used

### 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

@@ -113,3 +109,3 @@

// result.page = 1
// result.totalPages = 10
// result.totalPages = 10
// result.hasNextPage = true

@@ -133,2 +129,4 @@ // result.nextPage = 2

* prevPage
* hasNextPage
* hasPrevPage
* totalPages

@@ -205,3 +203,3 @@ * pagingCounter

lean: true,
offset: 20,
offset: 20,
limit: 10

@@ -223,3 +221,3 @@ };

// result.totalDocs
// result.limit - 0
// result.limit - 0
});

@@ -235,3 +233,3 @@ ```

mongoosePaginate.paginate.options = {
mongoosePaginate.paginate.options = {
lean: true,

@@ -264,3 +262,3 @@ limit: 20

// result.page = 1
// result.totalPages = 1
// result.totalPages = 1
// result.hasNextPage = false

@@ -274,4 +272,37 @@ // result.nextPage = null

#### Setting read preference.
Determines the MongoDB nodes from which to read.
```js
const options = {
lean: true,
limit: 10,
page: 1,
read: {
pref: 'secondary',
tags: [{
region: 'South'
}]
}
};
Model.paginate({}, options, function(err, result) {
// Result
});
```
Below are some references to understand more about preferences,
- https://github.com/Automattic/mongoose/blob/master/lib/query.js#L1008
- https://docs.mongodb.com/manual/core/read-preference/
- http://mongodb.github.io/node-mongodb-native/driver-articles/anintroductionto1_1and2_2.html#read-preferences
## 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
## License
[MIT](LICENSE)

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

* @param {Number} [options.limit=10]
* @param {Object} [options.read={}] - Determines the MongoDB nodes from which to read.
* @param {Function} [callback]

@@ -60,2 +61,3 @@ *

projection,
read,
select,

@@ -81,3 +83,2 @@ sort,

let docsPromise = [];
let docs = [];

@@ -117,2 +118,11 @@ // Labels

if (read && read.pref) {
/**
* Determines the MongoDB nodes from which to read.
* @param read.pref one of the listed preference options or aliases
* @param read.tags optional tags for this query
*/
mQuery.read(read.pref, read.tags);
}
// Hack for mongo < v3.4

@@ -157,2 +167,3 @@ if (Object.keys(collation).length > 0) {

meta.offset = offset;
page = Math.ceil((offset + 1) / limit);
}

@@ -199,2 +210,13 @@

if (limit == 0) {
meta[labelLimit] = 0;
meta[labelTotalPages] = null;
meta[labelPage] = null;
meta[labelPagingCounter] = null;
meta[labelPrevPage] = null;
meta[labelNextPage] = null;
meta[labelHasPrevPage] = false;
meta[labelHasNextPage] = false;
}
if (labelMeta) {

@@ -225,2 +247,2 @@ result = {

module.exports.paginate = paginate;
module.exports.paginate = paginate;

@@ -90,153 +90,210 @@ 'use strict';

describe('paginates', function () {
it('with limit and page', function () {
var query = {
title: {
$in: [/Book #1/i]
}
};
it('with page and limit', function () {
var query = {
title: {
$in: [/Book/i]
}
};
var options = {
limit: 0,
sort: {
_id: 1
},
collation: {
locale: 'en',
strength: 2
},
lean: true
};
var options = {
limit: 10,
page: 5,
lean: true
};
return Book.paginate(query, options).then((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(false);
expect(result.prevPage).to.equal(null);
expect(result.nextPage).to.equal(null);
expect(result.totalPages).to.equal(null);
});
return Book.paginate(query, options).then((result) => {
expect(result.docs).to.have.length(10);
expect(result.totalDocs).to.equal(100);
expect(result.limit).to.equal(10);
expect(result.page).to.equal(5);
expect(result.pagingCounter).to.equal(41);
expect(result.hasPrevPage).to.equal(true);
expect(result.hasNextPage).to.equal(true);
expect(result.prevPage).to.equal(4);
expect(result.nextPage).to.equal(6);
expect(result.totalPages).to.equal(10);
});
});
/*
it('with $where condition', function () {
var query = {
'$where': 'this.price < 100'
};
it('with offset and limit (not page)', function () {
var query = {
title: {
$in: [/Book/i]
}
};
var options = {
sort: {
price: -1
},
page: 2
};
var options = {
limit: 10,
offset: 98,
sort: {
_id: 1
},
lean: true
};
return Book.paginate(query, options).then((result) => {
expect(result.docs).to.have.length(6);
expect(result.docs[0].title).to.equal('Book #6');
expect(result.totalDocs).to.equal(16);
expect(result.limit).to.equal(10);
expect(result.page).to.equal(2);
expect(result.pagingCounter).to.equal(11);
expect(result.hasPrevPage).to.equal(true);
expect(result.hasNextPage).to.equal(false);
expect(result.prevPage).to.equal(1);
expect(result.nextPage).to.equal(null);
expect(result.totalPages).to.equal(2);
});
return Book.paginate(query, options).then((result) => {
expect(result.docs).to.have.length(2);
expect(result.totalDocs).to.equal(100);
expect(result.limit).to.equal(10);
expect(result.page).to.equal(10);
expect(result.pagingCounter).to.equal(91);
expect(result.hasPrevPage).to.equal(true);
expect(result.hasNextPage).to.equal(false);
expect(result.prevPage).to.equal(9);
expect(result.nextPage).to.equal(null);
expect(result.totalPages).to.equal(10);
});
*/
});
it('with empty custom labels', function () {
var query = {
title: {
$in: [/Book/i]
}
};
it('with limit=0 (metadata only)', function () {
var query = {
title: {
$in: [/Book #1/i]
}
};
const myCustomLabels = {
nextPage: false,
prevPage: '',
};
var options = {
limit: 0,
sort: {
_id: 1
},
collation: {
locale: 'en',
strength: 2
},
lean: true
};
var options = {
sort: {
_id: 1
},
limit: 10,
page: 5,
select: {
title: 1,
price: 1
},
customLabels: myCustomLabels
};
return Book.paginate(query, options).then((result) => {
return Book.paginate(query, options).then((result) => {
expect(result.docs).to.have.length(10);
expect(result.docs[0].title).to.equal('Book #41');
expect(result.totalDocs).to.equal(100);
expect(result.limit).to.equal(10);
expect(result.page).to.equal(5);
expect(result.pagingCounter).to.equal(41);
expect(result.hasPrevPage).to.equal(true);
expect(result.hasNextPage).to.equal(true);
expect(result.totalPages).to.equal(10);
expect(result.prevPage).to.equal(undefined);
expect(result.nextPage).to.equal(undefined);
});
expect(result.docs).to.have.length(0);
expect(result.totalDocs).to.equal(12);
expect(result.limit).to.equal(0);
expect(result.page).to.equal(null);
expect(result.pagingCounter).to.equal(null);
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(null);
});
});
it('with custom labels', function () {
var query = {
title: {
$in: [/Book/i]
}
};
/*
it('with $where condition', function () {
var query = {
'$where': 'this.price < 100'
};
const myCustomLabels = {
totalDocs: 'itemCount',
docs: 'itemsList',
limit: 'perPage',
page: 'currentPage',
nextPage: 'next',
prevPage: 'prev',
totalPages: 'pageCount',
pagingCounter: 'pageCounter',
hasPrevPage: 'hasPrevious',
hasNextPage: 'hasNext'
};
var options = {
sort: {
price: -1
},
page: 2
};
var options = {
sort: {
_id: 1
},
limit: 10,
page: 5,
select: {
title: 1,
price: 1
},
customLabels: myCustomLabels
};
return Book.paginate(query, options).then((result) => {
expect(result.itemsList).to.have.length(10);
expect(result.itemsList[0].title).to.equal('Book #41');
expect(result.itemCount).to.equal(100);
expect(result.perPage).to.equal(10);
expect(result.currentPage).to.equal(5);
expect(result.pageCounter).to.equal(41);
expect(result.hasPrevious).to.equal(true);
expect(result.hasNext).to.equal(true);
expect(result.prev).to.equal(4);
expect(result.next).to.equal(6);
expect(result.pageCount).to.equal(10);
});
return Book.paginate(query, options).then((result) => {
expect(result.docs).to.have.length(6);
expect(result.docs[0].title).to.equal('Book #6');
expect(result.totalDocs).to.equal(16);
expect(result.limit).to.equal(10);
expect(result.page).to.equal(2);
expect(result.pagingCounter).to.equal(11);
expect(result.hasPrevPage).to.equal(true);
expect(result.hasNextPage).to.equal(false);
expect(result.prevPage).to.equal(1);
expect(result.nextPage).to.equal(null);
expect(result.totalPages).to.equal(2);
});
});
*/
it('with empty custom labels', function () {
var query = {
title: {
$in: [/Book/i]
}
};
const myCustomLabels = {
nextPage: false,
prevPage: '',
};
var options = {
sort: {
_id: 1
},
limit: 10,
page: 5,
select: {
title: 1,
price: 1
},
customLabels: myCustomLabels
};
return Book.paginate(query, options).then((result) => {
expect(result.docs).to.have.length(10);
expect(result.docs[0].title).to.equal('Book #41');
expect(result.totalDocs).to.equal(100);
expect(result.limit).to.equal(10);
expect(result.page).to.equal(5);
expect(result.pagingCounter).to.equal(41);
expect(result.hasPrevPage).to.equal(true);
expect(result.hasNextPage).to.equal(true);
expect(result.totalPages).to.equal(10);
expect(result.prevPage).to.equal(undefined);
expect(result.nextPage).to.equal(undefined);
});
});
it('with custom labels', function () {
var query = {
title: {
$in: [/Book/i]
}
};
const myCustomLabels = {
totalDocs: 'itemCount',
docs: 'itemsList',
limit: 'perPage',
page: 'currentPage',
nextPage: 'next',
prevPage: 'prev',
totalPages: 'pageCount',
pagingCounter: 'pageCounter',
hasPrevPage: 'hasPrevious',
hasNextPage: 'hasNext'
};
var options = {
sort: {
_id: 1
},
limit: 10,
page: 5,
select: {
title: 1,
price: 1
},
customLabels: myCustomLabels
};
return Book.paginate(query, options).then((result) => {
expect(result.itemsList).to.have.length(10);
expect(result.itemsList[0].title).to.equal('Book #41');
expect(result.itemCount).to.equal(100);
expect(result.perPage).to.equal(10);
expect(result.currentPage).to.equal(5);
expect(result.pageCounter).to.equal(41);
expect(result.hasPrevious).to.equal(true);
expect(result.hasNext).to.equal(true);
expect(result.prev).to.equal(4);
expect(result.next).to.equal(6);
expect(result.pageCount).to.equal(10);
});
});
it('with custom Meta label', function () {

@@ -343,2 +400,2 @@ var query = {

});
});
SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc