typeorm-paginator
Version:
TypeORM query builder pagination library.
272 lines (271 loc) • 12.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.PagePaginator = void 0;
var _regeneratorRuntime = _interopRequireDefault(require("regenerator-runtime"));
var _normalizeOrderBy = require("./utils/normalizeOrderBy");
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
try {
var info = gen[key](arg);
var value = info.value;
} catch (error) {
reject(error);
return;
}
if (info.done) {
resolve(value);
} else {
Promise.resolve(value).then(_next, _throw);
}
}
function _asyncToGenerator(fn) {
return function() {
var self = this, args = arguments;
return new Promise(function(resolve, reject) {
var gen = fn.apply(self, args);
function _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
}
_next(undefined);
});
};
}
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;
}
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function _iterableToArrayLimit(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for(var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true){
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally{
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally{
if (_d) throw _e;
}
}
return _arr;
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
}
var PagePaginator = /*#__PURE__*/ function() {
"use strict";
function PagePaginator(entity, param) {
var orderBy = param.orderBy, columnNames = param.columnNames, take = param.take;
_classCallCheck(this, PagePaginator);
this.entity = entity;
this.orderBy = orderBy;
this.columnNames = columnNames !== null && columnNames !== void 0 ? columnNames : {
};
var ref, ref1, ref2;
this.takeOptions = typeof take === 'number' ? {
default: take,
min: 0,
max: Infinity
} : {
default: (ref = take === null || take === void 0 ? void 0 : take.default) !== null && ref !== void 0 ? ref : 20,
min: Math.max(0, (ref1 = take === null || take === void 0 ? void 0 : take.min) !== null && ref1 !== void 0 ? ref1 : 0),
max: (ref2 = take === null || take === void 0 ? void 0 : take.max) !== null && ref2 !== void 0 ? ref2 : Infinity
};
}
_createClass(PagePaginator, [
{
key: "paginate",
value: function paginate(qb) {
var params = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {
};
return _asyncToGenerator(_regeneratorRuntime.default.mark(function _callee() {
var _page, page, take, qbForCount, _orderBy, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, key, value, _key, hasNext, nodes1;
return _regeneratorRuntime.default.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
;
page = Math.max((_page = params.page) !== null && _page !== void 0 ? _page : 1, 1);
take = Math.max(this.takeOptions.min, Math.min(params.take || this.takeOptions.default, this.takeOptions.max));
qbForCount = qb.clone();
;
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
_ctx.prev = 6;
for(_iterator = (0, _normalizeOrderBy).normalizeOrderBy((_orderBy = params.orderBy) !== null && _orderBy !== void 0 ? _orderBy : this.orderBy)[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
_value = _slicedToArray(_step.value, 2), key = _value[0], value = _value[1];
;
qb.addOrderBy((_key = this.columnNames[key]) !== null && _key !== void 0 ? _key : "".concat(qb.alias, ".").concat(key), value ? 'ASC' : 'DESC');
}
_ctx.next = 14;
break;
case 10:
_ctx.prev = 10;
_ctx.t0 = _ctx["catch"](6);
_didIteratorError = true;
_iteratorError = _ctx.t0;
case 14:
_ctx.prev = 14;
_ctx.prev = 15;
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
case 17:
_ctx.prev = 17;
if (!_didIteratorError) {
_ctx.next = 20;
break;
}
throw _iteratorError;
case 20:
return _ctx.finish(17);
case 21:
return _ctx.finish(14);
case 22:
hasNext = false;
_ctx.next = 25;
return qb.clone().offset((page - 1) * take).limit(take + 1).getMany().then(function(nodes) {
if (nodes.length > take) {
hasNext = true;
}
return nodes.slice(0, take);
});
case 25:
nodes1 = _ctx.sent;
_ctx.next = 28;
return qbForCount.getCount();
case 28:
_ctx.t1 = _ctx.sent;
_ctx.t2 = nodes1;
_ctx.t3 = hasNext;
return _ctx.abrupt("return", {
count: _ctx.t1,
nodes: _ctx.t2,
hasNext: _ctx.t3
});
case 32:
case "end":
return _ctx.stop();
}
}, _callee, this, [
[
6,
10,
14,
22
],
[
15,
,
17,
21
]
]);
}).bind(this))();
}
},
{
key: "promisePaginate",
value: function promisePaginate(qb) {
var params = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {
};
var _page;
var page = Math.max((_page = params.page) !== null && _page !== void 0 ? _page : 1, 1);
var take = Math.max(this.takeOptions.min, Math.min(params.take || this.takeOptions.default, this.takeOptions.max));
var qbForCount = qb.clone();
var _orderBy;
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
try {
for(var _iterator = (0, _normalizeOrderBy).normalizeOrderBy((_orderBy = params.orderBy) !== null && _orderBy !== void 0 ? _orderBy : this.orderBy)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
var _value = _slicedToArray(_step.value, 2), key = _value[0], value = _value[1];
var _key;
qb.addOrderBy((_key = this.columnNames[key]) !== null && _key !== void 0 ? _key : "".concat(qb.alias, ".").concat(key), value ? 'ASC' : 'DESC');
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally{
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally{
if (_didIteratorError) {
throw _iteratorError;
}
}
}
var cachePromiseNodes = null;
var promiseNodes = function() {
if (!cachePromiseNodes) {
cachePromiseNodes = qb.clone().offset((page - 1) * take).limit(take + 1).getMany().then(function(nodes) {
var hasNext = false;
if (nodes.length > take) {
hasNext = true;
}
return {
hasNext: hasNext,
nodes: nodes.slice(0, take)
};
});
}
return cachePromiseNodes;
};
return {
get count () {
return qbForCount.getCount();
},
get nodes () {
return promiseNodes().then(function(param) {
var nodes = param.nodes;
return nodes;
});
},
get hasNext () {
return promiseNodes().then(function(param) {
var hasNext = param.hasNext;
return hasNext;
});
}
};
}
}
]);
return PagePaginator;
}();
exports.PagePaginator = PagePaginator;