通过azure函数更新和删除cosmos db中的文档

问题描述 投票:1回答:1

我是宇宙数据库以及天蓝色功能的新手,我无法快速完成任务。我已经能够在阳光下找到每个教程来创建和阅读文档,但不能更新和删除。似乎没有人使用azure函数获得完整的CRUD教程。

有人可以在azure函数中向我显示一个典型的.csx文件,该文件包含文档,更新它并返回OK响应吗?

我已经尝试过了

#load "..\Shared\Classes.csx"
using System.Net;

public static HttpResponseMessage Run(HttpRequestMessage req, 
IEnumerable<Business> businessToBeUpdated, out dynamic updatedBusiness, 
TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");

//compiler requires this assignment
updatedBusiness = null;

// Get request body
Business data = req.Content.ReadAsAsync<Business>().Result;
businessToBeUpdated = businessToBeUpdated.FirstOrDefault<Business>();
log.Info(businessToBeUpdated.Count().ToString());
if(businessToBeUpdated != null && data != null)
{
    //update it
    businessToBeUpdated = data;
    //updatedBusiness.id = data.id;
    log.Info(businessToBeUpdated.website);
}
else{
    return req.CreateResponse(HttpStatusCode.BadRequest);
}

return req.CreateResponse(HttpStatusCode.OK);
}

这是与之相关的绑定。

{
"bindings": [
{
  "authLevel": "anonymous",
  "name": "req",
  "type": "httpTrigger",
  "direction": "in",
  "route": "updatebiz/{id}"
},
{
  "name": "$return",
  "type": "http",
  "direction": "out"
},
{
  "type": "documentDB",
  "name": "businessToBeUpdated",
  "databaseName": "dbname",
  "collectionName": "Businesses",
  "sqlQuery": "Select * FROM c where c.id = {id}",
  "connection": "connection",
  "direction": "in"
},
{
  "type": "documentDB",
  "name": "updatedBusiness",
  "databaseName": "dbname",
  "collectionName": "Businesses",
  "createIfNotExists": false,
  "connection": "connection",
  "direction": "out"
  }
],
"disabled": false
}
azure azure-cosmosdb azure-functions csx
1个回答
4
投票

更新文档的函数的最简单示例看起来与创建文档的函数完全相同:函数将根据指定的id文档是否已存在来执行其中一个。

你没有提到你面临的确切问题。您的代码甚至没有编译,具有相互分配的可枚举和单个对象。最重要的是,你永远不会将updatedBusiness分配给null以外的任何东西。

我带来了一个实际的例子,它做了我认为你想要完成的事情。

csx脚本:

using System.Net;

public class Business
{
    public string id { get; set;}
    public string name { get; set;}
}

public static HttpResponseMessage Run(HttpRequestMessage req, 
    Business businessToBeUpdated, out Business updatedBusiness, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");

    var data = req.Content.ReadAsAsync<Business>().Result;
    if(businessToBeUpdated == null || data == null)
    {
        updatedBusiness = businessToBeUpdated;
        return req.CreateResponse(HttpStatusCode.BadRequest);
    }

    updatedBusiness = data; 
    // or merge data and businessToBeUpdated in some desired way

    return req.CreateResponse(HttpStatusCode.OK);
}

function.json

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "route": "updatebiz/{id}"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "documentDB",
      "name": "businessToBeUpdated",
      "databaseName": "dbname",
      "collectionName": "Businesses",
      "id": "{id}",
      "connection": "connection",
      "direction": "in"
    },
    {
      "type": "documentDB",
      "name": "updatedBusiness",
      "databaseName": "dbname",
      "collectionName": "Businesses",
      "id": "{id}",
      "connection": "connection",
      "direction": "out"
    }
  ],
  "disabled": false
}
© www.soinside.com 2019 - 2024. All rights reserved.