feathersjs / hooks

Async middleware for JavaScript and TypeScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

integrate it to existing feathers app

noor-tg opened this issue · comments

how to integrate this package to existing feathers app ?

This is a very relevant question, we hope to get some information soon.

You can use it in your app already right now as documented. For the core integration, follow the feathersjs/feathers#1798 PR and other linked ones.

Hi @daffl, I haven't found anything there explaining how to use the new hooks in an app with Feathersjs v4. I'm particularly interested in this new model because it will make it easier for adding cross cutting concerns like logging. The actual hooks in Feathersjs v4, although it's very clean, it's difficult to implement a cross cutting concerns that spans the entire client request (vide feathersjs/feathers#1881).

I think it deserves a brief demo about how to integrate it with existing Feathersjs v4. It should show the 'old' Feathersjs v4 hooks style and the new style for a side-by-side comparison.

That will be the next step for v5. This release specifically focussed on being usable standalone without anything else Feathers related. You can attach them to any object including a service:

const { hooks } = require('@feathersjs/hooks');
const app = feathers();

const logRuntime = async (context, next) => {
  const start = new Date().getTime();

  await next();

  const end = new Date().getTime();

  console.log(`Function '${context.method || '[no name]'}' returned '${context.result}' after ${end - start}ms`);
}

hooks(app.service('messages'), {
  create: [ logRuntime ]
});

This can be accomplished for all services using app.mixins. Note that required properties (like app and service currently have to be added manually by customizing the context).

@daffl, thanks for informing that. The v5 is a great evolution of the framework.

This has now been done in the Feathers v5.0.0-beta.0 prerelease where the new hooks are a first class citizen. See the migration guide for how to test it.