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.0.15 to 1.0.16

232

index.js

@@ -1,16 +0,14 @@

var Promise = require('bluebird');
/**
* @param {Object} [query={}]
* @param {Object} [options={}]
* @param {Object|String} [options.select]
* @param {Object|String} [options.sort]
* @param {Object|String} [options.customLabels]
* @param {Object|} [options.collation]
* @param {Array|Object|String} [options.populate]
* @param {Boolean} [options.lean=false]
* @param {Boolean} [options.leanWithId=true]
* @param {Number} [options.offset=0] - Use offset or page to set skip position
* @param {Number} [options.page=1]
* @param {Number} [options.limit=10]
* @param {Object|String} [options.select]
* @param {Object|String} [options.sort]
* @param {Object|String} [options.customLabels]
* @param {Object|} [options.collation]
* @param {Array|Object|String} [options.populate]
* @param {Boolean} [options.lean=false]
* @param {Boolean} [options.leanWithId=true]
* @param {Number} [options.offset=0] - Use offset or page to set skip position
* @param {Number} [options.page=1]
* @param {Number} [options.limit=10]
* @param {Function} [callback]

@@ -20,113 +18,133 @@ *

*/
function paginate(query, options, callback) {
query = query || {};
options = Object.assign({}, paginate.options, options);
options.customLabels = options.customLabels ? options.customLabels : {};
var select = options.select;
var sort = options.sort;
var collation = options.collation;
var populate = options.populate;
var lean = options.lean || false;
var leanWithId = options.hasOwnProperty('leanWithId') ? options.leanWithId : true;
query = query || {};
options = Object.assign({}, paginate.options, options);
options.customLabels = options.customLabels ? options.customLabels : {};
var limit = options.hasOwnProperty('limit') ? options.limit : 10;
var skip, offset, page;
var defaultLimit = 10;
// Custom Labels
var labelTotal = options.customLabels.totalDocs ? options.customLabels.totalDocs : 'totalDocs';
var labelLimit = options.customLabels.limit ? options.customLabels.limit : 'limit';
var labelPage = options.customLabels.page ? options.customLabels.page : 'page';
var labelTotalPages = options.customLabels.totalPages ? options.customLabels.totalPages : 'totalPages';
var labelDocs = options.customLabels.docs ? options.customLabels.docs : 'docs';
var labelNextPage = options.customLabels.nextPage ? options.customLabels.nextPage : 'nextPage';
var labelPrevPage = options.customLabels.prevPage ? options.customLabels.prevPage : 'prevPage';
var select = options.select;
var sort = options.sort;
var collation = options.collation || {};
var populate = options.populate;
var lean = options.lean || false;
var leanWithId = options.hasOwnProperty('leanWithId') ? options.leanWithId : true;
var limit = options.hasOwnProperty('limit') ? options.limit : defaultLimit;
var skip;
var offset;
var page;
if (options.hasOwnProperty('offset')) {
offset = options.offset;
skip = offset;
} else if (options.hasOwnProperty('page')) {
page = options.page;
skip = (page - 1) * limit;
} else {
offset = 0;
page = 1;
skip = offset;
}
// Custom Labels
var labelTotal = options.customLabels.totalDocs ? options.customLabels.totalDocs : 'totalDocs';
var labelLimit = options.customLabels.limit ? options.customLabels.limit : 'limit';
var labelPage = options.customLabels.page ? options.customLabels.page : 'page';
var labelTotalPages = options.customLabels.totalPages ? options.customLabels.totalPages : 'totalPages';
var labelDocs = options.customLabels.docs ? options.customLabels.docs : 'docs';
var labelNextPage = options.customLabels.nextPage ? options.customLabels.nextPage : 'nextPage';
var labelPrevPage = options.customLabels.prevPage ? options.customLabels.prevPage : 'prevPage';
var promises = {
docs: Promise.resolve([]),
count: this.countDocuments(query).exec()
};
if (options.hasOwnProperty('offset')) {
offset = parseInt(options.offset);
skip = offset;
} else if (options.hasOwnProperty('page')) {
page = parseInt(options.page);
skip = (page - 1) * limit;
} else {
offset = 0;
page = 1;
skip = offset;
}
if (limit) {
var query = this.find(query)
.select(select)
.sort(sort)
.collation(collation)
.skip(skip)
.limit(limit)
.lean(lean);
const count = this.countDocuments(query).exec();
if (populate) {
[].concat(populate).forEach(function(item) {
query.populate(item);
});
}
const model = this.find(query);
model.select(select);
model.sort(sort);
model.lean(lean);
promises.docs = query.exec();
// Hack for mongo < v3.4
if (Object.keys(collation).length > 0) {
model.collation(collation);
}
if (lean && leanWithId) {
promises.docs = promises.docs.then(function(docs) {
docs.forEach(function(doc) {
doc.id = String(doc._id);
});
if (limit) {
model.skip(skip);
model.limit(limit);
}
return docs;
});
}
}
if (populate) {
model.populate(populate);
}
return Promise.props(promises)
.then(function(data) {
var result = {
[labelDocs]: data.docs,
[labelTotal]: data.count,
[labelLimit]: limit
};
var docs = model.exec();
if (offset !== undefined) {
result.offset = offset;
}
if (lean && leanWithId) {
docs = docs.then(function (docs) {
docs.forEach(function (doc) {
doc.id = String(doc._id);
});
return docs;
});
}
if (page !== undefined) {
return Promise.all([count, docs])
.then(function (values) {
const pages = Math.ceil(data.count / limit) || 1;
var result = {
[labelDocs]: values[1],
[labelTotal]: values[0],
[labelLimit]: limit
};
result.hasPrevPage = false;
result.hasNextPage = false;
if (offset !== undefined) {
result.offset = offset;
}
result[labelPage] = page;
result[labelTotalPages] = pages;
// Set prev page
if(page > 1) {
result.hasPrevPage = true;
result[labelPrevPage] = (page - 1);
} else {
result[labelPrevPage] = null;
}
if (page !== undefined) {
// Set next page
if(page < pages) {
result.hasNextPage = true;
result[labelNextPage] = (page + 1);
} else {
result[labelNextPage] = null;
}
}
const pages = Math.ceil(values[0] / limit) || 1;
return result;
})
.asCallback(callback);
result.hasPrevPage = false;
result.hasNextPage = false;
result[labelPage] = page;
result[labelTotalPages] = pages;
// Set prev page
if (page > 1) {
result.hasPrevPage = true;
result[labelPrevPage] = (page - 1);
} else {
result[labelPrevPage] = null;
}
// Set next page
if (page < pages) {
result.hasNextPage = true;
result[labelNextPage] = (page + 1);
} else {
result[labelNextPage] = null;
}
}
// Adding support for callbacks if specified.
if (callback) {
process.emitWarning(
'mongoose-paginate-v2: callback will be removed from future versions, use promise instead.',
'DeprecationWarning'
);
return callback(null, result);
} else {
return Promise.resolve(result);
}
}).catch(function (error) {
if (callback) {
return callback(error);
} else {
return Promise.reject(error);
}
});
}

@@ -137,6 +155,6 @@

*/
module.exports = function(schema) {
schema.statics.paginate = paginate;
module.exports = function (schema) {
schema.statics.paginate = paginate;
};
module.exports.paginate = paginate;
module.exports.paginate = paginate;
{
"name": "mongoose-paginate-v2",
"version": "1.0.15",
"version": "1.0.16",
"description": "A cursor based custom pagination library for Mongoose with customizable labels.",

@@ -34,5 +34,3 @@ "main": "index.js",

"homepage": "https://github.com/aravindnc/mongoose-paginate-v2#readme",
"dependencies": {
"bluebird": "3.5.1"
},
"dependencies": {},
"devDependencies": {

@@ -39,0 +37,0 @@ "babel-cli": "^6.26.0",

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