Azure node.js用于更新/检索azure表存储中的实体

时间:2017-06-28 11:24:21

标签: azure azure-storage azure-functions azure-table-storage

如何更新Azure node.js函数以更新/检索azure表存储中的实体。我在函数中找到的方法只是插入一个条目。 那么如何查询/更新表格呢?

任务是在rowkey和分区键的基础上简单地检索数据,然后增加存储为{"num":val}的键值对的值。

1 个答案:

答案 0 :(得分:0)

请阅读Azure Functions Storage table bindings处的“存储表输入绑定”。它包含function.json和节点功能的示例。

如果某些内容仍然不明确,请使用确切的问题优化您的问题。

更新:

以下是针对您的优化问题的示例解决方案。我只在C#中使用它,我希望你可以派生节点实现。

csx

#r "Microsoft.WindowsAzure.Storage"

using System;
using System.Net;
using Microsoft.WindowsAzure.Storage.Table;

public class Entity : TableEntity
{
    public int num {get; set;}
}

public static HttpResponseMessage Run(HttpRequestMessage req, string partition, 
    string rowkey, Entity inputEntity, out Entity outputEntity)
{
    if (inputEntity == null)
        outputEntity = new Entity { PartitionKey = partition, RowKey = rowkey, num = 1};
    else
    {
        inputEntity.num += 1;
        outputEntity = inputEntity;
    }

    return req.CreateResponse(HttpStatusCode.OK, $"Done, num = {outputEntity.num}");
}

function.json

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "route": "HttpTriggerTableUpdate/{partition}/{rowkey}"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "table",
      "name": "inputEntity",
      "tableName": "MyTable",
      "partitionKey": "{partition}",
      "rowKey": "{rowkey}",
      "connection": "my_STORAGE",
      "direction": "in"
    },
    {
      "type": "table",
      "name": "outputEntity",
      "tableName": "MyTable",
      "partitionKey": "{partition}",
      "rowKey": "{rowkey}",
      "connection": "my_STORAGE",
      "direction": "out"
    }
  ],
  "disabled": false
}