Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add currentpage to return #445

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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._
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SequelizePaginate {
* @param {paginateOptions} [params] - Options to filter query.
* @returns {Promise<PaginateResult>} 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 ({
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface PaginateResult<TAttributes> {
docs: Array<TAttributes>
pages: number
total: number
currentPage: number
}

export function paginate<TInstance, TAttributes>(
Expand Down
25 changes: 18 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,59 +47,66 @@ 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
})
expect(docs).to.be.an('array')
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
})
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 extras', async () => {
const { docs, pages, total } = await Author.paginate({
const { docs, pages, total, currentPage } = await Author.paginate({
include: [{ model: Book }],
order: [['id']]
})
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)
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
Expand All @@ -108,20 +115,24 @@ 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
})
expect(docs).to.be.an('array')
expect(docs.length).to.equal(5)
expect(pages).to.equal(3)
expect(total).to.equal(11)
expect(currentPage).to.equal(1)
})
})
})