0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

存在しない属性名を使ってdynamoにupdateをかけたい時

Last updated at Posted at 2019-06-17

DynaoDBのMap属性をもつアイテムに対して更新をかけたいが、その際に存在していなキー名を追加して更新したい場合の覚書。

let obj = {
        [departmentCode]:{
            [userId]:role
        }
    }

let putParams = {
        TableName: tenantTable,
        Key: { tenantId : tenantId },
        UpdateExpression: 'set #u = :r',
        ExpressionAttributeNames: {
            '#u' : 'users',
        },
        ExpressionAttributeValues: {
          ':r' : obj
        }
      };

let putData = await docClient.update(putParams).promise();

簡単にいうと、存在しない属性名を使って更新をかけることはできない。
(ValidationException: The document path provided in the update expression is invalid for update

なので、確実に存在している属性名までをExpressionAttributeNamesにかき、その値としてオブジェクトを渡す。
オブジェクトはあくまで値をして渡されるだけなので、そのままの形で登録される。結果的にMap型として保存される。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?