Last active
August 29, 2021 20:14
-
-
Save claustres/2560304754a1bcaf39d40bf1ecbe7058 to your computer and use it in GitHub Desktop.
Proxy service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Currently on leave, I will answer in a couple of weeks
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
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.
thanks a lot for your help and all the material you have published on feathers