node.js - 从 Azure Function (Node.JS) 设置项目的 TimeToLive

标签 node.js azure azure-functions azure-cosmosdb

我有一个 Azure Function,其输出绑定(bind)到 Azure Cosmos DB 以保存项目。我正在尝试在项目级别设置 TimeToLive (我知道 TTL 必须打开才能使容器正常工作)。在容器级别,TTL 通过门户启用为 -1。有没有办法通过属性通过每个元素的 OutputBinding 设置 TTL?

我的绑定(bind):

{
  "type": "cosmosDB",
  "direction": "out",
  "name": "outputDocument",
  "databaseName": "myDatabase",
  "collectionName": "myCollection",
  "createIfNotExists": true,
  "PartitionKey": "/customerId",
  "defaultTtl": -1, //doesn't work and has to be set via Azure Portal
  "connectionStringSetting": "CosmosDbConnectionString"
}

当然还有我将数据写入 Cosmos DB 的 index.ts 部分:

const httpTrigger: AzureFunction = async function (context: Context, eventGridEvent: EventGridEvent<DTO>): Promise<void> {
    context.bindings.outputDocument = JSON.stringify(eventGridEvent.data);
}
export default HttpTrigger;

最佳答案

该项目需要添加一个“ttl”属性。

假设您的 eventGridEvent.data 包含您要保存的完整文档,您可以执行以下操作:

var item = eventGridEvent.data;
item["ttl"] = 60; // 60 seconds
context.bindings.outputDocument = JSON.stringify(item);

关于node.js - 从 Azure Function (Node.JS) 设置项目的 TimeToLive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74234730/

相关文章:

azure - 在需要提升权限的 Azure WebJob 中运行脚本

javascript - npm - EPERM : operation not permitted on Windows

azure - 无法使用不记名 token 访问 AAD 安全 Web API

javascript - Node.js 多个 Express 对象

azure - 与 ADF 同步增量数据

python - 尽管响应状态为 202,Azure 文档翻译器无法工作

Azure API 管理策略 - 使用条件语句 - 对于有效负载

azure - 是否可以使用队列名称模式或为多个队列创建队列触发的azure函数?

node.js - 如何在JavaScript中调试c++生成的* .so文件?

node.js - 通过字符串长度搜索弹性查询