diff --git a/src/index.js b/src/index.js index 8f408c08..1374b3cf 100644 --- a/src/index.js +++ b/src/index.js @@ -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 @@ -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 diff --git a/src/types.d.ts b/src/types.d.ts index 2bf7a5e6..665f5ddb 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -1,4 +1,4 @@ -import { FindOptions, Instance, Model } from 'sequelize' +import { FindOptions, Model } from 'sequelize' export class SequelizePaginate { public paginate(Model: Model): void @@ -10,7 +10,7 @@ export interface Paginate { } export interface PaginateResult { - docs: Array> + docs: Array pages: number total: number } @@ -19,5 +19,5 @@ export function paginate( Model: Model ): void export function pagination( - params: FindOptions & Paginate -): Promise> + params: FindOptions & Paginate +): Promise diff --git a/test/index.js b/test/index.js index ead7bb8d..70ff547c 100644 --- a/test/index.js +++ b/test/index.js @@ -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) + }) }) })