Skip to content

Commit

Permalink
fix(hooks): hook afterPaginate
Browse files Browse the repository at this point in the history
  • Loading branch information
Duy Luong committed Jan 10, 2018
1 parent adbf41e commit f027712
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/LucidMongo/Hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const { resolver } = require('../../../lib/iocResolver')
*/
class Hooks {
constructor () {
this._events = ['create', 'update', 'delete', 'restore', 'find', 'fetch']
this._events = ['create', 'update', 'delete', 'restore', 'find', 'fetch', 'paginate']

/**
* The event aliases. Whenever a handler is saved for a alias,
Expand Down Expand Up @@ -141,11 +141,11 @@ class Hooks {
* @async
*
* @param {String} event
* @param {Object} ctx
* @param {Spread} ...args
*
* @return {void}
*/
async exec (event, ctx) {
async exec (event, ...args) {
const handlers = this._handlers[event] || []
const aliasesHandlers = this._aliases[event] ? this._handlers[this._aliases[event]] || [] : []
const allHandlers = handlers.concat(aliasesHandlers)
Expand All @@ -163,7 +163,7 @@ class Hooks {
*/
for (let handler of allHandlers) {
const { method } = resolver.forDir('modelHooks').resolveFunc(handler.handler)
await method(ctx)
await method(...args)
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions test/unit/lucid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,26 @@ test.group('Lucid | Query delete', (group) => {
const newUsers = await User.all()
assert.lengthOf(newUsers.rows, 2)
})

test('call after paginate hook when calling paginate method', async (assert) => {
assert.plan(3)
class User extends Model {
}

User._bootIfNotBooted()

const fn = async function (instances, pages) {
assert.deepEqual(pages, { perPage: 20, total: helpers.formatNumber(2), page: 1, lastPage: 1 })

instances.forEach((instance) => {
assert.instanceOf(instance, User)
})
}

User.addHook('afterPaginate', fn)
await ioc.use('Database').collection('users').insert([{ username: 'virk' }, { username: 'nikk' }])
await User.query().paginate()
})
})

test.group('Lucid | Aggregate', (group) => {
Expand Down

0 comments on commit f027712

Please sign in to comment.