Skip to content

Instantly share code, notes, and snippets.

@claustres
Last active August 29, 2021 20:14
Show Gist options
  • Save claustres/2560304754a1bcaf39d40bf1ecbe7058 to your computer and use it in GitHub Desktop.
Save claustres/2560304754a1bcaf39d40bf1ecbe7058 to your computer and use it in GitHub Desktop.
Proxy service
import { merge } from 'feathers-commons'
function createProxyService (options) {
const targetService = options.service
function proxy (params) {
return merge(params, options.params)
}
return {
find (params) { return targetService.find(proxy(params)) },
get (id, params) { return targetService.get(id, proxy(params)) },
create (data, params) { return targetService.create(data, proxy(params)) },
update (id, data, params) { return targetService.update(id, data, proxy(params)) },
patch (id, data, params) { return targetService.patch(id, data, proxy(params)) },
remove (id, params) { return targetService.remove(id, proxy(params)) }
}
}
...
function createOrganisationService (hook) {
app.use(hook.result._id.toString() + '/users', createProxyService({
service: app.service('users'),
params: { query: { 'organisation': hook.result._id.toString() } }
})
return hook
}
@chrisbag
Copy link

Hello,

I am using Feathersjs on a production app and I stumbled upon this piece of code while reading your article on Access Controle with Feathers (https://blog.feathersjs.com/access-control-strategies-with-feathersjs-72452268739d).
I find the approach very interesting but am a bit confused.

  • Is serviceProxying just a clever way of doing nested routes ? (In this case organisation/:id/users)
  • In createOrganisationService function you pass in a hook as a parameter. Where/how do you actually call this function in a feathers app? I find it weird instantiating a service by calling a service hook function. What am I missing ? Or do you eventually call it as an after hook of a organisation query that you would initally call, meaning that you set up as many organisations services as you have organisations ?

thanks a lot for your help and all the material you have published on feathers

@claustres
Copy link
Author

Currently on leave, I will answer in a couple of weeks

@claustres
Copy link
Author

Service proxying might be viewed as a more feathers way to do nested routes but it could be used to do others things as well.

About organisations I actually want to be able to create any number of organisations dynamically so there is a hook on each create/remove operation on the organisation service in order to dynamically add/remove the related organisation services. You can have a look at our Akt'n'Map application to see how we use it in a production application, eg here or here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment