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.2.1 to 1.3.0

dist/index.js

6

package.json
{
"name": "mongoose-paginate-v2",
"version": "1.2.1",
"version": "1.3.0",
"description": "A cursor based custom pagination library for Mongoose with customizable labels.",
"main": "lib/index.js",
"main": "dist/index.js",
"scripts": {
"pretest": "npm run build",
"test": "./node_modules/.bin/mocha tests/*.js -R spec --ui bdd --timeout 5000",
"build": "babel src -d lib",
"build": "babel src -d dist",
"prepublish": "npm run test"

@@ -11,0 +11,0 @@ },

@@ -15,3 +15,2 @@ # mongoose-paginate-v2

## Installation

@@ -28,7 +27,7 @@

```js
var mongoose = require('mongoose');
var mongoosePaginate = require('mongoose-paginate-v2');
const mongoose = require('mongoose');
const mongoosePaginate = require('mongoose-paginate-v2');
var mySchema = new mongoose.Schema({
/* your schema definition */
const mySchema = new mongoose.Schema({
/* your schema definition */
});

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

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

@@ -67,2 +66,3 @@ myModel.paginate().then({}) // Usage

Promise fulfilled with object having properties:
* `docs` {Array} - Array of documents

@@ -79,2 +79,3 @@ * `totalDocs` {Number} - Total number of documents in collection that match a query

* `pagingCounter` {Number} - The starting sl. number of first document.
* `meta` {Object} - Object of pagination meta data (Default false).

@@ -88,23 +89,21 @@ Please note that the above properties can be renamed by setting customLabel attribute.

```javascript
const options = {
page: 1,
limit: 10,
collation: {
locale: 'en'
}
page: 1,
limit: 10,
collation: {
locale: 'en'
}
};
Model.paginate({}, options, function(err, result) {
// result.docs
// result.totalDocs = 100
// result.limit = 10
// result.page = 1
// result.totalPages = 10
// result.hasNextPage = true
// result.nextPage = 2
// result.hasPrevPage = false
// result.prevPage = null
// result.pagingCounter = 1
// result.docs
// result.totalDocs = 100
// result.limit = 10
// result.page = 1
// result.totalPages = 10
// result.hasNextPage = true
// result.nextPage = 2
// result.hasPrevPage = false
// result.prevPage = null
// result.pagingCounter = 1
});

@@ -125,37 +124,38 @@ ```

* pagingCounter
* meta
You should pass the names of the properties you wish to changes using `customLabels` object in options.
Set the property to false to remove it from the result.
Same query with custom labels
Same query with custom labels
```javascript
const myCustomLabels = {
totalDocs: 'itemCount',
docs: 'itemsList',
limit: 'perPage',
page: 'currentPage',
nextPage: 'next',
prevPage: 'prev',
totalPages: 'pageCount'
pagingCounter: 'slNo'
totalDocs: 'itemCount',
docs: 'itemsList',
limit: 'perPage',
page: 'currentPage',
nextPage: 'next',
prevPage: 'prev',
totalPages: 'pageCount',
pagingCounter: 'slNo',
meta: 'paginator'
};
const options = {
page: 1,
limit: 10,
customLabels: myCustomLabels
page: 1,
limit: 10,
customLabels: myCustomLabels
};
Model.paginate({}, options, function(err, result) {
// result.itemsList [here docs become itemsList]
// result.itemCount = 100 [here totalDocs becomes itemCount]
// result.perPage = 10 [here limit becomes perPage]
// result.currentPage = 1 [here page becomes currentPage]
// result.pageCount = 10 [here totalPages becomes pageCount]
// result.next = 2 [here nextPage becomes next]
// result.prev = null [here prevPage becomes prev]
// result.slNo = 1 [here pagingCounter becomes slNo]
// result.hasNextPage = true [not changeable]
// result.hasPrevPage = false [not changeable]
// result.itemsList [here docs become itemsList]
// result.paginator.itemCount = 100 [here totalDocs becomes itemCount]
// result.paginator.perPage = 10 [here limit becomes perPage]
// result.paginator.currentPage = 1 [here page becomes currentPage]
// result.paginator.pageCount = 10 [here totalPages becomes pageCount]
// result.paginator.next = 2 [here nextPage becomes next]
// result.paginator.prev = null [here prevPage becomes prev]
// result.paginator.slNo = 1 [here pagingCounter becomes slNo]
// result.paginator.hasNextPage = true
// result.paginator.hasPrevPage = false
});

@@ -167,8 +167,9 @@ ```

Using `offset` and `limit`:
```javascript
Model.paginate({}, { offset: 30, limit: 10 }, function(err, result) {
// result.docs
// result.totalPages
// result.limit - 10
// result.offset - 30
// result.docs
// result.totalPages
// result.limit - 10
// result.offset - 30
});

@@ -178,5 +179,6 @@ ```

With promise:
```js
Model.paginate({}, { offset: 30, limit: 10 }).then(function(result) {
// ...
// ...
});

@@ -190,12 +192,12 @@ ```

var options = {
select: 'title date author',
sort: { date: -1 },
populate: 'author',
lean: true,
offset: 20,
limit: 10
select: 'title date author',
sort: { date: -1 },
populate: 'author',
lean: true,
offset: 20,
limit: 10
};
Book.paginate(query, options).then(function(result) {
// ...
// ...
});

@@ -210,5 +212,5 @@ ```

Model.paginate({}, { limit: 0 }).then(function(result) {
// result.docs - empty array
// result.totalDocs
// result.limit - 0
// result.docs - empty array
// result.totalDocs
// result.limit - 0
});

@@ -220,2 +222,3 @@ ```

config.js:
```javascript

@@ -225,4 +228,4 @@ var mongoosePaginate = require('mongoose-paginate-v2');

mongoosePaginate.paginate.options = {
lean: true,
limit: 20
lean: true,
limit: 20
};

@@ -232,6 +235,7 @@ ```

controller.js:
```javascript
Model.paginate().then(function(result) {
// result.docs - array of plain javascript objects
// result.limit - 20
// result.docs - array of plain javascript objects
// result.limit - 20
});

@@ -241,2 +245,3 @@ ```

## License
[MIT](LICENSE)

@@ -31,2 +31,5 @@ /**

pagingCounter: 'pagingCounter',
hasPrevPage: 'hasPrevPage',
hasNextPage: 'hasNextPage',
meta: null,
},

@@ -86,2 +89,5 @@ collation: {},

const labelTotalPages = customLabels.totalPages;
const labelHasPrevPage = customLabels.hasPrevPage;
const labelHasNextPage = customLabels.hasNextPage;
const labelMeta = customLabels.meta;

@@ -113,7 +119,5 @@ if (options.hasOwnProperty('offset')) {

mQuery.skip(skip);
mQuery.limit(limit);
if (populate) {

@@ -133,3 +137,2 @@ mQuery.populate(populate);

}
}

@@ -140,10 +143,10 @@

const [count, docs] = values;
const result = {
[labelDocs]: docs,
const meta = {
[labelTotal]: count,
[labelLimit]: limit
};
let result = {};
if (typeof offset !== 'undefined') {
result.offset = offset;
meta.offset = offset;
}

@@ -155,15 +158,14 @@

result.hasPrevPage = false;
result.hasNextPage = false;
meta[labelHasPrevPage] = false;
meta[labelHasNextPage] = false;
meta[labelPage] = page;
meta[labelTotalPages] = pages;
meta[labelPagingCounter] = ((page - 1) * limit) + 1;
result[labelPage] = page;
result[labelTotalPages] = pages;
result[labelPagingCounter] = ((page - 1) * limit) + 1;
// Set prev page
if (page > 1) {
result.hasPrevPage = true;
result[labelPrevPage] = (page - 1);
meta[labelHasPrevPage] = true;
meta[labelPrevPage] = (page - 1);
} else {
result[labelPrevPage] = null;
meta[labelPrevPage] = null;
}

@@ -173,9 +175,24 @@

if (page < pages) {
result.hasNextPage = true;
result[labelNextPage] = (page + 1);
meta[labelHasNextPage] = true;
meta[labelNextPage] = (page + 1);
} else {
result[labelNextPage] = null;
meta[labelNextPage] = null;
}
}
// Remove customLabels set to false
delete meta['false'];
if (labelMeta) {
result = {
[labelDocs]: docs,
[labelMeta]: meta
};
} else {
result = {
[labelDocs]: docs,
...meta
};
}
return isCallbackSpecified ? callback(null, result) : Promise.resolve(result);

@@ -182,0 +199,0 @@ }).catch((error) => {

@@ -5,3 +5,3 @@ 'use strict';

let expect = require('chai').expect;
let mongoosePaginate = require('../lib/index');
let mongoosePaginate = require('../dist/index');

@@ -80,3 +80,2 @@ let MONGO_URI = 'mongodb://localhost/mongoose_paginate_test';

describe('paginates', function () {

@@ -103,3 +102,2 @@ it('with limit and page', function () {

return Book.paginate(query, options).then((result) => {
expect(result.docs).to.have.length(0);

@@ -147,5 +145,42 @@ expect(result.totalDocs).to.equal(12);

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 = {

@@ -165,3 +200,5 @@ title: {

totalPages: 'pageCount',
pagingCounter: 'pageCounter'
pagingCounter: 'pageCounter',
hasPrevPage: 'hasPrevious',
hasNextPage: 'hasNext'
};

@@ -180,6 +217,4 @@

customLabels: myCustomLabels
};
return Book.paginate(query, options).then((result) => {
expect(result.itemsList).to.have.length(10);

@@ -191,4 +226,4 @@ expect(result.itemsList[0].title).to.equal('Book #41');

expect(result.pageCounter).to.equal(41);
expect(result.hasPrevPage).to.equal(true);
expect(result.hasNextPage).to.equal(true);
expect(result.hasPrevious).to.equal(true);
expect(result.hasNext).to.equal(true);
expect(result.prev).to.equal(4);

@@ -199,7 +234,37 @@ expect(result.next).to.equal(6);

});
});
it('with custom Meta label', function () {
var query = {
title: {
$in: [/Book/i]
}
};
const myCustomLabels = {
meta: 'meta',
docs: 'itemsList',
totalDocs: 'total'
};
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.meta).to.be.an.instanceOf(Object);
expect(result.meta.total).to.equal(100);
});
});
after(function (done) {

@@ -206,0 +271,0 @@ mongoose.connection.db.dropDatabase(done);

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