Merrock
Merrock

Reputation: 51

Feathers JS nested Routing or creating alternate services

The project I'm working on uses the feathers JS framework server side. Many of the services have hooks (or middleware) that make other calls and attach data before sending back to the client. If I have a new feature that needs to query a database but for a only few specific things I'm thinking I don't want to use the already built out "find" method for this database query as that "find" method has many other unneeded hooks and calls to other databases to get data I do not need for this new query on my feature.

My two solutions so far:

  1. I could use the standard "find" query and just write if statements in all hooks that check for a specific string parameter that can be passed in on client side so these hooks are deactivated on this specific call but that seems tedious especially if I find this need for several other different services that have already been built out.

  2. I initialize a second service below my main service so if my main service is:

    app.use('/comments', new JHService(options));
    

    right underneath I write:

    app.use('/comments/allParticipants', new JHService(options));
    

    And then attach a whole new set of hooks for that service. Basically it's a whole new service with the only relation to the origin in that the first part of it's name is 'comments' Since I'm new to feathers I'm not sure if that is a performant or optimal solution.

Is there a better solution then those options? or is option 1 or option 2 the most correct way to solve my current issue?

Upvotes: 0

Views: 1604

Answers (1)

Daff
Daff

Reputation: 44215

You can always wrap the population hooks into a conditional hook:

const hooks = require('feathers-hooks-common');

app.service('myservice').after({
  create: hooks.iff(hook => hook.params.populate !== false, populateEntries)
});

Now population will only run if params.populate is not false.

Upvotes: 1

Related Questions