Skip to content

Instantly share code, notes, and snippets.

@ibrahim-dogan
Last active October 8, 2020 08:45
Show Gist options
  • Save ibrahim-dogan/61d83c96f0b8ab361fc16620799c50ca to your computer and use it in GitHub Desktop.
Save ibrahim-dogan/61d83c96f0b8ab361fc16620799c50ca to your computer and use it in GitHub Desktop.
You can update any json object with this code below.
const object = {
"name": "blabla",
"surname": "blabla",
"age": 18,
"foo": "bar",
"date": new Date().toString()
};
var updatedKeys = Object.keys(object);
/**
* it returns the update expression for dynamodb.
* example:
* "set name = :name, surname = :surname, age = :age, foo = :foo, date = :date"
* @type {string}
*/
var updateExpression = "set " + updatedKeys.map(x => `${x} = :${x}`).join(", ");
var expressionAttributeValues = {};
/**
* it fills :key with the value in dynamodb format.
* example:
* expressionAttributeValues = {
":name": "blabla",
":surname": "blabla",
":age": 18,
":foo": "bar",
":date": new Date().toString()
* }
*/
updatedKeys.forEach((key) => expressionAttributeValues[`:${key}`] = object[key])
let params = {
TableName: TABLE_NAME,
Key: {
"pk": `your_pk`,
"sk": `your_sort_key_if_exists`
},
UpdateExpression: updateExpression,
ExpressionAttributeValues: expressionAttributeValues
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment