diff --git a/README.md b/README.md index cc5a740f..918a0f15 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ module.exports = (sequelize, DataTypes) => { // controller.js const { Op } = db.sequelize // Default page = 1 and paginate = 25 -const { docs, pages, total } = await db.MyModel.paginate() +const { docs, pages, total, currentPage } = await db.MyModel.paginate() // Or with extra options const options = { attributes: ['id', 'name'], @@ -42,7 +42,7 @@ const options = { order: [['name', 'DESC']], where: { name: { [Op.like]: `%elliot%` } } } -const { docs, pages, total } = await db.MyModel.paginate(options) +const { docs, pages, total, currentPage } = await db.MyModel.paginate(options) ``` **NOTE:** _If **options** include **limit** or **offset** are ignored._ diff --git a/src/index.js b/src/index.js index 1374b3cf..f50263f0 100644 --- a/src/index.js +++ b/src/index.js @@ -37,7 +37,7 @@ class SequelizePaginate { * @param {paginateOptions} [params] - Options to filter query. * @returns {Promise} Total pages and docs. * @example - * const { docs, pages, total } = await MyModel.paginate({ page: 1, paginate: 25 }) + * const { docs, pages, total, currentPage } = await MyModel.paginate({ page: 1, paginate: 25 }) * @memberof Model */ const pagination = async function ({ @@ -76,7 +76,8 @@ class SequelizePaginate { /* eslint-enable no-console */ if (params.order) options.order = params.order const docs = await this.findAll(options) - return { docs, pages, total } + const currentPage = parseInt(page) + return { docs, pages, total, currentPage } } const instanceOrModel = Model.Instance || Model // @ts-ignore diff --git a/src/types.d.ts b/src/types.d.ts index 665f5ddb..e720a0ed 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -13,6 +13,7 @@ export interface PaginateResult { docs: Array pages: number total: number + currentPage: number } export function paginate( diff --git a/test/index.js b/test/index.js index 70ff547c..63985260 100644 --- a/test/index.js +++ b/test/index.js @@ -47,15 +47,16 @@ describe('sequelizePaginate', () => { }) describe('', () => { it('should paginate with defaults', async () => { - const { docs, pages, total } = await Author.paginate() + const { docs, pages, total, currentPage } = await Author.paginate() expect(docs).to.be.an('array') expect(docs.length).to.equal(25) expect(pages).to.equal(4) expect(total).to.equal(99) + expect(currentPage).to.equal(1) }) it('should paginate with page and paginate', async () => { - const { docs, pages, total } = await Author.paginate({ + const { docs, pages, total, currentPage } = await Author.paginate({ page: 2, paginate: 50 }) @@ -63,10 +64,11 @@ describe('sequelizePaginate', () => { expect(docs.length).to.equal(49) expect(pages).to.equal(2) expect(total).to.equal(99) + expect(currentPage).to.equal(1) }) it('should paginate and ignore limit and offset', async () => { - const { docs, pages, total } = await Author.paginate({ + const { docs, pages, total, currentPage } = await Author.paginate({ limit: 2, offset: 50 }) @@ -74,10 +76,11 @@ describe('sequelizePaginate', () => { expect(docs.length).to.equal(25) expect(pages).to.equal(4) expect(total).to.equal(99) + expect(currentPage).to.equal(1) }) it('should paginate with extras', async () => { - const { docs, pages, total } = await Author.paginate({ + const { docs, pages, total, currentPage } = await Author.paginate({ include: [{ model: Book }], order: [['id']] }) @@ -85,21 +88,25 @@ describe('sequelizePaginate', () => { expect(docs.length).to.equal(25) expect(pages).to.equal(4) expect(total).to.equal(99) + expect(currentPage).to.equal(1) expect(docs[0].books).to.be.an('array') expect(docs[0].books.length).to.equal(99) }) it('should paginate with defaults and group by statement', async () => { const group = ['id'] - const { docs, pages, total } = await Author.paginate({ group }) + const { docs, pages, total, currentPage } = await Author.paginate({ + group + }) expect(docs).to.be.an('array') expect(docs.length).to.equal(25) expect(pages).to.equal(4) expect(total).to.equal(99) + expect(currentPage).to.equal(1) }) it('should paginate with filters, order and paginate', async () => { - const { docs, pages, total } = await Author.paginate({ + const { docs, pages, total, currentPage } = await Author.paginate({ order: [['name', 'DESC']], where: { name: { [Sequelize.Op.like]: 'author1%' } }, paginate: 5 @@ -108,13 +115,16 @@ describe('sequelizePaginate', () => { expect(docs.length).to.equal(5) expect(pages).to.equal(3) expect(total).to.equal(11) + expect(currentPage).to.equal(1) }) it('should paginate with custom scope', async () => { Author.addScope('author1', { where: { name: { [Sequelize.Op.like]: 'author1%' } } }) - const { docs, pages, total } = await Author.scope('author1').paginate({ + const { docs, pages, total, currentPage } = await Author.scope( + 'author1' + ).paginate({ order: [['name', 'DESC']], paginate: 5 }) @@ -122,6 +132,7 @@ describe('sequelizePaginate', () => { expect(docs.length).to.equal(5) expect(pages).to.equal(3) expect(total).to.equal(11) + expect(currentPage).to.equal(1) }) }) })