loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Dynamic properties in JSON payloads

Open bajtos opened this issue 12 years ago • 9 comments

As a developer, I want to add dynamic (computed) properties to my model. Such properties should be exposed in javascript API (e.g. MyModel.dynamic) and also in the JSON payload used by REST.

See #600 for background.

bajtos avatar Oct 06 '14 10:10 bajtos

Any solution for this?

yuanyong avatar Jul 17 '15 04:07 yuanyong

Currently you can fake this with model hooks, but there should be an api for computed/dynamic properties. A standard feature of most ODMs nowadays.

acao avatar Aug 30 '15 17:08 acao

See https://github.com/fullcube/loopback-ds-computed-mixin

mrfelton avatar Aug 30 '15 18:08 mrfelton

Oh very nice, thanks!

acao avatar Aug 30 '15 18:08 acao

@mrfelton's solution seems reasonable... any plans to add this integrate something like this into the loopback codebase?

jmwohl avatar Feb 27 '16 18:02 jmwohl

This is a very common use case that could be made elegant in LB. I like the approach @mrfelton took in his mixin: https://github.com/fullcube/loopback-ds-computed-mixin

What would be really great is if it doesn't have to be specified in the mixin object but in the properties of the model themselves or maybe a "computed" object that has the computed fields (just as long as it stays documented in swagger). Here's a concept:

// Model.json
{
    "name": "Item",
    "properties": {
        "name": {
          "type": "string"
      },
        "email": {
            "type": "string"
        },
        "info": {
            "type": "string",
            "computed": "computeInfo"
        }
    }
}

// Model.js
Item.computeInfo = function computeInfo(item) {
    // e.g. returns: "info: "Rick, [email protected]"
    return item.name + ', ' + item.email;
};

Or another approach, so you don't have to touch the JS (which also means it can be abstracted out to the UI at some point):

// Computed only inside .json (no .js needed, just expressions and str replacement)
{
    "name": "Item",
    "properties": {
        // ...
        "info": {
            "type": "string",
            "computed": "{name}, {email}"
        }
    }
}

rblalock avatar Jun 17 '16 19:06 rblalock

Another comment on this that is semi-related - I just realized Loopback has this functionality where you can define a defaultFn - https://github.com/strongloop/loopback/issues/292 (source: https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/model.js#L288-L313) . Only 3 use cases are supported and supporting a custom function isn't supported yet. If and when support for allowing a custom defaultFn comes, it'd probably be good to keep the above suggestions consistent with this as well, to keep it congruent.

rblalock avatar Jul 08 '16 16:07 rblalock

I'm agree with @rblalock !!! it would be very convenient to be able to create dynamic properties.

Biax2099 avatar Mar 13 '17 15:03 Biax2099

Any update on this ?

toniopelo avatar Sep 10 '17 14:09 toniopelo