In this article I will share a reusable TypeScript function to update multiple fields of DynamoDB table.
If you’re not using a library or function for this purpose, constructing a DynamoDB query every time can really make your code messy.
Before you read further, AWS already has these two libraries that simplify a lot of the DynamoDB operations if you don’t want to work with the low level SDK methods.
I have this table users
which has unique partition key of user_id
. Every time the user record is updated, I want to update the updated_at
timestamp as well.
Below is the the reusable function that does that.
Disclaimer: I only share minimally working code snippets. My goal is to inspire you to learn and elaborate from what I have.
To use the function, you simply pass in the user_id
and fields
as shown below.
await updateUserFields(userId, {
field1: field1Value,
field2: field2Value,
field3: field3Value,
});
Happy coding!