Skip to content

Commit

Permalink
Merge branch 'release/4.0.14'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 22, 2017
2 parents a7f7bbb + f301b8f commit c9d0592
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,18 @@
<a name="4.0.14"></a>
## [4.0.14](https://github.com/adonisjs/adonis-lucid/compare/v4.0.13...v4.0.14) (2017-08-22)


### Bug Fixes

* **package:** update pluralize to version 7.0.0 ([#162](https://github.com/adonisjs/adonis-lucid/issues/162)) ([7723779](https://github.com/adonisjs/adonis-lucid/commit/7723779))


### Features

* **hooks:** add afterFetch hook ([941986e](https://github.com/adonisjs/adonis-lucid/commit/941986e))



<a name="4.0.13"></a>
## [4.0.13](https://github.com/adonisjs/adonis-lucid/compare/v4.0.12...v4.0.13) (2017-08-18)

Expand Down
4 changes: 2 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "@adonisjs/lucid",
"version": "4.0.13",
"version": "4.0.14",
"description": "SQL ORM built on top of Active Record pattern",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -29,7 +29,7 @@
"knex": "^0.13.0",
"lodash": "^4.17.4",
"moment": "^2.18.1",
"pluralize": "^6.0.0",
"pluralize": "^7.0.0",
"pretty-hrtime": "^1.0.3",
"require-all": "^2.2.0"
},
Expand Down
4 changes: 2 additions & 2 deletions src/Lucid/Hooks/index.js
Expand Up @@ -24,7 +24,7 @@ const { resolver } = require('../../../lib/iocResolver')
*/
class Hooks {
constructor () {
this._events = ['create', 'update', 'delete', 'restore', 'find']
this._events = ['create', 'update', 'delete', 'restore', 'find', 'fetch']

/**
* The event aliases. Whenever a handler is saved for a alias,
Expand Down Expand Up @@ -73,7 +73,7 @@ class Hooks {
* ```
*/
addHandler (event, handler, name) {
if (_.includes(this._events[event]) && !_.includes(this._aliasEvents, event)) {
if (!_.includes(this._events, event) && !_.includes(this._aliasEvents, event)) {
throw GE.InvalidArgumentException.invalidParameter(`${event} is not a valid hook event`)
}
this._handlers[event] = this._handlers[event] || []
Expand Down
12 changes: 11 additions & 1 deletion src/Lucid/QueryBuilder/index.js
Expand Up @@ -282,6 +282,13 @@ class QueryBuilder {
const modelInstances = this._mapRowsToInstances(rows)
await this._eagerLoad(modelInstances)

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

/**
* Return an instance of active model serializer
*/
Expand Down Expand Up @@ -317,7 +324,10 @@ class QueryBuilder {
await modelInstance.loadMany(this._eagerLoads)
}

await this.Model.$hooks.after.exec('find', modelInstance)
if (this.Model.$hooks) {
await this.Model.$hooks.after.exec('find', modelInstance)
}

return modelInstance
}

Expand Down
30 changes: 30 additions & 0 deletions test/unit/lucid.spec.js
Expand Up @@ -1492,4 +1492,34 @@ test.group('Model', (group) => {
const users = await User.query().where('username', 'virk').setVisible(['created_at', 'id']).fetch()
assert.deepEqual(Object.keys(users.first().toObject()), ['created_at', 'id'])
})

test('define after fetch hook', async (assert) => {
class User extends Model {
}

User._bootIfNotBooted()

const fn = async function () {}
User.addHook('afterFetch', fn)

assert.deepEqual(User.$hooks.after._handlers.fetch, [{ handler: fn, name: undefined }])
})

test('call after fetch hook when fetching data', async (assert) => {
assert.plan(2)
class User extends Model {
}

User._bootIfNotBooted()

const fn = async function (instances) {
instances.forEach((instance) => {
assert.instanceOf(instance, User)
})
}

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

0 comments on commit c9d0592

Please sign in to comment.