mongoose-paginate-v2
Version:
A custom pagination library for Mongoose with customizable labels.
140 lines (132 loc) • 7.49 kB
JavaScript
"use strict";
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var PaginationParametersHelper = /*#__PURE__*/function () {
function PaginationParametersHelper(request) {
_classCallCheck(this, PaginationParametersHelper);
this.query = request.query;
}
/**
* Handle boolean options
* If the 'option'-Parameter is a string, check if it equals 'true'
* If not, it should be a boolean, and can be returned as it is.
*
* @param {string|boolean} option
* @return {boolean}
* */
return _createClass(PaginationParametersHelper, [{
key: "booleanOpt",
value: function booleanOpt(option) {
return typeof option === 'string' ? option === 'true' : option;
}
/**
* Handle options that are strings or objects (including arrays)
*
* @param {object|string} option
* @return {object|string}
* */
}, {
key: "optObjectOrString",
value: function optObjectOrString(option) {
// Since the JSON in the query object will be strings,
// we need to be able to detect this, in order to differentiate between JSON-objects and pure strings.
// a pure string, e.g. 'field -test', might not be parsed as wished by JSON.parse
var openingBrackets = ['{', '['];
var closingBrackets = ['}', ']'];
var firstCharIsBracket = option[0] && openingBrackets.includes(option[0]);
var lastCharIsBracket = option[option.length - 1] && closingBrackets.includes(option[option.length - 1]);
var optionIsObject = firstCharIsBracket && lastCharIsBracket;
try {
return optionIsObject ? JSON.parse(option) : option;
} catch (err) {
// Fallback for parsing errors of objects
return {};
}
}
/**
* Yields the "query" parameter for Model.paginate()
* given any attributes of the Express req.query-Object,
* */
}, {
key: "getQuery",
value: function getQuery() {
var _this$query;
var filtersQueryParameter = (_this$query = this.query) === null || _this$query === void 0 ? void 0 : _this$query.query;
if (!filtersQueryParameter) return {};
try {
return JSON.parse(filtersQueryParameter);
} catch (err) {
return {};
}
}
/**
* Yields the "options" parameter for Model.paginate(),
* given any attributes of the Express req.query-Object
* */
}, {
key: "getOptions",
value: function getOptions() {
if (!this.query) return {};
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,
leanWithVirtuals = this.query.leanWithVirtuals,
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);
if (collation) options['collation'] = this.optObjectOrString(collation);
if (sort) options['sort'] = this.optObjectOrString(sort);
if (populate) options['populate'] = this.optObjectOrString(populate);
if (projection !== undefined) options['projection'] = this.optObjectOrString(projection);
if (lean !== undefined) options['lean'] = this.booleanOpt(lean);
if (leanWithId !== undefined) options['leanWithId'] = this.booleanOpt(leanWithId);
if (leanWithVirtuals !== undefined) options['leanWithVirtuals'] = this.booleanOpt(leanWithVirtuals);
if (offset) options['offset'] = Number(offset);
if (page) options['page'] = Number(page);
if (limit || limit == 0) options['limit'] = Number(limit);
if (customLabels) options['customLabels'] = this.optObjectOrString(customLabels);
if (pagination !== undefined) options['pagination'] = this.booleanOpt(pagination);
if (useEstimatedCount !== undefined) options['useEstimatedCount'] = this.booleanOpt(useEstimatedCount);
if (useCustomCountFn !== undefined) options['useCustomCountFn'] = this.booleanOpt(useCustomCountFn);
if (forceCountFn !== undefined) options['forceCountFn'] = this.booleanOpt(forceCountFn);
if (allowDiskUse) options['allowDiskUse'] = this.booleanOpt(allowDiskUse);
if (read) options['read'] = this.optObjectOrString(read);
if (mongooseOptions) options['options'] = this.getOptions(mongooseOptions);
return options;
}
/**
* Yields an array with positions:
* [0] "query" parameter, for Model.paginate()
* [1] "options" parameter, for Model.paginate()
* */
}, {
key: "get",
value: function get() {
return [_objectSpread({}, this.getQuery()), _objectSpread({}, this.getOptions())];
}
}]);
}();
module.exports = PaginationParametersHelper;