Loopback: Dynamic properties in JSON payloads

Created on 6 Oct 2014  路  9Comments  路  Source: strongloop/loopback

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.

feature stale

Most helpful comment

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.

All 9 comments

Any solution for this?

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.

Oh very nice, thanks!

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

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

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.

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

Any update on this ?

Was this page helpful?
0 / 5 - 0 ratings