gpt4 book ai didi

node.js - 使用 DynamoDB 和 putItem 的 AWS SDK 不允许我在不更新的情况下插入

转载 作者:搜寻专家 更新时间:2023-11-01 00:43:34 25 4
gpt4 key购买 nike

下面是我如何将一个项目插入数据库:

DynamoDB.putItemAsync({
"TableName": tblName,
"Item": {
"UserId": { "S": String(obj.user_id) },
"CampaignId": { "N": String(obj.campaign_id) },
"Email": { "S": String(obj.email) },
"CustomActivityNodeId": { "N": String(obj.custom_activity_node_id) }
},
"Expected": {
"UserId": {
"Exists": false
}
}
})

putItemAsync 调用是因为我使用 bluebirdpromisified 库。我试过这样做:

{
"Expected": {
"UserId": {
"Exists": false
}
}
}

通过我的 putItem 调用,但运气不好。我只想插入记录而不更新现有记录

最佳答案

来自 PutItem API reference

To prevent a new item from replacing an existing item, use a conditional expression that contains the attribute_not_exists function with the name of the attribute being used as the partition key for the table. Since every record must contain that attribute, the attribute_not_exists function will only succeed if no matching item exists.

首先,确保 UserId 是表的 PK,然后你可以使用 ConditionExpressionattribute_not_exists:

DynamoDB.table('users').PutItem({}, {
ConditionExpression: "attribute_not_exists(email)"
}).then((response) => response.json()).then((res) => {
// [LOG] possible bug with time
})

这将创建一个项目ONLY 如果电子邮件(我在表中的 pk)以前不存在。

附言:我使用的是 wrapper用于 react-native 中的 dynamodb。

关于node.js - 使用 DynamoDB 和 putItem 的 AWS SDK 不允许我在不更新的情况下插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26540763/

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