0

I am working on one query where I need to use TrasactWrite of dynamoDB and perform update query on one of my table.

Scenario is I am having array of unique IDs and I need to perform same update operation on each ID in array and change one flag in table without using Loop or map.

But I am having difficulty in finding example or docs related to how do I use IN operator on IDs which are primary keys in a Key parameter of transactWrite.

Here is my sample code:

let IDs = await find('table', { type: 'some_type' }, 'type-index}
let params = {
                TransactItems: [{
                    Update: {
                        TableName: 'table',
                        Key: '#_id IN :IDs,
                        UpdateExpression: 'set #flag = :flag',
                        ExpressionAttributeNames: {
                            '#flag': 'flag',
                            '#_id': '_id'
                        },
                        ExpressionAttributeValues: {
                            ':flag': false,
                            ":IDs": IDs
                        }
                    }
                }]
            }

Already tried: Answer Its not same question as mine

I will answer if anyone has any question. Any help will be really helpful. Thank you

K P
  • 854
  • 7
  • 19

1 Answers1

2

You can't use IN operator in this case - Update. With update operator you have to put key value to Update object. A key includes partitionKey and sortKey, I think in your case, you just set partitionKey.

To update more than one item by ID, you can put many Update object to TransactItems.

let IDs = await find('table', { type: 'some_type' }, 'type-index');

let params = {
  TransactItems: [], // init empty array
}

IDs.forEach((ID) => {// loop though IDs array
  params.TransactItems.push({
    Update: {
      TableName: 'table',
      Key: {
        '_id': ID, // ID value
      },
      UpdateExpression: 'set #flag = :flag',
      ExpressionAttributeNames: {
        '#flag': 'flag',
        // '#_id': '_id' // remove this line
      },
      ExpressionAttributeValues: {
        ':flag': false,
        // ":IDs": IDs // remove this line
      }
    }
  })
});
// await client.transactWrite(params).promise()
hoangdv
  • 15,138
  • 4
  • 27
  • 48
  • thank you for answer. though I am not allowed to use loops, now I know that it isn't possible the way i want. we will figure out something else. – K P Apr 22 '20 at 10:09