shruti iyyer
shruti iyyer

Reputation: 260

azure easy tables custom api

I read in this post that easy tables can have relational database features like join etc for combining tables while querying data. Unfortunately I haven't been able to find much about how to go about doing this.

In my case i have an easy table user and another called subject choice having userid attribute in common and i need to retrieve information in a mobile service app based on info in both of these tables.

How do I go about this?

Upvotes: 4

Views: 624

Answers (2)

user230910
user230910

Reputation: 2372

You can manipulate the easy tables query from the easy api script editor.

You will see a .json and .js file for each "easy table" you created. There will be a commented table.read function with the same signature as my example below. Uncommenting this function and modifying the content allows any kind of custom manipulation server side.

So to do a "Join" you COULD fetch the records from both tables yourself, and then return a "combined" json object.

Here is an example of fetching the data from the storage and deciding when to return:

table.read(function (context) {
 var response = context.execute();
 response.then(function (records)
     {
        console.log("Records Fetched: " , records);
        // HERE YOU CAN MESS WITH THE DATA
        context.res.status(200).send(records);
        context.done();
     }, function (error)
     {
         console.log(error);
         context.res.status(500).send(error);
             context.done();
     });
});

Upvotes: 3

Bruce Chen
Bruce Chen

Reputation: 18465

According to your description, I followed this tutorial for getting started with the Easy Tables of Azure Mobile App. In my opinion, Easy Tables could provide you with a simple way to add back-end data storage for your mobile app.

We could wrap the operations to the specific table as follows:

public class UserDataService
{
    MobileServiceClient mobileService;
    IMobileServiceTable<User> userTable;

    public void Initialize()
    {
        mobileService= new MobileServiceClient("http://<your-mobileapp-name>.azurewebsites.net/");
        userTable = mobileService.GetTable<User>();
    }

    public async Task<User> GetUserAsync(string uid)
    {
        return await this.userTable.LoolupAsync(uid);
    }

    public async Task AddUserAsync(User user)
    {
        await this.userTable.InsertAsync(user);
    }
}

easy tables can have relational database features like join etc for combining tables while querying data.

As far as I know, you could add a foreign key in a Easy Table for creating your relationships. But you couldn't retrieve data from multiple tables in a single query. For your scenario, you could invoke the methods UserDataService.GetUserAsync and SubjectChoiceDataService.GetByUserIdAsync to aggregate the data as you expected.

Upvotes: 3

Related Questions