From 2cd1e38b0ef41144ea72056f8a3c7f3a30229f7c Mon Sep 17 00:00:00 2001 From: Julien Le Coupanec Date: Sun, 4 Feb 2018 16:09:52 +0000 Subject: [PATCH] Feathers.js: Hook functions --- backend/feathers.js | 47 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/backend/feathers.js b/backend/feathers.js index 9b30de6..9af5cd0 100644 --- a/backend/feathers.js +++ b/backend/feathers.js @@ -93,26 +93,53 @@ myService.removeListener(eventname, [ listener ]) // removes all listeners (or // Hooks are pluggable middleware functions that can be registered // before, after or on errors of a service method. You can register a // single hook function or create a chain of them to create complex work-flows. -app.service('messages').hooks({ +app.service('my-service').hooks({ before: { - create(context) { - context.data.createdAt = new Date(); - }, + all: [ + // Use normal functions + function(context) { console.log('before all hook ran'); } + ], - update(context) { - context.data.updatedAt = new Date(); - }, + find: [ + // Use ES6 arrow functions + context => console.log('before find hook 1 ran'), + context => console.log('before find hook 2 ran') + ], - patch(context) { - context.data.updatedAt = new Date(); - } + async create (context) { + return context + }, }, + after: { + all: [], + find: [], + get: [], + create: [], + update: [], + patch: [], + remove: [] + }, + + // Here is an example for a very useful application hook that logs every service method error + // with the service and method name as well as the error stack error(context) { console.error(`Error in ${context.path} calling ${context.method} method`, context.error); } }); +context.app // [read only] contains the Feathers application object +context.service // [read only] contains the service this hook currently runs on +context.path // [read only] contains the service name (or path) without leading or trailing slashes +context.method // [read only] contains the name of the service method (find, get, create, update...) +context.type // [read only] contains the hook type (one of before, after or error) +context.params // [writable] contains the service method parameters (including params.query) +context.id // [writable] contains the id for a get, remove, update and patch service method call +context.data // [writable] contains the data of a create, update and patch service method call +context.error // [writable] contains the error object that was thrown in a failed method call (only available in error hooks) +context.result // [writable] contains the result of the successful service method call (only available in after hooks) +context.dispatch // [writable and optional] contains a "safe" version of the data that should be sent to any client + /* ******************************************************************************************* * 2. TRANSPORT: Expose a Feathers application as an API server.