This is a migrated thread and some comments may be shown as answers.

Return data with power fields?

4 Answers 155 Views
Cloud Code
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
JDBPocketware
Top achievements
Rank 2
JDBPocketware asked on 15 Oct 2014, 05:24 PM
Hi, is it possible for cloud code function to return data with power fields or relations?

A simple snippet sample would be appreciated.

4 Answers, 1 is accepted

Sort by
0
Anton Sotirov
Telerik team
answered on 16 Oct 2014, 01:57 PM
Hi Jandieg,

In order to return a data with relation, in the cloud code you will need to make a request to the api, using an expand expression, like this:
Everlive.CloudFunction.onRequest(function(request, response, done){
    var expandExpression = {
        Role: true
    };
    Everlive.Sdk.withMasterKey().data('Users').expand(expandExpression).get()
        .then(function(data){
            response.body = data;
            done();
        }, function(err){
            response.statusCode = 400;
            response.body = err;
            done();
        });
});

Using power fields is similar, with the difference you will need to use the 'X-Everlive-Power-Fields' header, like this:
Everlive.CloudFunction.onRequest(function(request, response, done){
    var powerFieldsExpression = {
        UsersCount: {
        "contentType" : "Users",
        "queryType" : "count"
        }
    };
    Everlive.Sdk.withMasterKey().data('Users')
         .withHeaders({'X-Everlive-Power-Fields': JSON.stringify(powerFieldsExpression)})
         .get()
         .then(function(data){
             response.body = data;
             done();
         }, function(err){
             response.statusCode = 400;
             response.body = err;
             done();
         });
});

For more information about defining relation and power fields expressions, you can check the following documentation articles:
- Defining power fields
- Specifying an expand definition

I hope this helps.

Regards,
Anton Sotirov
Telerik
 
Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
 
0
JDBPocketware
Top achievements
Rank 2
answered on 17 Oct 2014, 08:40 PM
Thanks!
0
Albert Anglin
Top achievements
Rank 1
answered on 05 Aug 2015, 06:06 PM

How would I modify this to include powerfields?

var dataProvider = app.everlive,
        dataSourceOptions = {
            type: 'everlive',
            transport: {
                typeName: 'Quest',
                dataProvider: dataProvider
            },
            schema: {
                model: {
                    fields: {
                        'Name': {
                            field: 'Name',
                            defaultValue: ''
                        },
                        'Description': {
                            field: 'Description',
                            defaultValue: ''
                        },
                        'QuestTypeID': {
                            field: 'QuestTypeID',
                            defaultValue: ''
                        },  
                        'TeamTypeID': {
                            field: 'TeamTypeID',
                            defaultValue: ''
                        },    
                        'RatingCount': {
                            field: 'RatingCount',
                            defaultValue: ''
                        },    
                        'RatingAverage': {
                            field: 'RatingAverage',
                            defaultValue: ''
                        },                  
                    }
                }
            },
            serverSorting: true,
            serverPaging: true,
            pageSize: 5
        }var dataProvider = app.everlive,
        dataSourceOptions = {
            type: 'everlive',
            transport: {
                typeName: 'Quest',
                dataProvider: dataProvider
            },
            schema: {
                model: {
                    fields: {
                        'Name': {
                            field: 'Name',
                            defaultValue: ''
                        },
                        'Description': {
                            field: 'Description',
                            defaultValue: ''
                        },
                        'QuestTypeID': {
                            field: 'QuestTypeID',
                            defaultValue: ''
                        },  
                       
                            field: 'RatingAverage',
                            defaultValue: ''
                        },                  
                    }
                }
            },
            serverSorting: true,
            serverPaging: true,
            pageSize: 5
        }

 So that, QuestTypeID and TeamTypeID returned the associated data?

 ​

0
Dimitar Dimitrov
Telerik team
answered on 07 Aug 2015, 01:33 PM
Hi Albert,

The better approach would be to use expand expression in order to get the additional data.

The expand expression definition is explained here - link.

For example:

{
    "QuestTypeID": {
        "TargetTypeName" : "<QuestTypeName-Here",
        "ReturnAs": "QuestTypeIdExpanded",
        "SingleField": "<the-field-you-want>"   
    },
    TeamTypeID"": {        
        "TargetTypeName" : "TeamsTypeName-Here",
        "ReturnAs": "TeamTypeIdExpanded",
         "SingleField": "<the-field-you-want>"   
    }
}

Then set the expand header in the datasource as shown in the example here - link

read: {
    headers: {
        "X-Everlive-Expand": JSON.stringify(expandExpression)
    }
}


If you need more help, do not hesitate to write us back.

Regards,
Dimitar Dimitrov
Telerik
 
Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
 
Tags
Cloud Code
Asked by
JDBPocketware
Top achievements
Rank 2
Answers by
Anton Sotirov
Telerik team
JDBPocketware
Top achievements
Rank 2
Albert Anglin
Top achievements
Rank 1
Dimitar Dimitrov
Telerik team
Share this question
or