Nesting Relation Expands

As explained in Expand Expression Format, expands can be nested. Nesting allows you to go one or more relation levels deeper and expand fields of expanded data. It is a subject of limitations discussed in Limitations and Design Considerations.

To demonstrate nesting, we'll use the data model from Introduction to Data Relations. The aim is to create a single request that takes an activity and the comments to that activity along with information about the comment author.

First, let's start with a main request and its result. You can test the request by picking the activity with Text: "It is finally time for graduation..." from the Friends sample social app.

var everliveApp = new Everlive('your-app-id');

var data = everliveApp.data('Activities');
data.getById(item-id)
    .then(function(data){
        console.log(JSON.stringify(data));
    },
    function(error){
        console.log(JSON.stringify(error));
    });

The request above returns the following:

{
    "Result": {
        "Id": "activityId",
        "Text": "It is finally time for graduation! Good job everyone, we made it.",
    ...
        "Likes": [
            "userId"
        ]
    }
}

Next, by adding an expand expression, we can replace the activityId with the actual comment object from the Comments content type. We use a relation to retrieve the comments that have the same activityId as the Id of the current activity.

var everliveApp = new Everlive('your-app-id');

var expandExp = {
   "Comments.ActivityId": true
};

var data = everliveApp.data('Activities');
data.expand(expandExp).getById(item-id)
    .then(function(data){
        console.log(JSON.stringify(data));
    },
    function(error){
        console.log(JSON.stringify(error));
    });

The expanded result looks as follows:

{
    "Result": {
        "Comments.ActivityId": [{
            "Comment": "Congratulations!",
            "ActivityId": "activityId",
            "CreatedBy": "UserId1",
            ...
        }, {
            "Comment": "Thanks :)",
            "ActivityId": "activityId",
            "CreatedBy": "UserId2",
            ....
        }],     
        "Text": "It is finally time for graduation! Good job everyone, we made it.",
        ...
        "Likes": [
            "userId"
        ]
    }
}

The returned Comments object contains user IDs of comment authors, but we want to have the actual user names instead. We can use a nested expand expression to expand the CreatedBy field in each comment:

var everliveApp = new Everlive('your-app-id');

var expandExp = {
   "Comments.ActivityId": {
      "ReturnAs": "Comments",
      "Expand": {
         "CreatedBy": {
            "TargetTypeName" : "Users",
            "ReturnAs" : "Author"
            }
      }
   }
};

var data = everliveApp.data('Activities');
data.expand(expandExp).getById(item-id)
    .then(function(data){
        console.log(JSON.stringify(data));
    },
    function(error){
        console.log(JSON.stringify(error));
    });

The nested expand expression specifies the related content type (Users) using the TargetTypeName option and also renames CreatedBy to Author using ReturnAs. We also added the ReturnAs option to rename the Comments.ActivityId property in the result to Comments.

And the final result:

{
    "Result": {
        "Comments": [{
            "Comment": "Congratulations!",
            "ActivityId": "activityId",
            "Author": { // CreatedBy is now a resolved user
                "Username": "seth",
                ...
            },
            ...
        }, {
            "Comment": "Thanks :)",
            "ActivityId": "activityId",
            "Author": {
                "Username": "michael",
                ...
            },
            ...
        }],
        "Text": "It is finally time for graduation! Good job everyone, we made it.",
        ...
        "Likes": [
            "userId"
        ]
    }
}

As you can see, expand expressions can be nested to help you get the desired result. Each of the expand expressions used in this example can be further extended with advanced options. For example, you could sort Comments by CreatedAt or return only the Username of each user.

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.