diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0319fe76..829b6bee 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,18 @@
+
+## [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))
+
+
+
## [4.0.13](https://github.com/adonisjs/adonis-lucid/compare/v4.0.12...v4.0.13) (2017-08-18)
diff --git a/package.json b/package.json
index 9efe85ff..7fdddecd 100644
--- a/package.json
+++ b/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": {
@@ -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"
},
diff --git a/src/Lucid/Hooks/index.js b/src/Lucid/Hooks/index.js
index 6ad3664d..ddef9ddc 100644
--- a/src/Lucid/Hooks/index.js
+++ b/src/Lucid/Hooks/index.js
@@ -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,
@@ -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] || []
diff --git a/src/Lucid/QueryBuilder/index.js b/src/Lucid/QueryBuilder/index.js
index 23c62939..d24e9f68 100644
--- a/src/Lucid/QueryBuilder/index.js
+++ b/src/Lucid/QueryBuilder/index.js
@@ -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
*/
@@ -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
}
diff --git a/test/unit/lucid.spec.js b/test/unit/lucid.spec.js
index 707002de..3d70f14b 100644
--- a/test/unit/lucid.spec.js
+++ b/test/unit/lucid.spec.js
@@ -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')
+ })
})