Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement custom / complex operation routes in FeathersJS

Tags:

feathersjs

I need to implement a bunch of routes that do very custom / complex operations on a FeathersJS app.

One of those routes is /Category/disableExclusiveContentsOf/:id. It runs a query against half a dozen database tables to find rows that relate to the category :id exclusively. I absolutely cannot do that using the querying abstractions FeathersJS provides. Then, it uses FeathersJS' "local" API to update the rows I found, so that data update events are fired to clients.

However, if I implement the route using Express alone, Feathers authentication / authorization hooks won't run, so the endpoint won't be protected, which is a requirement.

How can I accommodate such things in a FeathersJS application?

like image 537
Gui Prá Avatar asked Dec 15 '22 03:12

Gui Prá


1 Answers

You can still implement the route using your own service and use the :id as route parameter:

app.use('/Category/disableExclusiveContentsOf/:id', {
  find() {
    // do complex stuff here
  }
});

One thing I'd recommend changing is that the URL seems to be action and not resource oriented. This means that someone can change your application data with a GET request which is generally considered not a good practise (e.g. in some cases the Google crawler came in and deleted/changed a bunch of things).

Feathers encourages you to think in resources rather than custom routes and actions. In your case you would have an ExclusiveContents service that you can POST to:

app.use('/Category/ExclusiveContents/:categoryId', {
  create(data, params) {
    // do complex stuff here
    params.categoryId // the id of the category
    data // -> additional data from the POST request
  }
});
like image 178
Daff Avatar answered May 09 '23 11:05

Daff