Return data with power fields?

5 posts, 0 answers
  1. Jandieg
    Jandieg avatar
    43 posts
    Member since:
    Feb 2012

    Posted 15 Oct 2014 Link to this post

    Hi, is it possible for cloud code function to return data with power fields or relations?

    A simple snippet sample would be appreciated.
  2. Anton Sotirov
    Admin
    Anton Sotirov avatar
    4 posts

    Posted 16 Oct 2014 Link to this post

    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.
     
  3. Jandieg
    Jandieg avatar
    43 posts
    Member since:
    Feb 2012

    Posted 17 Oct 2014 in reply to Anton Sotirov Link to this post

    Thanks!
  4. Albert Anglin
    Albert Anglin avatar
    7 posts
    Member since:
    Mar 2007

    Posted 05 Aug 2015 in reply to Anton Sotirov Link to this post

    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?

     ​

  5. Dimitar Dimitrov
    Admin
    Dimitar Dimitrov avatar
    26 posts

    Posted 07 Aug 2015 Link to this post

    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.
     
Back to Top