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
}
@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