-
Hi, I've been wondering lately, what are some common or example use cases for writing your own custom services or customizing them by extending the service class, rather than providing the functionality via service hooks? (inspired by the current Slack discussion https://feathersjs.slack.com/archives/C0HJE6A65/p1629626955210200) |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
Personally I don't think I ever found a custom service unnecessary later but I've seen more than a few example where what is done in hooks should really be a custom or customized (extended) service instead. This is part of the reason why I added the Hooks vs. extending services section to the guide. For example, if you end up setting I have a feeling that the upcoming schema definitions and resolvers will add a more concise way to encapsulate things that used to be done across (multiple) hooks. My hope is it may become more clear where things should go even though it is introducing a new concept:
|
Beta Was this translation helpful? Give feedback.
-
Thanks. I've got my hopes high for schema definitions and (especially) resolvers coming up as a new standard for things like population. Good luck with that! |
Beta Was this translation helpful? Give feedback.
-
I've used custom service as a service that is not tied to any model (database tables). For example, I want to expose a API that calls other service // in client
feathers.service('batch').create({ service: 'post', data: [...] });
// in batch service
create(data) {
// you can do some other stuff here
this.app.service(data.service).create(data.data);
// or here before
return
} So essentially, you exposed one service to your consumer. This is very simple example, but hope you get the gist of it. |
Beta Was this translation helpful? Give feedback.
-
Another big one I can think of is using a custom service as a proxy to another existing API which allows you to do things like:
|
Beta Was this translation helpful? Give feedback.
Personally I don't think I ever found a custom service unnecessary later but I've seen more than a few example where what is done in hooks should really be a custom or customized (extended) service instead. This is part of the reason why I added the Hooks vs. extending services section to the guide.
For example, if you end up setting
context.result
in hooks a lot (other than e.g. a generic caching system) it might be worth customizing the service instead. The most extreme example I've seen was a custom service method simply returningtrue
which never got hit due to a hook around it always settingcontext.result
. That hook should have really been the service method itself.I have a feeling…