AWS开发工具包使用DynamoDB和putItem将不允许插入而不更新
这里是我如何插入一个项目到数据库中:
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
调用是因为我使用bluebird
对库进行了promisified
。 我试过这样做:
{ "Expected": { "UserId": { "Exists": false } } }
用我的putItem
调用,但没有运气。 所有我想要做的是插入logging没有现有的更新
来自PutItem API参考
要防止新项目replace现有项目,请使用包含attribute_not_exists函数的条件expression式,并使用用作表的分区键的属性的名称。 由于每个logging都必须包含该属性,因此只有在没有匹配项存在的情况下,attribute_not_exists函数才会成功。
首先,确保UserId
是表的PK,然后可以使用带有attribute_not_exists
ConditionExpression
:
DynamoDB.table('users').PutItem({}, { ConditionExpression: "attribute_not_exists(email)" }).then((response) => response.json()).then((res) => { // [LOG] possible bug with time })
这将只创build一个项目,如果电子邮件(我在表中的pk)以前不存在。
PS:我用react-native的dynamodb 封装 。