gpt4 book ai didi

node.js - dynamodb 从 Nodejs 循环中更新插入

转载 作者:行者123 更新时间:2023-12-02 03:09:03 24 4
gpt4 key购买 nike

所以基本上我有一个数组(features),并且想要从 forEach-loop< 在 dynamodb 中执行 upsert 操作features 数组如下所示:[{'feature': 'existing_feature', 'visibility': ['should_update']}, {'feature': 'new_feature', 'visibility' : ['should_insert']} ]。现在这里更新按预期发生,但插入没有发生。

update(table, params) {
const query = {
...params,
TableName: this.tableName(table)
}
console.log('update-query', query)
return this._docClient.update(query).promise()


}

updateSupportTierDetails(features) {
console.log('inside updateSupportTierDetails')
const promises = []

features.forEach(element => {
const params = {
Key: {
'feature': element.feature
},
UpdateExpression: 'set visibility = :visibility',
ExpressionAttributeValues: {
':visibility': element.visibility
},
ReturnValues: "UPDATED_NEW"
}
console.log('updateSupportTierDetails params', params)
const promise = this.update('features', params)
console.log('promise', promise)
promises.push(promise)
});

console.log('promises', promises)

return Promise.all([...promises])
.then(result => {
console.log('result', result)
if(result.count === 0) {
console.log('updateSupportTierDetails: No Upsert Operation')
return []
}
console.log('updateSupportTierDetails: Upsert Operation Success')
return result
})


}

最佳答案

DynamoDb put 将插入一条新记录(如果不存在)或更新现有记录(如果存在)(基于分区键)

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property

但是,如果您要循环更新插入多个项目,请考虑使用批量写入。

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#batchWrite-property

关于node.js - dynamodb 从 Nodejs 循环中更新插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58179966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com