DynamoDB - Dynamically Build an Update Expression
source link: https://gist.github.com/daverickdunn/4c6a0f61a0b969ec59e589353138bdc3
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Dynamically Build an Update Expression · GitHub
Instantly share code, notes, and snippets.
Outputs:
{ UpdateExpression: 'set #field = :field, #other = :other,',
ExpressionAttributeNames: { '#field': 'field', '#other': 'other' },
ExpressionAttributeValues: { ':field': { subfield: 123 }, ':other': '456' } }
Nice! Super useful appreciate you sharing
For single field updates, the trim function doesn't work on line 12. I would recommend changing it to a regex expression like so:
exp.UpdateExpression = exp.UpdateExpression.replace(/(^,)|(,$)/g, "")
For single field updates, the trim function doesn't work on line 12. I would recommend changing it to a regex expression like so:
exp.UpdateExpression = exp.UpdateExpression.replace(/(^,)|(,$)/g, "")
Hi Mason, thanks for the feedback, however I can't spot the issue you're describing. Can you give an example of a "single field update"? Thanks.
Edit: NM, I see the issue! trim
only removes whitespace. I'm not sure where I got the idea that it accepted an argument...
jericromero commented on May 3, 2021 •
it works. btw, does it support nested objects?
it works. btw, does it support nested objects?
It does not. If you're looking to update nested objects on a regular basis, then I suggest you consider using a different DB.
Edit: To be clear, what I mean is, if you want to update specific nested fields. If you're happy to update entire objects this works fine.
Thanks @daverickdunn! Exactly what I was looking for!
Thanks @daverickdunn .. it saves me a lot
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK