Last active
October 8, 2020 08:45
-
-
Save ibrahim-dogan/61d83c96f0b8ab361fc16620799c50ca to your computer and use it in GitHub Desktop.
You can update any json object with this code below.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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