diff --git a/src/LucidMongo/Hooks/index.js b/src/LucidMongo/Hooks/index.js index ddef9dd..bc14487 100644 --- a/src/LucidMongo/Hooks/index.js +++ b/src/LucidMongo/Hooks/index.js @@ -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, @@ -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) @@ -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) } } } diff --git a/test/unit/lucid.spec.js b/test/unit/lucid.spec.js index 2eb45e3..b79762e 100644 --- a/test/unit/lucid.spec.js +++ b/test/unit/lucid.spec.js @@ -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) => {