Search code examples
amazon-web-servicesamazon-dynamodbaws-sdk

With DynamoDB and docClient, Is it possible to get return values when using transactWrite?


Let's say I am doing an update and a delete:

const transactionParams = {
    ReturnConsumedCapacity: "INDEXES",
    TransactItems: [
      {
        Delete: {
          TableName: reactionTableName,
          Key: {
            "SOME_PK_",
            "SOME_SK_",
          },
          ReturnValues: 'ALL_OLD',
        },
      },
      {
        Update: {
          TableName: reviewTableName,
          Key: { PK: "SOME_PK", SK: "SOME_SK" },
          ReturnValues: 'ALL_OLD',
        },
      },
    ],
  };

  try {
    const result = await docClient.transactWrite(transactionParams).promise();
  } catch (error) {
    context.done(error, null);
  }

even thoughI am using ReturnValues as "ALL_OLD" I can't seem to get access to that. Is this possible with transactWrite, or am I required to do a get after and eat up a read?


Solution

  • await docClient.transactWrite(transactWriteItemsQueryParam, (err, data) => {
            if (err) {
              console.error('Transaction Failed. Error JSON:', JSON.stringify(err, null, 2));
              reject(err);
            } else {
              console.log('Transaction succeeded:', JSON.stringify(data, null, 2));
                //data[0].Items -is result of delete query
                //data[1].Items -is result of update query
              resolve(data);
            }
          });