LoginSignup
0
0

More than 3 years have passed since last update.

DynamoDBでアイテム新規作成時にcreatedAt, updatedAtを保存し、更新時にはupdatedAtだけを更新する

Posted at

if_not_existsを使用してupdateItemを行えばよい。

    // use if_not_exists()
    UpdateExpression: `SET #QQ_ID = :answer, #updatedAt = :updatedAt, #createdAt = if_not_exists(#createdAt, :createdAt)`,
    ExpressionAttributeNames: {
        '#QQ_ID'   : `QQ_${question_id}`,
        '#updatedAt': 'UpdatedAt',
        '#createdAt': 'CreatedAt',
    },
    ExpressionAttributeValues: {
        ':answer': answer,
        ':updatedAt' : now.toISOString(), 
        ':createdAt' : now.toISOString(), 
    }

if_not_exists (path, value)
指定された path で項目が属性を含まない場合、if_not_exists は value に評価されます。それ以外の場合は、path に評価されます。
https://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.SET

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0