Skip to content

Commit

Permalink
feat(lucid): add afterPaginate hook
Browse files Browse the repository at this point in the history
Closes #236
  • Loading branch information
thetutlage committed Nov 17, 2017
1 parent 27ac971 commit f12d8a5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/Lucid/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
14 changes: 13 additions & 1 deletion src/Lucid/QueryBuilder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,22 @@ class QueryBuilder {
const modelInstances = this._mapRowsToInstances(result.data)
await this._eagerLoad(modelInstances)

/**
* Pagination meta data
*/
const pages = _.omit(result, ['data'])

/**
* Fire afterPaginate event
*/
if (this.Model.$hooks) {
await this.Model.$hooks.after.exec('paginate', modelInstances, pages)
}

/**
* Return an instance of active model serializer
*/
return new this.Model.Serializer(modelInstances, _.omit(result, ['data']))
return new this.Model.Serializer(modelInstances, pages)
}

/**
Expand Down
22 changes: 21 additions & 1 deletion test/unit/lucid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1565,7 +1565,7 @@ test.group('Model', (group) => {

User.addHook('afterFetch', fn)
await ioc.use('Database').table('users').insert([{ username: 'virk' }, { username: 'nikk' }])
await User.all('username', 'virk')
await User.all()
})

test('create a new row when unable to find one', async (assert) => {
Expand Down Expand Up @@ -1660,4 +1660,24 @@ test.group('Model', (group) => {
assert.equal(user.created_at, user.toJSON().created_at)
assert.equal(user.updated_at, user.toJSON().updated_at)
})

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').table('users').insert([{ username: 'virk' }, { username: 'nikk' }])
await User.query().paginate()
})
})

0 comments on commit f12d8a5

Please sign in to comment.