Gui Prá
Gui Prá

Reputation: 5677

Getting service handle or mount point from Feathers hook

I want to implement an after-hook that replaces record IDs with globally unique IDs of the form {serviceName}:{id} (where id is a typical MySQL numerical ID).

However I couldn't find a way to access serviceName or service mount point from hooks. Is it possible?

If not, I'll just stick with passing the service name as a parameter during hook instantiation, but that's manual and error prone. I expect there's a better way to do this :)

Upvotes: 0

Views: 293

Answers (1)

Daff
Daff

Reputation: 44215

You can iterate over all services and then return the path for the one that matches the hook context:

app.service('myservice').before({
  create(hook) {
    const service = this;
    const allServices = hook.app.services;

    const path = Object.keys(allServices).find(
      currentPath => allServices[currentPath] === service
    );
  }
});

Upvotes: 1

Related Questions