Azure易表格自定义API

4

我在这篇文章中看到,Easy Tables 可以具有关系型数据库的功能,例如连接等,在查询数据时可以结合多个表。不幸的是,我没有找到如何实现这一点的详细信息。

在我的情况下,我有一个名为 "user" 的 Easy Table 和另一个名为 "subject choice" 的 Easy Table,两个表都有共同的 "userid" 属性,我需要根据这两个表中的信息在移动服务应用程序中检索信息。

我该怎么做呢?

2个回答

3
根据您的描述,我按照这个教程开始了Azure Mobile App的Easy Tables。在我看来,Easy Tables可以为您的移动应用程序提供一个简单的方式来添加后端数据存储。
我们可以将操作包装到特定的表中,如下所示:
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可以在查询数据时像连接等关系型数据库功能一样具有联接表的功能。

据我所知,您可以在Easy Table中添加外键以创建关系。但是无法在单个查询中检索多个表中的数据。对于您的情况,您可以调用UserDataService.GetUserAsync和SubjectChoiceDataService.GetByUserIdAsync方法来按预期聚合数据。


3
您可以从易表API脚本编辑器中操纵易表查询。对于每个您创建的“易表”,都将有一个.json和.js文件。其中包含了有注释的table.read函数,其签名与下面的示例相同。取消此函数的注释并修改内容,允许进行任何类型的自定义操作。因此,如果要执行“Join”操作,您可以自己获取两个表中的记录,然后返回一个“合并”的JSON对象。下面是从存储中获取数据并决定何时返回的示例:
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();
     });
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接