ItemCollectionMetrics is empty after successful transactWrite using DynamoDB.DocumentClient

1.5k Views Asked by At

I am executing a transactWrite (https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#transactWrite-property) instruction to DynamoDb and expecting to get back ItemCollectionMetrics (I am specifying ReturnItemCollectionMetrics: 'SIZE' in the request).

The object gets returned empty {} even though changes happened on the dynamo db tables.

Does anyone have an idea about this?

Code

const dynamoResponse = await dynamoDbDocumentClient.transactWrite({
        TransactItems: [
            {
                Put: {
                    TableName: ENV.BLAH_CONTENT_COUNT_MESSAGES_TABLE,
                    ExpressionAttributeNames : {
                        '#v' : 'v',
                    },
                    ExpressionAttributeValues : {
                        ':v' : blah.v
                    },
                    ConditionExpression: '(attribute_exists(blahId) AND #v<>:v) OR attribute_not_exists(blahId)',
                    Item: {...blahCountMessage}
                }
            },
            {
                Update: {
                    TableName: ENV.BLAH_CONTENT_COUNTS_TABLE,
                    Key: { id: blahContentCount.id },
                    ExpressionAttributeNames : {
                        '#v' : 'v',
                        '#count' : 'count',
                        '#contentId': 'contentId'
                    },
                    ExpressionAttributeValues : {
                        ':v' : 1,
                        ':count' : deleted ? -1: 1,
                        ':contentId': blahContentCount.contentId,
                        ':defaultNumber': 0
                    },
                    ConditionExpression: 'attribute_not_exists(cognitoId)',
                    UpdateExpression: 'SET #contentId = :contentId, #count = if_not_exists(#count, :defaultNumber) + :count, #v = if_not_exists(#v, :defaultNumber) + :v',
                    ReturnValuesOnConditionCheckFailure: 'ALL_OLD'
                }
            }
        ],
        ReturnItemCollectionMetrics: 'SIZE'
    }).promise();

Outupt

console.log(JSON.stringify(dynamoResponse.ItemCollectionMetrics)); // {}

Thanks!

1

There are 1 best solutions below

1
On BEST ANSWER

ReturnItemCollectionMetrics:

"Information about item collections, if any, that were affected by the operation. ItemCollectionMetrics is only returned if the request asked for it. If the table does not have any local secondary indexes, this information is not returned in the response."

Per: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_ItemCollectionMetrics.html

Could it be that this transaction is either not updating anything - or you don't have an LSI?

Also of note: The documentation says that transactions are disabled for "global tables", I'm not exactly sure if they mean a GSI... but I am not using a LSI which might be also why no ItemCollectionMetrics is ever returned in response to my transactions.

If you update your parameter object to include ReturnConsumedCapacity: 'TOTAL' the response object should look like:

   [ { TableName: 'table_name',
       CapacityUnits: 8,
       WriteCapacityUnits: 8 } ] }