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

3

9

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?

Innominate answered 18/5, 2020 at 16:19 Comment(0)
S
-2
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);
        }
      });
Snooker answered 21/5, 2020 at 11:50 Comment(1)
This doesn't compile (typescript) and is not complient with the docs... I really wonder if this is the correct answer. See docs.aws.amazon.com/amazondynamodb/latest/APIReference/…Badenpowell
M
0

Have you checked out ItemCollectionMetrics in the response? The docs suggest its a map of table name -> items affected by the query

https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_TransactWriteItems.html#API_TransactWriteItems_ResponseElements

Multiflorous answered 20/5, 2020 at 20:52 Comment(1)
Metrics are only, well, metrics. They contain stuff like affected tables and the affected item count, not full items.Ninurta
A
0

No. According to the SDK documentation, the TransactWriteCommandOutput only contains ItemCollectionMetrics, no Attributes.

Alleged answered 11/6, 2024 at 5:25 Comment(0)
S
-2
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);
        }
      });
Snooker answered 21/5, 2020 at 11:50 Comment(1)
This doesn't compile (typescript) and is not complient with the docs... I really wonder if this is the correct answer. See docs.aws.amazon.com/amazondynamodb/latest/APIReference/…Badenpowell

© 2022 - 2025 — McMap. All rights reserved.