7

DynamoDB - Dynamically Build an Update Expression

 1 year ago
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.
neoserver,ios ssh client

Dynamically Build an Update Expression · GitHub

Instantly share code, notes, and snippets.

DynamoDB - Dynamically Build an Update Expression

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, "")

Author

daverickdunn commented on Dec 16, 2020

edited

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

edited

it works. btw, does it support nested objects?

Author

daverickdunn commented on May 3, 2021

edited

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!

syerwin commented on Jul 30

Thanks @daverickdunn .. it saves me a lot


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK