mongoose-paginate-v2
Advanced tools
Comparing version 1.8.0 to 1.8.1
# Changelog | ||
## v1.8.1 | ||
[2024-05-17] | ||
- Fix #186 | ||
- Fix - Use default values for page and limit when passing undefined | ||
## v1.8.0 | ||
[2024-01-05] | ||
- Support for Mongoose v8 | ||
@@ -13,2 +22,4 @@ | ||
[2023-07-26] | ||
- Fix - set limit when limit is 0 | ||
@@ -15,0 +26,0 @@ |
"use strict"; | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
/** | ||
@@ -32,5 +29,3 @@ * @param {Object} [query={}] | ||
var PaginationParametersHelper = require('./pagination-parameters'); | ||
var paginateSubDocsHelper = require('./pagination-subdocs'); | ||
var defaultOptions = { | ||
@@ -64,3 +59,2 @@ customLabels: { | ||
}; | ||
function paginate(query, options, callback) { | ||
@@ -70,25 +64,21 @@ options = _objectSpread(_objectSpread(_objectSpread({}, defaultOptions), paginate.options), options); | ||
var _options = options, | ||
collation = _options.collation, | ||
lean = _options.lean, | ||
leanWithId = _options.leanWithId, | ||
populate = _options.populate, | ||
projection = _options.projection, | ||
read = _options.read, | ||
select = _options.select, | ||
sort = _options.sort, | ||
pagination = _options.pagination, | ||
useEstimatedCount = _options.useEstimatedCount, | ||
useCustomCountFn = _options.useCustomCountFn, | ||
forceCountFn = _options.forceCountFn, | ||
allowDiskUse = _options.allowDiskUse, | ||
customFind = _options.customFind; | ||
collation = _options.collation, | ||
lean = _options.lean, | ||
leanWithId = _options.leanWithId, | ||
populate = _options.populate, | ||
projection = _options.projection, | ||
read = _options.read, | ||
select = _options.select, | ||
sort = _options.sort, | ||
pagination = _options.pagination, | ||
useEstimatedCount = _options.useEstimatedCount, | ||
useCustomCountFn = _options.useCustomCountFn, | ||
forceCountFn = _options.forceCountFn, | ||
allowDiskUse = _options.allowDiskUse, | ||
customFind = _options.customFind; | ||
var customLabels = _objectSpread(_objectSpread({}, defaultOptions.customLabels), options.customLabels); | ||
var limit = defaultOptions.limit; | ||
if (pagination) { | ||
if (pagination && !isNaN(Number(options.limit))) { | ||
limit = parseInt(options.limit, 10) > 0 ? parseInt(options.limit, 10) : 0; | ||
} | ||
var isCallbackSpecified = typeof callback === 'function'; | ||
@@ -99,4 +89,5 @@ var findOptions = options.options; | ||
var skip; | ||
var docsPromise = []; // Labels | ||
var docsPromise = []; | ||
// Labels | ||
var labelDocs = customLabels.docs; | ||
@@ -113,3 +104,2 @@ var labelLimit = customLabels.limit; | ||
var labelMeta = customLabels.meta; | ||
if (Object.prototype.hasOwnProperty.call(options, 'offset')) { | ||
@@ -119,3 +109,3 @@ offset = parseInt(options.offset, 10); | ||
} else if (Object.prototype.hasOwnProperty.call(options, 'page')) { | ||
page = parseInt(options.page, 10) < 1 ? 1 : parseInt(options.page, 10); | ||
page = parseInt(options.page, 10) > 0 ? parseInt(options.page, 10) : 1; | ||
skip = (page - 1) * limit; | ||
@@ -127,17 +117,17 @@ } else { | ||
} | ||
if (!pagination) { | ||
page = 1; | ||
} | ||
var countPromise; | ||
var countPromise; // Only run count when pagination is enabled | ||
// Only run count when pagination is enabled | ||
if (pagination) { | ||
if (forceCountFn === true) { | ||
// Deprecated since starting from MongoDB Node.JS driver v3.1 | ||
// Hack for mongo < v3.4 | ||
if (Object.keys(collation).length > 0) { | ||
countPromise = this.countDocuments(query).collation(collation).exec(); | ||
countPromise = this.countDocuments(query, findOptions).collation(collation).exec(); | ||
} else { | ||
countPromise = this.countDocuments(query).exec(); | ||
countPromise = this.countDocuments(query, findOptions).exec(); | ||
} | ||
@@ -152,5 +142,5 @@ } else { | ||
if (Object.keys(collation).length > 0) { | ||
countPromise = this.countDocuments(query).collation(collation).exec(); | ||
countPromise = this.countDocuments(query, findOptions).collation(collation).exec(); | ||
} else { | ||
countPromise = this.countDocuments(query).exec(); | ||
countPromise = this.countDocuments(query, findOptions).exec(); | ||
} | ||
@@ -160,14 +150,10 @@ } | ||
} | ||
if (limit) { | ||
var mQuery = this[customFind](query, projection, findOptions); | ||
if (populate) { | ||
mQuery.populate(populate); | ||
} | ||
mQuery.select(select); | ||
mQuery.sort(sort); | ||
mQuery.lean(lean); | ||
if (read && read.pref) { | ||
@@ -180,9 +166,8 @@ /** | ||
mQuery.read(read.pref, read.tags); | ||
} // Hack for mongo < v3.4 | ||
} | ||
// Hack for mongo < v3.4 | ||
if (Object.keys(collation).length > 0) { | ||
mQuery.collation(collation); | ||
} | ||
if (pagination) { | ||
@@ -192,3 +177,2 @@ mQuery.skip(skip); | ||
} | ||
try { | ||
@@ -201,5 +185,3 @@ if (allowDiskUse === true) { | ||
} | ||
docsPromise = mQuery.exec(); | ||
if (lean && leanWithId) { | ||
@@ -216,11 +198,8 @@ docsPromise = docsPromise.then(function (docs) { | ||
} | ||
return Promise.all([countPromise, docsPromise]).then(function (values) { | ||
var count = values[0]; | ||
var docs = values[1]; | ||
if (pagination !== true) { | ||
count = docs.length; | ||
} | ||
var meta = { | ||
@@ -230,3 +209,2 @@ [labelTotal]: count | ||
var result = {}; | ||
if (typeof offset !== 'undefined') { | ||
@@ -236,5 +214,5 @@ meta.offset = offset; | ||
} | ||
var pages = limit > 0 ? Math.ceil(count / limit) || 1 : null; | ||
var pages = limit > 0 ? Math.ceil(count / limit) || 1 : null; // Setting default values | ||
// Setting default values | ||
meta[labelLimit] = count; | ||
@@ -248,7 +226,7 @@ meta[labelTotalPages] = 1; | ||
meta[labelNextPage] = null; | ||
if (pagination) { | ||
meta[labelLimit] = limit; | ||
meta[labelTotalPages] = pages; // Set prev page | ||
meta[labelTotalPages] = pages; | ||
// Set prev page | ||
if (page > 1) { | ||
@@ -260,5 +238,5 @@ meta[labelHasPrevPage] = true; | ||
meta[labelPrevPage] = 1; | ||
} // Set next page | ||
} | ||
// Set next page | ||
if (page < pages) { | ||
@@ -268,7 +246,6 @@ meta[labelHasNextPage] = true; | ||
} | ||
} // Remove customLabels set to false | ||
} | ||
// Remove customLabels set to false | ||
delete meta['false']; | ||
if (limit == 0) { | ||
@@ -284,3 +261,2 @@ meta[labelLimit] = 0; | ||
} | ||
if (labelMeta) { | ||
@@ -296,3 +272,2 @@ result = { | ||
} | ||
return isCallbackSpecified ? callback(null, result) : Promise.resolve(result); | ||
@@ -303,7 +278,6 @@ }).catch(function (error) { | ||
} | ||
/** | ||
* @param {Schema} schema | ||
*/ | ||
module.exports = function (schema) { | ||
@@ -313,5 +287,4 @@ schema.statics.paginate = paginate; | ||
}; | ||
module.exports.PaginationParameters = PaginationParametersHelper; | ||
module.exports.paginateSubDocs = paginateSubDocsHelper; | ||
module.exports.paginate = paginate; |
"use strict"; | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
var PaginationParametersHelper = /*#__PURE__*/function () { | ||
function PaginationParametersHelper(request) { | ||
_classCallCheck(this, PaginationParametersHelper); | ||
this.query = request.query; | ||
} | ||
/** | ||
@@ -29,4 +23,2 @@ * Handle boolean options | ||
* */ | ||
_createClass(PaginationParametersHelper, [{ | ||
@@ -37,2 +29,3 @@ key: "booleanOpt", | ||
} | ||
/** | ||
@@ -44,3 +37,2 @@ * Handle options that are strings or objects (including arrays) | ||
* */ | ||
}, { | ||
@@ -57,3 +49,2 @@ key: "optObjectOrString", | ||
var optionIsObject = firstCharIsBracket && lastCharIsBracket; | ||
try { | ||
@@ -66,2 +57,3 @@ return optionIsObject ? JSON.parse(option) : option; | ||
} | ||
/** | ||
@@ -71,3 +63,2 @@ * Yields the "query" parameter for Model.paginate() | ||
* */ | ||
}, { | ||
@@ -77,6 +68,4 @@ key: "getQuery", | ||
var _this$query; | ||
var filtersQueryParameter = (_this$query = this.query) === null || _this$query === void 0 ? void 0 : _this$query.query; | ||
if (!filtersQueryParameter) return {}; | ||
try { | ||
@@ -88,2 +77,3 @@ return JSON.parse(filtersQueryParameter); | ||
} | ||
/** | ||
@@ -93,3 +83,2 @@ * Yields the "options" parameter for Model.paginate(), | ||
* */ | ||
}, { | ||
@@ -99,23 +88,25 @@ key: "getOptions", | ||
if (!this.query) return {}; | ||
var options = {}; // Instantiate variables with all the possible options for Model.paginate() | ||
var options = {}; | ||
// Instantiate variables with all the possible options for Model.paginate() | ||
var select = this.query.select, | ||
collation = this.query.collation, | ||
sort = this.query.sort, | ||
populate = this.query.populate, | ||
projection = this.query.projection, | ||
lean = this.query.lean, | ||
leanWithId = this.query.leanWithId, | ||
offset = this.query.offset, | ||
page = this.query.page, | ||
limit = this.query.limit, | ||
customLabels = this.query.customLabels, | ||
pagination = this.query.pagination, | ||
useEstimatedCount = this.query.useEstimatedCount, | ||
useCustomCountFn = this.query.useCustomCountFn, | ||
forceCountFn = this.query.forceCountFn, | ||
allowDiskUse = this.query.allowDiskUse, | ||
read = this.query.read, | ||
mongooseOptions = this.query.options; // For every option that is set, add it to the 'options' object-literal | ||
collation = this.query.collation, | ||
sort = this.query.sort, | ||
populate = this.query.populate, | ||
projection = this.query.projection, | ||
lean = this.query.lean, | ||
leanWithId = this.query.leanWithId, | ||
offset = this.query.offset, | ||
page = this.query.page, | ||
limit = this.query.limit, | ||
customLabels = this.query.customLabels, | ||
pagination = this.query.pagination, | ||
useEstimatedCount = this.query.useEstimatedCount, | ||
useCustomCountFn = this.query.useCustomCountFn, | ||
forceCountFn = this.query.forceCountFn, | ||
allowDiskUse = this.query.allowDiskUse, | ||
read = this.query.read, | ||
mongooseOptions = this.query.options; | ||
// For every option that is set, add it to the 'options' object-literal | ||
if (select) options['select'] = this.optObjectOrString(select); | ||
@@ -141,2 +132,3 @@ if (collation) options['collation'] = this.optObjectOrString(collation); | ||
} | ||
/** | ||
@@ -147,3 +139,2 @@ * Yields an array with positions: | ||
* */ | ||
}, { | ||
@@ -155,6 +146,4 @@ key: "get", | ||
}]); | ||
return PaginationParametersHelper; | ||
}(); | ||
module.exports = PaginationParametersHelper; |
"use strict"; | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
/** | ||
@@ -28,11 +25,9 @@ * Pagination process for sub-documents | ||
var populate = option.populate, | ||
_option$page = option.page, | ||
page = _option$page === void 0 ? 1 : _option$page, | ||
_option$limit = option.limit, | ||
limit = _option$limit === void 0 ? 10 : _option$limit; | ||
_option$page = option.page, | ||
page = _option$page === void 0 ? 1 : _option$page, | ||
_option$limit = option.limit, | ||
limit = _option$limit === void 0 ? 10 : _option$limit; | ||
if (!populate) { | ||
throw new Error('populate is required'); | ||
} | ||
var offset = (page - 1) * limit; | ||
@@ -44,3 +39,2 @@ option.offset = offset; | ||
}; | ||
if (typeof populate === 'string') { | ||
@@ -53,10 +47,9 @@ populate = _objectSpread({ | ||
} | ||
option.populate = populate; | ||
return populate; | ||
} | ||
function populateResult(result, populate) { | ||
return result.populate(populate); | ||
} | ||
/** | ||
@@ -68,22 +61,19 @@ * Convert result of sub-docs list to pagination like docs | ||
*/ | ||
function constructDocs(paginatedResult, option) { | ||
var populate = option.populate, | ||
_option$offset = option.offset, | ||
offset = _option$offset === void 0 ? 0 : _option$offset, | ||
_option$page2 = option.page, | ||
page = _option$page2 === void 0 ? 1 : _option$page2, | ||
_option$limit2 = option.limit, | ||
limit = _option$limit2 === void 0 ? 10 : _option$limit2; | ||
_option$offset = option.offset, | ||
offset = _option$offset === void 0 ? 0 : _option$offset, | ||
_option$page2 = option.page, | ||
page = _option$page2 === void 0 ? 1 : _option$page2, | ||
_option$limit2 = option.limit, | ||
limit = _option$limit2 === void 0 ? 10 : _option$limit2; | ||
var path = populate.path; | ||
var count = option.count; | ||
var paginatedDocs = paginatedResult[path]; | ||
if (!paginatedDocs) { | ||
throw new Error(`Parse error! Cannot find key on result with path ${path}`); | ||
} | ||
page = Math.ceil((offset + 1) / limit); | ||
page = Math.ceil((offset + 1) / limit); // set default meta | ||
// set default meta | ||
var meta = { | ||
@@ -101,4 +91,5 @@ docs: paginatedDocs, | ||
meta.totalPages = totalPages; | ||
meta.pagingCounter = (page - 1) * limit + 1; // Set prev page | ||
meta.pagingCounter = (page - 1) * limit + 1; | ||
// Set prev page | ||
if (page > 1) { | ||
@@ -110,5 +101,5 @@ meta.hasPrevPage = true; | ||
meta.prevPage = 1; | ||
} // Set next page | ||
} | ||
// Set next page | ||
if (page < totalPages) { | ||
@@ -118,3 +109,2 @@ meta.hasNextPage = true; | ||
} | ||
if (limit == 0) { | ||
@@ -126,3 +116,2 @@ meta.limit = 0; | ||
} | ||
Object.defineProperty(paginatedResult, path, { | ||
@@ -132,16 +121,15 @@ value: meta, | ||
}); | ||
} // options properties for main document query | ||
} | ||
// options properties for main document query | ||
var populate = options.populate, | ||
_options$read = options.read, | ||
read = _options$read === void 0 ? {} : _options$read, | ||
_options$select = options.select, | ||
select = _options$select === void 0 ? '' : _options$select, | ||
_options$pagination = options.pagination, | ||
pagination = _options$pagination === void 0 ? true : _options$pagination, | ||
pagingOptions = options.pagingOptions, | ||
projection = options.projection; | ||
_options$read = options.read, | ||
read = _options$read === void 0 ? {} : _options$read, | ||
_options$select = options.select, | ||
select = _options$select === void 0 ? '' : _options$select, | ||
_options$pagination = options.pagination, | ||
pagination = _options$pagination === void 0 ? true : _options$pagination, | ||
pagingOptions = options.pagingOptions, | ||
projection = options.projection; | ||
var mQuery = this.findOne(query, projection); | ||
if (read && read.pref) { | ||
@@ -155,15 +143,11 @@ /** | ||
} | ||
if (select) { | ||
mQuery.select(select); | ||
} | ||
return new Promise(function (resolve, reject) { | ||
mQuery.exec().then(function (result) { | ||
var newPopulate = []; | ||
if (populate) { | ||
newPopulate.push(newPopulate); | ||
} | ||
if (pagination && pagingOptions) { | ||
@@ -178,3 +162,2 @@ if (Array.isArray(pagingOptions)) { | ||
var _populate = getSubDocsPopulate(pagingOptions); | ||
pagingOptions.count = result[_populate.path].length; | ||
@@ -184,3 +167,2 @@ newPopulate.push(_populate); | ||
} | ||
populateResult(result, newPopulate).then(function (paginatedResult) { | ||
@@ -197,3 +179,2 @@ // convert paginatedResult to pagination docs | ||
} | ||
callback && callback(null, paginatedResult); | ||
@@ -209,3 +190,2 @@ resolve(paginatedResult); | ||
} | ||
module.exports = paginateSubDocs; |
{ | ||
"name": "mongoose-paginate-v2", | ||
"version": "1.8.0", | ||
"version": "1.8.1", | ||
"description": "A custom pagination library for Mongoose with customizable labels.", | ||
@@ -85,3 +85,3 @@ "main": "dist/index.js", | ||
"lint-staged": "^10.5.4", | ||
"mocha": "^8.4.0", | ||
"mocha": "^10.4.0", | ||
"mongoose": "^8.0.0", | ||
@@ -88,0 +88,0 @@ "prettier": "^2.3.2", |
@@ -87,3 +87,3 @@ /** | ||
if (pagination) { | ||
if (pagination && !isNaN(Number(options.limit))) { | ||
limit = parseInt(options.limit, 10) > 0 ? parseInt(options.limit, 10) : 0; | ||
@@ -118,3 +118,3 @@ } | ||
} else if (Object.prototype.hasOwnProperty.call(options, 'page')) { | ||
page = parseInt(options.page, 10) < 1 ? 1 : parseInt(options.page, 10); | ||
page = parseInt(options.page, 10) > 0 ? parseInt(options.page, 10) : 1; | ||
skip = (page - 1) * limit; | ||
@@ -140,5 +140,7 @@ } else { | ||
if (Object.keys(collation).length > 0) { | ||
countPromise = this.countDocuments(query).collation(collation).exec(); | ||
countPromise = this.countDocuments(query, findOptions) | ||
.collation(collation) | ||
.exec(); | ||
} else { | ||
countPromise = this.countDocuments(query).exec(); | ||
countPromise = this.countDocuments(query, findOptions).exec(); | ||
} | ||
@@ -153,5 +155,7 @@ } else { | ||
if (Object.keys(collation).length > 0) { | ||
countPromise = this.countDocuments(query).collation(collation).exec(); | ||
countPromise = this.countDocuments(query, findOptions) | ||
.collation(collation) | ||
.exec(); | ||
} else { | ||
countPromise = this.countDocuments(query).exec(); | ||
countPromise = this.countDocuments(query, findOptions).exec(); | ||
} | ||
@@ -158,0 +162,0 @@ } |
@@ -746,2 +746,28 @@ 'use strict'; | ||
}); | ||
it('pagination=default, limit/page=undefined -> return first 10', function () { | ||
var query = { | ||
title: { | ||
$in: [/Book/i], | ||
}, | ||
}; | ||
var options = { | ||
page: undefined, | ||
limit: undefined, | ||
}; | ||
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(1); | ||
expect(result.pagingCounter).to.equal(1); | ||
expect(result.hasPrevPage).to.equal(false); | ||
expect(result.hasNextPage).to.equal(true); | ||
expect(result.prevPage).to.equal(null); | ||
expect(result.nextPage).to.equal(2); | ||
expect(result.totalPages).to.equal(10); | ||
}); | ||
}); | ||
}); | ||
@@ -748,0 +774,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
327033
25
1992