dynamodb-data-types icon indicating copy to clipboard operation
dynamodb-data-types copied to clipboard

updateExpr() and nested values

Open tomhoag opened this issue 2 years ago • 4 comments

Prior to trying out updateExpr(), I have a working set of UpdateItemCommand parameters that includes:

{
  TableName: 'MyTable',
  Key: { PK: { S: 'VDR123' }, SK: { S: 'VEHTESTER165' } },
  UpdateExpression: 'ADD #data.deliveryIds :deliveryIds',
  ExpressionAttributeValues: { ':deliveryIds': { SS: [Array] } },
  ExpressionAttributeNames: { '#data': 'data' },
  ReturnValues: 'UPDATED_NEW'
}

Note the nested attribute 'deliveryIds' in the data element, and that 'data' is a dynamoDB reserved word.

I expected that updateExpr() yield similar UpdateExpression, ExpressionAttributeValues and ExpressionAttributeNames:

const updates = updateExpr()
		.add({'data.deliveryIds': [deliveryId]});
const { UpdateExpression,  ExpressionAttributeValues, ExpressionAttributeNames } = updates.expr();

However, this is the result:

UpdateExpression ADD data.deliveryIds :a ExpressionAttributeValues { ':a': { SS: [ 'DEL#31415' ] } } ExpressionAttributeNames undefined

Note that 'data' is not aliased and when used with the UpdateItemCommand, fails.

Am I using updateExpr() incorrectly? or does it not currently support nested with reserved words?

Thanks!

tomhoag avatar Apr 26 '22 20:04 tomhoag

@tomhoag Thanks for pointing this out.

This library is missing support for nested attributes in Document Paths.

I will implement it within the next 4 to 5 days because this library should support it.

kayomarz avatar Apr 27 '22 05:04 kayomarz

fwiw -- this is my work around for the time being:

const knownReservedWords = ['data','status'];
let usedReservedWords = [];
for(const word of knownReservedWords) {
	if(UpdateExpression.includes(word)) {
		usedReservedWords.push(word);
		UpdateExpression = UpdateExpression.replace(new RegExp(word, 'g'), '#'+word);
	}
}
ExpressionAttributeNames = ExpressionAttributeNames ?? {};
for(const word of usedReservedWords) {
	const key = '#'+word;
	ExpressionAttributeNames[key] = word;
}

knownReservedWords is the list of words that I know that are being used in my code. The list needs to be much more extensive, check for case, etc.

tomhoag avatar Apr 29 '22 12:04 tomhoag

@tomhoag After working on this, I realize there are other cases too, where nested document paths are not handled. I've started working on it and will complete it over the coming weekend. Until then please can you continue using your workaround.

kayomarz avatar May 02 '22 12:05 kayomarz

@kayomarz Will do. Thanks for the status update!

tomhoag avatar May 03 '22 11:05 tomhoag