DynamoDB createIfNotExist
DynamoDB createIfNotExist
在 dynamoDB 中,我应该创建某种日志信息,它具有用户可以更改的状态。但这是不必要的额外信息。主要问题是:
- 我有一个对象:
{
id: '123',
log: 'mybestlog',
state: 'PENDING'
}
- 如果具有相同
id
的文档不存在,我想创建此文档对象。
- 如果具有相同
id
的对象退出,我想取回当前对象和标志。
希望我说的够清楚。
我试过:
const result: PutItemOutput = await this.dynamodbClient.put({
TableName: this.config.tableName,
Item: {
...newItem,
status: statusEnum.PENDING,
creationDate: moment().toISOString(),
},
ConditionExpression: 'attribute_not_exists(id)',
ReturnValues: 'ALL_NEW',
})
.promise()
.catch((error) => {
this.logger.error(`### END-ERROR ${methodDescription} | error: ${error}`, metadata);
throw new InternalServerErrorException(error.stack);
});
但在响应中找不到任何 Items
或标志 Exist
。我得到了 AWS.DynamoDB.PutItemOutput
:
{
Attributes?,
ConsumedCapacity?,
ItemCollectionMetrics?,
}
请帮助我,在我的情况下最好的解决方案是什么。
您不会像您所想的那样收到带有标志的响应,表明项目已退出 dynamoDB。
当您使用那个 ConditionExpression 调用 put 操作时。两个主要成果和对您重要的成果是:
- 您的 ConditionExpression 成功,您将在 Attributes[=25= 中获得 ALL_NEW 值]键。
- 否则你会得到一些错误。对您来说重要的是 ConditionalCheckFailedException,这意味着 ConditionExpression 失败并且密钥已经存在。
您需要自己相应地调整响应。
在 dynamoDB 中,我应该创建某种日志信息,它具有用户可以更改的状态。但这是不必要的额外信息。主要问题是:
- 我有一个对象:
{
id: '123',
log: 'mybestlog',
state: 'PENDING'
}
- 如果具有相同
id
的文档不存在,我想创建此文档对象。 - 如果具有相同
id
的对象退出,我想取回当前对象和标志。
希望我说的够清楚。
我试过:
const result: PutItemOutput = await this.dynamodbClient.put({
TableName: this.config.tableName,
Item: {
...newItem,
status: statusEnum.PENDING,
creationDate: moment().toISOString(),
},
ConditionExpression: 'attribute_not_exists(id)',
ReturnValues: 'ALL_NEW',
})
.promise()
.catch((error) => {
this.logger.error(`### END-ERROR ${methodDescription} | error: ${error}`, metadata);
throw new InternalServerErrorException(error.stack);
});
但在响应中找不到任何 Items
或标志 Exist
。我得到了 AWS.DynamoDB.PutItemOutput
:
{
Attributes?,
ConsumedCapacity?,
ItemCollectionMetrics?,
}
请帮助我,在我的情况下最好的解决方案是什么。
您不会像您所想的那样收到带有标志的响应,表明项目已退出 dynamoDB。
当您使用那个 ConditionExpression 调用 put 操作时。两个主要成果和对您重要的成果是:
- 您的 ConditionExpression 成功,您将在 Attributes[=25= 中获得 ALL_NEW 值]键。
- 否则你会得到一些错误。对您来说重要的是 ConditionalCheckFailedException,这意味着 ConditionExpression 失败并且密钥已经存在。
您需要自己相应地调整响应。