在 Azure Table 存储的插入功能中查询其他表

Query other tables in insert function of Azure Table Storage

我正在使用 Azure Table 存储在插入新项目后发送推送通知。目前我正在尝试查询第二个 table 以检索我想用于推送通知的字符串。

我是 Node.js 的新手,所以我研究了一些代码并尝试了以下操作以在 TestTable 上开始查询以找到基于 TestProperty 的正确实体。找到正确的实体后,我想使用其中的特定 属性 进行处理。

这是我当前代码遇到的错误:

TypeError: Cannot read property 'table' of undefined

我尝试查询第二个代码的部分代码 table

var azureMobileApps = require('azure-mobile-apps'),
tables = require('azure-mobile-apps/src/express/tables'),
queries = require('azure-mobile-apps/src/query'),
logger = require('azure-mobile-apps/src/logger');

var table = azureMobileApps.table();

table.insert(function (context) {
    logger.info('Running TestTable1.insert');
    var testTable = azureMobileApps.tables.table('TestTable2');
    var query = queries.create('TestTable2').where({ TestProperty : context.item.testproperty }); 

    return context.execute()
        .then(function (results) {
        .....

由于AzureTable存储是一种在云端存储非结构化NoSQL数据的服务,您可以参考https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-tables/了解更多。

但是,azure-mobile-apps-node sdk 中的 tables 模块包含将 table 添加到 Azure 移动应用程序的功能。它 returns 一个可以附加到 express 应用程序的路由器,具有一些用于注册 table 的附加功能。这实际上利用了 Azure SQL(SQL Azure 上的服务器数据库服务)。

根据您的代码片段,您似乎正在实施第二个概念。

根据你的描述,如果我没有理解错的话,你想在 EasyTables 脚本中的 table1 操作中查询 table2

我们可以利用 "use()" 自定义中间件来指定要针对 table 的每个请求执行的中间件,如 [=15= 的 azure-mobile-apps sdk 文档中的描述].

例如

var queries = require('azure-mobile-apps/src/query');
var insertMiddleware = function(req,res,next){
    var table = req.azureMobile.tables('table2'),
    query = queries.create('table2')
            .where({ TestProperty : req.body.testproperty });
    table.read(query).then(function(results) {
        if(results){
            req.someStoreData = somehander(results); //some hander operations here to get what you want to store and will use in next step
            next();
        }else{
            res.send("no data");
        }
    });
};

table.insert.use(insertMiddleware, table.operation);
table.insert(function (context) {
   console.log(context.req.someStoreData);
   return context.execute();
});

此外,如果您需要在 EasyTables 脚本中推送通知,您可以参考 Github 上的示例 https://github.com/Azure/azure-mobile-apps-node/blob/master/samples/push-on-insert/tables/TodoItem.js

谢谢加里。这非常有帮助。 要完成您的回答,您现在可以这样写:

async function filterByAllowedDomain(context) {
  var domains = await context.tables('domains')
    .where({ allowed: true })
    .read();

  var categories = await context.tables('categories')
    .where(function (ids) {
        return this.domainId in ids;
    }, domains.map(d => d.id))
    .read();

  context.query.where(function (ids) {
    return this.categoryId in ids;
  }, categories.map(c => c.id));

  return context.execute(); }