Modifying Data Requests Using Cloud Code

Modifying Data Requests Using Cloud Code

You have the option to modify a request send by the user before it is processed by Telerik Platform. For example, if an integer field has a range of allowed values, you can write a simple script that checks whether the value for each data item is within certain limits specified by an array of values, and replace it with the default.

  • Return custom object
  • Return additional data
  • Force a value within certain limits
  • Include querying expressions
  • Set dynamic properties

Return custom object

You can simply return your own custom object by invoking the Everlive.Response.setObjectResult(value) function.

Everlive.Events.beforeRead(function(request, context, done) {
    Everlive.Response.setObjectResult("This is a hello message from Cloud Code");
    done();
});

The result will be similar to this:

{
    "Result": "This is a hello message from Cloud Code"
}

Return additional data

You can return custom additional information with each request. This can be done in the AFTER event by using the additionalData field of the response argument. You can put there both simple values and complex object, they will be returned by the service in the exact way you specified them.

Everlive.Events.afterRead(function(request, response, context, done) {
    response.additionalData.MyValue = 5;
    response.additionalData.MyComplexObject = {
        a: 5,
        b: new Date()
    };

    done();
});

The result will be similar to this:

{
    "MyValue": 5,
    "MyComplexObject": {
        "a": 5,
        "b": "2012-12-04T09:55:50.000Z"
    },
    "Result": (default result from the request goes here)
}

Force a value within certain limits

If you want to define a field Gender that accepts values 1,2,3 where 1 is undefined, 2 is male and 3 is female, you can simply execute the following script that will always set the value to 1 - undefined in case it is outside the array:

Everlive.Events.beforeCreate(function(request, context, done) {
    normalizeItemData(request.data);
    done();
});

Everlive.Events.beforeUpdate(function(request, context, done) {
    normalizeItemData(request.data);
    done();
});

var allowedGenderValues = [1, 2, 3];

var normalizeItemData = function(item) {
    if (allowedGenderValues.indexOf(item.Gender) == -1) {
        item.Gender = 1;
    }
};

Include querying expressions

Using Cloud Code you can also refine the results of each request by defining the filter, fields, sort, paging, and expand expression straight in the cloud code of your pre-processing functions. In this way you can always limit the results returned of each request without having to define them at the client. The following properties are supported by the request argument of all pre-processing functions.

  • filterExpression
  • sortExpression
  • fieldsExpression
  • skip
  • take
  • options
  • expandExpression

To always return the user ID and DisplayName you can set the following fieldsExpression. Please navigate to the Querying section where you can read about all supported expressions.

Everlive.Events.beforeRead(function(request, context, done) {
    request.fieldsExpression = {"DisplayName" : 1};

    done();
});

The result of the query above will simply return an ID and DisplayName for each user:

{
    "Result": [
        {
            "Id": "136b5368-2ef4-40fa-a62d-5f409fbc173c",
            "DisplayName": "Seth Peterson"
        },
        {
            "Id": "cd94813a-893d-4463-8281-7b4439c62a2d",
            "DisplayName": "Michael Taylor"
        },
        {
            "Id": "bc224f8b-8a3b-4e13-ac33-07801eb1d0ed",
            "DisplayName": "Andy Gerald"
        }],
     "Count" : 3
}

Set dynamic properties

You can set dynamic properties to be returned with each object part of a service call. For example, if you want to return the ordinal of each item in your collection, you can enhance the code snippet to above to also return the Ordinal based on the CreatedAt field.

Everlive.Events.beforeRead(function(request, context, done) {
    request.fieldsExpression = {"DisplayName" : 1};
    request.sortExpression = {"CreatedAt" : 1};
    done();
});

Everlive.Events.afterRead(function(request, response, context, done) {
    if(!response.error) {
        for(var i = 0; i < response.result.length; i++) {
            response.result[i].Ordinal = i+1;
        }
    }
    done();
});

The result of the query above will return the ID, DisplayName and Ordinal:

{
    "Result": [
        {
            "Id": "136b5368-2ef4-40fa-a62d-5f409fbc173c",
            "DisplayName": "Seth Peterson",
            "Ordinal": 1
        },
        {
            "Id": "cd94813a-893d-4463-8281-7b4439c62a2d",
            "DisplayName": "Michael Taylor",
            "Ordinal": 2
        },
        {
            "Id": "bc224f8b-8a3b-4e13-ac33-07801eb1d0ed",
            "DisplayName": "Andy Gerald",
            "Ordinal": 3
        }],
     "Count" : 3
}
Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.