Skip to content

Commit

Permalink
fix: allow for inclusion of scope by @dsmackie
Browse files Browse the repository at this point in the history
Update types

fix #46
  • Loading branch information
dsmackie authored and lgaticaq committed Apr 10, 2019
1 parent 58dd837 commit 5861f9f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class SequelizePaginate {
return acc
}, {})

let total = await Model.count(countOptions)
let total = await this.count(countOptions)

if (options.group !== undefined) {
// @ts-ignore
Expand All @@ -75,7 +75,7 @@ class SequelizePaginate {
}
/* eslint-enable no-console */
if (params.order) options.order = params.order
const docs = await Model.findAll(options)
const docs = await this.findAll(options)
return { docs, pages, total }
}
const instanceOrModel = Model.Instance || Model
Expand Down
8 changes: 4 additions & 4 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FindOptions, Instance, Model } from 'sequelize'
import { FindOptions, Model } from 'sequelize'

export class SequelizePaginate<TInstance, TAttributes> {
public paginate(Model: Model<TInstance, TAttributes>): void
Expand All @@ -10,7 +10,7 @@ export interface Paginate {
}

export interface PaginateResult<TAttributes> {
docs: Array<Instance<TAttributes>>
docs: Array<TAttributes>
pages: number
total: number
}
Expand All @@ -19,5 +19,5 @@ export function paginate<TInstance, TAttributes>(
Model: Model<TInstance, TAttributes>
): void
export function pagination<T, TAttributes>(
params: FindOptions<T> & Paginate
): Promise<Instance<TAttributes>>
params: FindOptions & Paginate
): Promise<TAttributes>
14 changes: 14 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,19 @@ describe('sequelizePaginate', () => {
expect(pages).to.equal(3)
expect(total).to.equal(11)
})

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({
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)
})
})
})

0 comments on commit 5861f9f

Please sign in to comment.