在 Azure 表存储的插入函数中查询其他表

Query other tables in insert function of Azure Table Storage

本文关键字:查询 函数 其他 Azure 存储 插入      更新时间:2023-09-26

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

我对 Node 很陌生.js所以我通过一些代码并尝试了以下内容来启动对 TestTable 的查询,以基于 TestProperty 找到正确的实体。找到正确的实体后,我想使用它中的特定属性来处理。

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

TypeError: Cannot read property 'table' of undefined

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

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) {
        .....

由于 Azure 表存储是一项将非结构化 NoSQL 数据存储在云中的服务,因此可以参考 https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-tables/以获取更多信息。

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

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

根据您的描述,如果我没有误解,您希望在 EasyTables 脚本中的table1操作中查询table2

我们可以利用自定义中间件的"use()"来指定要针对表的每个请求执行的中间件,如 Azure 移动应用 SDK 文档在 http://azure.github.io/azure-mobile-apps-node/module-azure-mobile-apps_express_tables_table.html#~use 上的描述。

例如

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(); }