resgateio / resgate

A Realtime API Gateway used with NATS to build REST, real time, and RPC APIs, where all your clients are synchronized seamlessly.
https://resgate.io
MIT License
685 stars 67 forks source link

Support for linked resources #2

Closed jirenius closed 6 years ago

jirenius commented 6 years ago

Issue

Models currently only supports primitive properties.
To fetch a complex resource structure, you have to do it with multiple requests:

Example:

Promise.all([
    api.getResource('userService.user.42'),
    api.getResource('userService.user.42.roles')
]).then(result => {
    api.getResource('orgService.org.'+result[0].orgId)
        .then(org => {
            console.log("Name : ", result[0].name);
            console.log("Roles: ", result[1]);
            console.log("Org. : ", org);
        });
});

Suggested solution

RES protocol could define a special object that is used for resource links, eg. {"rid":"orgService.org.3"}. If a property contains such a link, the gateway will fetch these resources, doing indirect subscriptions just like collections models, and return them together with the parent model in the response.

Example on get.userService.user.42 service result response:

{
    "id": 42,
    "name": "Jane Doe",
    "roles": {"rid":"userService.user.42.roles"},
    "org": {"rid":"orgService.org.3"}
}

In client:

api.getResource('userService.user.42').then(user => {
    console.log("Name : ", user.name);
    console.log("Roles: ", user.roles);
    console.log("Org. : ", user.org);
});
jirenius commented 6 years ago

Completed in https://github.com/jirenius/resgate/pull/11