【发布时间】:2023-04-07 05:23:01
【问题描述】:
我只想向 DynamoDb 添加 id + 一些值。如果 id 已经存在,它应该什么都不做或更新
我可以去
search
if not found > insert
if found > do nothing or update (for now do nothing is fine)
但希望有更好的方法来做到这一点。 id 应该是检查的关键。
这是节点中的代码:
const dynamodbParams = {
TableName: process.env.DYNAMODB_TABLE_BLICKANALYTICS,
Item: {
id: userId,
createdAt: timestamp
},
};
dynamoDb.put(dynamodbParams).promise()
.then(data => {
console.log('saved: ', dynamodbParams);
})
.catch(err => {
console.error(err);
});
我在 yml 中使用它。不知道yml中是否有设置这个选项
resources:
Resources:
DynamoDbTableExpenses:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: Retain
Properties:
AttributeDefinitions:
-
AttributeName: id
AttributeType: S
-
AttributeName: createdAt
AttributeType: N
KeySchema:
-
AttributeName: id
KeyType: HASH
-
AttributeName: createdAt
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:provider.environment.DYNAMODB_TABLE_BLICKANALYTICS}
【问题讨论】:
标签: node.js amazon-web-services amazon-dynamodb