Apply Auto Population to a Property

What you’ll build

While following this guide, you will start an oeCloud based NodeJS application and -

  • create a new Model called Product with a field called owner and declare it to be auto-populated from the logged-in userId who creates the instance of Product.

What you’ll need

To complete this guide, you will need the following -

  • an understanding of what a Model is in the context of Loopback, which is the basis of the oeCloud. You can get this by perusing the documentation here
  • a running NodeJS application built using the oeCloud.
  • a working REST client. You can use the Linux cURL command as a REST client if you have access to a Linux machine or have Git Bash installed on your Windows machine. REST addons for browsers, like Postman for Firefox or Google Chrome also can be used to complete this guide.

How to complete this guide

Download and install the sample application from here.

Auto-Populate a property called owner in the Product Model

Need for Auto-Population
Frequently, while model(data) is saved from the front-end, we do not want the user to enter parts of the model(data), as the server already knows about it, or there is a space constraint on the UI for all fields. More importantly, it is sometimes not secure to allow data entry of certain fields. Such fields are best populated by the server, before the model (data) is persisted. The Auto-Populator feature caters to this requirement.

Auto-Populator
With this feature, you can auto-populate specific model-properties in a declarative way. You can declare a model property value to be set from any of the following entities at the time of saving data using any model whose base is BaseEntity:

Entity Description
BODY The body of the http request
QUERY The query parameters of the http request
HEADER The headers of the http request
COOKIE The Cookies in the http request
REQUEST The HTTP request
CALLCONTEXT The CallContext Object
ACCESSTOKEN The AccessToken
USER The logged-in User
USERPROFILE The logged-in user’s Profile

If auto-population is used for a property, then that property and its value need not be supplied while saving data of the corresponding model. (If this is supplied, it will be ignored).

The declaration is done in a manner similar to how Loopback allows declarative validations for properties, e.g., “required”: true.

To declare a model’s property to be auto-populated, a property setting called "setval" needs to be added to the property. The value of "setval" should be a string pattern of the form

<entity>.<member 1>.<member 2>… <member n>

where <entity> can be one of the above listed entities, e.g., USERPROFILE.

<member 1> is a top-level member (value or object) of <entity>. <member 2> is a member of <member 1> object, and so on.
The last member needs to be a string or number.

The pattern should evaluate to a valid string or number. An invalid pattern would result in the auto-populator doing nothing. For e.g., if the property is declared to be "required": true, then you would get a “cannot be blank” error while saving the model.

For example, to auto-populate the owner field in the Product Model from the userId of the logged-in USER object, you need to declare the owner property as follows in the Product Model definition:

"owner": {"type": "string", "setval" : "USER.userId"}

The Product Model definition incorporating the auto-populated owner property should be as follows:

{
    "name": "Product",
    "base": "BaseEntity",
    "strict": false,
    "plural": "Products",
    "idInjection": true,
    "options": {
        "validateUpsert": true
    },
    "properties": {
        "owner": {
            "type": "string",
            "setval" : "USER.userId"
        },
        "code": {
            "type": "string"
        },
        "name": {
            "type": "string"
        },
        "category": {
            "type": "string"
        },
        "price": {
            "type": "number"
        },
        "offeredSince": {
            "type": "date"
        },
        "active": {
            "type": "boolean"
        },
        "description": {
            "type": "string"
        }
    }
}

Do the following to create the Product Model with the owner field as an auto-populated field, populated from userId:

  • Open a command window (“cmd” in Windows OS, “bash” Terminal in Linux)
  • copy the following command to the command prompt, change the host and port, if necessary, and hit the ENTER key:

curl -v “http://localhost:3000/api/ModelDefinitions” -H “Content-Type: application/json” -H “Accept: application/json” –data “{"name": "Product","base": "BaseEntity","strict": false,"plural": "Products","idInjection": true,"options": {"validateUpsert": true},"properties": {"owner": {"type": "string", "setval" : "USER.userId"}, "code": {"type": "string"},"name": {"type": "string"},"category": {"type": "string"},"price": {"type": "number"},"offeredSince": {"type": "date"},"active": {"type": "boolean"},"description": {"type": "string"}}}”

This creates the Product Model with the owner field as an auto-populated field, populated from userId.

Summary

We have seen how we can auto-populate a specific property of a Model in oeCloud.