Using External Libraries in Cloud Code

Using External Libraries in Cloud Code

You can include a limited set of external JavaScript libraries to leverage their functionality and save time when writing Cloud Code. They are accessible in Cloud Code for Data and Cloud Functions.

The list of JavaScript libraries that you can use includes:

You don't need to download the libraries because they are already available in the Cloud Code API. For the same reason, you are limited to using the versions cited above.

Underscore

Cloud Code provides the following version of this library: 1.8.2.

Official documentation: Underscore

The following Cloud Function returns an array of user account IDs grouped by their Age and Name fields.

 Everlive.CloudFunction.onRequest(function (request, response, done) {
    var _ = require('underscore');

    Everlive.Sdk.$.data('Users').get({}, function (data) {
        var usersArray = data.result;

        var groups = _.groupBy(usersArray, function (user) {
            return user.Name + ' ' + user.Age;
        });

        var groupedData = _.map(groups, function (group) {
            return {
                userId: group[0].Name,
                age: group[0].Age,
                name: _.pluck(group, 'Id')
            };
        });

        response.result = JSON.stringify(groupedData);
        response.headers = {
            "Content-Type": "application/json"
        };
        response.statusCode = 200;
        done();
    }, function (err) {
        response.statusCode = 500;
        response.result = JSON.stringify(err);
        done();
    });
});

RSVP.js

Cloud Code provides the following version of this library: 3.0.17.

Official documentation: RSVP.

The following Cloud Function makes a separate update request for each element of the passed array. This is useful when you cannot use Updating Multiple Items because the items to update don't match common filter criteria.

Everlive.CloudFunction.onRequest(function(request, response, done) {
    var rsvp = require('rsvp');
    var bookPrices = request.data.NewPrices;
    var promises = [];
    var arrayLength = bookPrices.length;

    for (var index = 0; index < arrayLength; index+=1) {
        var bookToUpdate = bookPrices[index];
        var createPromise = Everlive.Sdk.$.data('Books').updateSingle({
            Id: bookToUpdate.Id,
            Price: bookToUpdate.NewPrice
        });

        promises.push(createPromise);
    }

    rsvp.all(promises).then(function(res) {
        done();
    }, function(err) {
        done(err);
    });
});

Moment.js

Cloud Code provides the following version of this library: 2.1.0.

Official documentation: Moment.js.

var moment = require('moment');

Moment Timezone

Cloud Code provides the following version of this library: 0.0.2.

Official documentation: Moment Timezone.

var momentTimezone = require('moment-timezone');

Async

Cloud Code provides the following version of this library: 1.2.1.

Official documentation: Async.

var async = require('async');

Node.js Buffer

Cloud Code provides the following version of this library: core library of Node.js v.4.5.0.

Official documentation: Node.js Buffer.

var buffer = require('buffer');

Node.js Crypto

Cloud Code provides the following version of this library: core library of Node.js v.4.5.0.

Official documentation: Node.js Crypto.

var crypto = require('crypto');

Node.js Query String

Cloud Code provides the following version of this library: core library of Node.js v.4.5.0.

Official documentation: Node.js Query String.

var querystring = require('querystring');

Node.js Url

Cloud Code provides the following version of this library: core library of Node.js v.4.5.0.

Official documentation: Node.js Url.

var url = require('url');

See Also

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.