typeorm-paginator
Version:
TypeORM query builder pagination library.
794 lines (793 loc) • 30.3 kB
JavaScript
"use strict";
var _regeneratorRuntime = _interopRequireDefault(require("regenerator-runtime"));
var _typeorm = require("typeorm");
var _pagePaginator = require("./page-paginator");
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
var desc = {
};
Object.keys(descriptor).forEach(function(key) {
desc[key] = descriptor[key];
});
desc.enumerable = !!desc.enumerable;
desc.configurable = !!desc.configurable;
if ("value" in desc || desc.initializer) {
desc.writable = true;
}
desc = decorators.slice().reverse().reduce(function(desc, decorator) {
return decorator ? decorator(target, property, desc) || desc : desc;
}, desc);
if (context && desc.initializer !== void 0) {
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
desc.initializer = undefined;
}
if (desc.initializer === void 0) {
Object.defineProperty(target, property, desc);
desc = null;
}
return desc;
}
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 _initializerDefineProperty(target, property, descriptor, context) {
if (!descriptor) return;
Object.defineProperty(target, property, {
enumerable: descriptor.enumerable,
configurable: descriptor.configurable,
writable: descriptor.writable,
value: descriptor.initializer ? descriptor.initializer.call(context) : void 0
});
}
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
var _class, _descriptor, _dec, _descriptor1, _dec1;
var _dec2 = (0, _typeorm).Entity({
name: 'users'
});
var User = _class = _dec2(((_class = function User1() {
"use strict";
_classCallCheck(this, User1);
_initializerDefineProperty(this, "id", _descriptor, this);
_initializerDefineProperty(this, "name", _descriptor1, this);
}) || _class, _dec = (0, _typeorm).PrimaryGeneratedColumn(), _dec1 = (0, _typeorm).Column({
type: String,
name: 'user_name'
}), _descriptor = _applyDecoratedDescriptor(_class.prototype, "id", [
_dec
], {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
}), _descriptor1 = _applyDecoratedDescriptor(_class.prototype, "name", [
_dec1
], {
configurable: true,
enumerable: true,
writable: true,
initializer: void 0
}), _class)) || _class;
function testPromisePaginationAndResolve(pagination) {
expect(pagination.count).toBeInstanceOf(Promise);
expect(pagination.hasNext).toBeInstanceOf(Promise);
expect(pagination.nodes).toBeInstanceOf(Promise);
return Promise.resolve().then(_asyncToGenerator(_regeneratorRuntime.default.mark(function _callee() {
return _regeneratorRuntime.default.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
_ctx.next = 2;
return pagination.count;
case 2:
_ctx.t0 = _ctx.sent;
_ctx.next = 5;
return pagination.nodes;
case 5:
_ctx.t1 = _ctx.sent;
_ctx.next = 8;
return pagination.hasNext;
case 8:
_ctx.t2 = _ctx.sent;
return _ctx.abrupt("return", {
count: _ctx.t0,
nodes: _ctx.t1,
hasNext: _ctx.t2
});
case 10:
case "end":
return _ctx.stop();
}
}, _callee);
})));
}
describe('testsuite of page-paginator', function() {
var connection;
beforeAll(_asyncToGenerator(_regeneratorRuntime.default.mark(function _callee() {
return _regeneratorRuntime.default.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
_ctx.next = 2;
return (0, _typeorm).createConnection({
type: 'sqlite',
database: ':memory:',
entities: [
User,
],
synchronize: true
});
case 2:
connection = _ctx.sent;
case 3:
case "end":
return _ctx.stop();
}
}, _callee);
})));
beforeEach(_asyncToGenerator(_regeneratorRuntime.default.mark(function _callee() {
return _regeneratorRuntime.default.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
_ctx.next = 2;
return connection.getRepository(User).clear();
case 2:
case "end":
return _ctx.stop();
}
}, _callee);
})));
it('test paginate default', _asyncToGenerator(_regeneratorRuntime.default.mark(function _callee() {
var repoUsers, nodes, paginator, pagination;
return _regeneratorRuntime.default.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
repoUsers = connection.getRepository(User);
nodes = [
repoUsers.create({
name: 'a'
}),
repoUsers.create({
name: 'b'
}),
repoUsers.create({
name: 'b'
}),
repoUsers.create({
name: 'c'
}),
repoUsers.create({
name: 'c'
}),
repoUsers.create({
name: 'c'
}),
];
_ctx.next = 4;
return repoUsers.save(nodes);
case 4:
paginator = new _pagePaginator.PagePaginator(User, {
orderBy: {
id: false
}
});
_ctx.next = 7;
return paginator.paginate(repoUsers.createQueryBuilder());
case 7:
pagination = _ctx.sent;
expect(pagination).toEqual({
count: 6,
nodes: [
nodes[5],
nodes[4],
nodes[3],
nodes[2],
nodes[1],
nodes[0],
],
hasNext: false
});
case 9:
case "end":
return _ctx.stop();
}
}, _callee);
})));
it('test page paginate by single-order', _asyncToGenerator(_regeneratorRuntime.default.mark(function _callee() {
var repoUsers, nodes, paginator, pagination, paginationNext, paginationNextNext;
return _regeneratorRuntime.default.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
repoUsers = connection.getRepository(User);
nodes = [
repoUsers.create({
name: 'a'
}),
repoUsers.create({
name: 'b'
}),
repoUsers.create({
name: 'b'
}),
repoUsers.create({
name: 'c'
}),
repoUsers.create({
name: 'c'
}),
repoUsers.create({
name: 'c'
}),
];
_ctx.next = 4;
return repoUsers.save(nodes);
case 4:
paginator = new _pagePaginator.PagePaginator(User, {
orderBy: {
id: false
},
take: 3
});
_ctx.next = 7;
return paginator.paginate(repoUsers.createQueryBuilder());
case 7:
pagination = _ctx.sent;
expect(pagination).toEqual({
count: 6,
nodes: [
nodes[5],
nodes[4],
nodes[3],
],
hasNext: true
});
_ctx.next = 11;
return paginator.paginate(repoUsers.createQueryBuilder(), {
page: 2
});
case 11:
paginationNext = _ctx.sent;
expect(paginationNext).toEqual({
count: 6,
nodes: [
nodes[2],
nodes[1],
nodes[0],
],
hasNext: false
});
_ctx.next = 15;
return paginator.paginate(repoUsers.createQueryBuilder(), {
page: 3
});
case 15:
paginationNextNext = _ctx.sent;
expect(paginationNextNext).toEqual({
count: 6,
nodes: [],
hasNext: false
});
case 17:
case "end":
return _ctx.stop();
}
}, _callee);
})));
it('test page paginate by multi-orders', _asyncToGenerator(_regeneratorRuntime.default.mark(function _callee() {
var repoUsers, nodes, paginator, pagination1, pagination2, pagination2Next, pagination2NextNext;
return _regeneratorRuntime.default.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
repoUsers = connection.getRepository(User);
nodes = [
repoUsers.create({
name: 'c'
}),
repoUsers.create({
name: 'b'
}),
repoUsers.create({
name: 'a'
}),
repoUsers.create({
name: 'c'
}),
repoUsers.create({
name: 'b'
}),
repoUsers.create({
name: 'c'
}),
];
_ctx.next = 4;
return repoUsers.save(nodes);
case 4:
paginator = new _pagePaginator.PagePaginator(User, {
orderBy: [
{
name: true
},
{
id: false
},
]
});
_ctx.next = 7;
return paginator.paginate(repoUsers.createQueryBuilder());
case 7:
pagination1 = _ctx.sent;
expect(pagination1).toEqual({
count: 6,
nodes: [
nodes[2],
nodes[4],
nodes[1],
nodes[5],
nodes[3],
nodes[0],
],
hasNext: false
});
_ctx.next = 11;
return paginator.paginate(repoUsers.createQueryBuilder(), {
take: 2
});
case 11:
pagination2 = _ctx.sent;
expect(pagination2).toEqual({
count: 6,
nodes: [
nodes[2],
nodes[4],
],
hasNext: true
});
_ctx.next = 15;
return paginator.paginate(repoUsers.createQueryBuilder(), {
take: 2,
page: 2
});
case 15:
pagination2Next = _ctx.sent;
expect(pagination2Next).toEqual({
count: 6,
nodes: [
nodes[1],
nodes[5],
],
hasNext: true
});
_ctx.next = 19;
return paginator.paginate(repoUsers.createQueryBuilder(), {
take: 2,
page: 3
});
case 19:
pagination2NextNext = _ctx.sent;
expect(pagination2NextNext).toEqual({
count: 6,
nodes: [
nodes[3],
nodes[0],
],
hasNext: false
});
case 21:
case "end":
return _ctx.stop();
}
}, _callee);
})));
it('test page paginate by custom column name', _asyncToGenerator(_regeneratorRuntime.default.mark(function _callee() {
var repoUsers, nodes, paginator, pagination1;
return _regeneratorRuntime.default.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
repoUsers = connection.getRepository(User);
nodes = [
repoUsers.create({
name: 'c'
}),
repoUsers.create({
name: 'b'
}),
repoUsers.create({
name: 'a'
}),
repoUsers.create({
name: 'c'
}),
repoUsers.create({
name: 'b'
}),
repoUsers.create({
name: 'c'
}),
];
_ctx.next = 4;
return repoUsers.save(nodes);
case 4:
paginator = new _pagePaginator.PagePaginator(User, {
columnNames: {
customName: 'User_name'
},
orderBy: [
{
id: false
},
]
});
_ctx.next = 7;
return paginator.paginate(repoUsers.createQueryBuilder(), {
orderBy: [
{
customName: true
},
{
id: false
},
]
});
case 7:
pagination1 = _ctx.sent;
expect(pagination1).toEqual({
count: 6,
nodes: [
nodes[2],
nodes[4],
nodes[1],
nodes[5],
nodes[3],
nodes[0],
],
hasNext: false
});
case 9:
case "end":
return _ctx.stop();
}
}, _callee);
})));
it('test promisePaginate default', _asyncToGenerator(_regeneratorRuntime.default.mark(function _callee() {
var repoUsers, nodes, paginator, pagination;
return _regeneratorRuntime.default.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
repoUsers = connection.getRepository(User);
nodes = [
repoUsers.create({
name: 'a'
}),
repoUsers.create({
name: 'b'
}),
repoUsers.create({
name: 'b'
}),
repoUsers.create({
name: 'c'
}),
repoUsers.create({
name: 'c'
}),
repoUsers.create({
name: 'c'
}),
];
_ctx.next = 4;
return repoUsers.save(nodes);
case 4:
paginator = new _pagePaginator.PagePaginator(User, {
orderBy: {
id: false
}
});
_ctx.next = 7;
return testPromisePaginationAndResolve(paginator.promisePaginate(repoUsers.createQueryBuilder()));
case 7:
pagination = _ctx.sent;
expect(pagination).toEqual({
count: 6,
nodes: [
nodes[5],
nodes[4],
nodes[3],
nodes[2],
nodes[1],
nodes[0],
],
hasNext: false
});
case 9:
case "end":
return _ctx.stop();
}
}, _callee);
})));
it('test page promisePaginate by single-order', _asyncToGenerator(_regeneratorRuntime.default.mark(function _callee() {
var repoUsers, nodes, paginator, pagination, paginationNext, paginationNextNext;
return _regeneratorRuntime.default.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
repoUsers = connection.getRepository(User);
nodes = [
repoUsers.create({
name: 'a'
}),
repoUsers.create({
name: 'b'
}),
repoUsers.create({
name: 'b'
}),
repoUsers.create({
name: 'c'
}),
repoUsers.create({
name: 'c'
}),
repoUsers.create({
name: 'c'
}),
];
_ctx.next = 4;
return repoUsers.save(nodes);
case 4:
paginator = new _pagePaginator.PagePaginator(User, {
orderBy: {
id: false
},
take: 3
});
_ctx.next = 7;
return testPromisePaginationAndResolve(paginator.promisePaginate(repoUsers.createQueryBuilder()));
case 7:
pagination = _ctx.sent;
expect(pagination).toEqual({
count: 6,
nodes: [
nodes[5],
nodes[4],
nodes[3],
],
hasNext: true
});
_ctx.next = 11;
return testPromisePaginationAndResolve(paginator.promisePaginate(repoUsers.createQueryBuilder(), {
page: 2
}));
case 11:
paginationNext = _ctx.sent;
expect(paginationNext).toEqual({
count: 6,
nodes: [
nodes[2],
nodes[1],
nodes[0],
],
hasNext: false
});
_ctx.next = 15;
return testPromisePaginationAndResolve(paginator.promisePaginate(repoUsers.createQueryBuilder(), {
page: 3
}));
case 15:
paginationNextNext = _ctx.sent;
expect(paginationNextNext).toEqual({
count: 6,
nodes: [],
hasNext: false
});
case 17:
case "end":
return _ctx.stop();
}
}, _callee);
})));
it('test page promisePaginate by multi-orders', _asyncToGenerator(_regeneratorRuntime.default.mark(function _callee() {
var repoUsers, nodes, paginator, pagination1, pagination2, pagination2Next, pagination2NextNext;
return _regeneratorRuntime.default.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
repoUsers = connection.getRepository(User);
nodes = [
repoUsers.create({
name: 'c'
}),
repoUsers.create({
name: 'b'
}),
repoUsers.create({
name: 'a'
}),
repoUsers.create({
name: 'c'
}),
repoUsers.create({
name: 'b'
}),
repoUsers.create({
name: 'c'
}),
];
_ctx.next = 4;
return repoUsers.save(nodes);
case 4:
paginator = new _pagePaginator.PagePaginator(User, {
orderBy: [
{
name: true
},
{
id: false
},
]
});
_ctx.next = 7;
return testPromisePaginationAndResolve(paginator.promisePaginate(repoUsers.createQueryBuilder()));
case 7:
pagination1 = _ctx.sent;
expect(pagination1).toEqual({
count: 6,
nodes: [
nodes[2],
nodes[4],
nodes[1],
nodes[5],
nodes[3],
nodes[0],
],
hasNext: false
});
_ctx.next = 11;
return testPromisePaginationAndResolve(paginator.promisePaginate(repoUsers.createQueryBuilder(), {
take: 2
}));
case 11:
pagination2 = _ctx.sent;
expect(pagination2).toEqual({
count: 6,
nodes: [
nodes[2],
nodes[4],
],
hasNext: true
});
_ctx.next = 15;
return testPromisePaginationAndResolve(paginator.promisePaginate(repoUsers.createQueryBuilder(), {
take: 2,
page: 2
}));
case 15:
pagination2Next = _ctx.sent;
expect(pagination2Next).toEqual({
count: 6,
nodes: [
nodes[1],
nodes[5],
],
hasNext: true
});
_ctx.next = 19;
return testPromisePaginationAndResolve(paginator.promisePaginate(repoUsers.createQueryBuilder(), {
take: 2,
page: 3
}));
case 19:
pagination2NextNext = _ctx.sent;
expect(pagination2NextNext).toEqual({
count: 6,
nodes: [
nodes[3],
nodes[0],
],
hasNext: false
});
case 21:
case "end":
return _ctx.stop();
}
}, _callee);
})));
it('test page promisePaginate by custom column name', _asyncToGenerator(_regeneratorRuntime.default.mark(function _callee() {
var repoUsers, nodes, paginator, pagination1;
return _regeneratorRuntime.default.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
repoUsers = connection.getRepository(User);
nodes = [
repoUsers.create({
name: 'c'
}),
repoUsers.create({
name: 'b'
}),
repoUsers.create({
name: 'a'
}),
repoUsers.create({
name: 'c'
}),
repoUsers.create({
name: 'b'
}),
repoUsers.create({
name: 'c'
}),
];
_ctx.next = 4;
return repoUsers.save(nodes);
case 4:
paginator = new _pagePaginator.PagePaginator(User, {
columnNames: {
customName: 'User_name'
},
orderBy: [
{
id: false
},
]
});
_ctx.next = 7;
return testPromisePaginationAndResolve(paginator.promisePaginate(repoUsers.createQueryBuilder(), {
orderBy: [
{
customName: true
},
{
id: false
},
]
}));
case 7:
pagination1 = _ctx.sent;
expect(pagination1).toEqual({
count: 6,
nodes: [
nodes[2],
nodes[4],
nodes[1],
nodes[5],
nodes[3],
nodes[0],
],
hasNext: false
});
case 9:
case "end":
return _ctx.stop();
}
}, _callee);
})));
});