UNPKG

nestjs-sequelize-paginate

Version:

🏳‍🌈 Pagination helper method for Sequelize models.

112 lines (111 loc) 4.82 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); const common_1 = require("@nestjs/common"); const sequelize_typescript_1 = require("sequelize-typescript"); const paginate_constans_1 = require("./paginate.constans"); let PaginateService = class PaginateService { constructor(options, sequelize) { this.options = options; this.sequelize = sequelize; } async findAllPaginate(options, optionsSequelize = {}) { const iu = (a) => { return a === undefined || a === null ? false : a; }; let url = iu(options.url) || iu(this.options.url) || null; const showUrl = iu(options.showUrl) || iu(this.options.showUrl) || null; const structure = iu(options.structure) || iu(this.options.structure) || null; const details = iu(options.details) || iu(this.options.details) || null; const isComplete = details === 'complete'; const modelName = options.model.name; const offset = iu(options.offset) || iu(this.options.defaultOffset) || null; const page = iu(options.page) || iu(this.options.defaultPage) || null; const path = iu(options.path) || null; const allowOffset = iu(options.showOffset) || iu(this.options.showOffset) || null; const end = page * offset; const start = end - offset; let totalItems = 0; let totalPages = 0; const itemCount = offset; // Pages let nextPage = null; let prevPage = null; // Urls let nextUrl = null; let prevUrl = null; let firstUrl = null; let lastUrl = null; // Aux let aux; // Data variables let payload = {}; let items = []; const data = await this.sequelize.models[modelName].findAndCountAll(Object.assign(Object.assign({}, optionsSequelize), { limit: offset, offset: start })); totalItems = data.count; totalPages = Math.ceil(totalItems / offset); items = data.rows; aux = page + 1; nextPage = aux <= totalPages ? aux : null; aux = page - 1; prevPage = aux >= 1 ? aux : null; url += path + '?page='; firstUrl = url + 1; lastUrl = url + totalPages; nextPage && (nextUrl = url + nextPage); prevPage && (prevUrl = url + prevPage); if (allowOffset) { nextPage && (nextUrl += '&offset=' + offset); prevPage && (prevUrl += '&offset=' + offset); } const meta = { page, nextPage, prevPage, }; isComplete && ((meta['offset'] = offset), (meta['totalItems'] = totalItems), (meta['totalPages'] = totalPages), (meta['itemCount'] = itemCount)); const links = { nextUrl, prevUrl, }; isComplete && ((links['firstUrl'] = firstUrl), (links['lastUrl'] = lastUrl)); switch (structure) { case 'segmented': payload = { meta, items, }; showUrl && (payload['links'] = links); break; case 'simple': default: payload = Object.assign(Object.assign({}, meta), { items }); showUrl && (payload = Object.assign(Object.assign({}, payload), links)); break; } return payload; } }; PaginateService = __decorate([ common_1.Injectable(), __param(0, common_1.Inject(paginate_constans_1.PAGINATE_OPTIONS)), __metadata("design:paramtypes", [Object, sequelize_typescript_1.Sequelize]) ], PaginateService); exports.PaginateService = PaginateService;