From 772377918ef88df03db2bc087bed0b7d2bccc076 Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Mon, 21 Aug 2017 13:14:25 +0530 Subject: [PATCH 1/3] fix(package): update pluralize to version 7.0.0 (#162) --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9efe85ff..19939713 100644 --- a/package.json +++ b/package.json @@ -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" }, From 941986eb88d2afe8030e9c87cd00911dde09fd33 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Mon, 21 Aug 2017 14:26:24 +0530 Subject: [PATCH 2/3] feat(hooks): add afterFetch hook --- src/Lucid/Hooks/index.js | 4 ++-- src/Lucid/QueryBuilder/index.js | 12 +++++++++++- test/unit/lucid.spec.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) 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') + }) }) From f301b8ffb3eb1862f28c8e51caef9fd43ccdfeb0 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Tue, 22 Aug 2017 22:09:16 +0530 Subject: [PATCH 3/3] chore(release): 4.0.14 --- CHANGELOG.md | 15 +++++++++++++++ package.json | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) 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 19939713..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": {