DynamoDB Update Items with existing information NodeJS

Viewed 24

Is there a way to achieve the following with UpdateItem in dynamo?

Take two attributes from an item and populate a 3rd with a combination of those 2 items?

For example:

{
   email: "myemail@gmail.com"
   location: "sales"
} 

and then update the item to be

{
   email: "myemail@gmail.com"
   location: "sales"
   sortKey: "myemail@gmail.com#sales"
}

I'm using NodeJS and the latest version of the SDK

1 Answers

The short answer is you cannot get the service side to concatenate the values for you. You must do it from the client side, which in your case will mean you need to read the item first, then update it.

However it seems like you are wanting to make your tables sortkey a concatenation of the other two values. In this case, the item would not already exist in the table, as you are not able to change the sortkey of an item. So lets look at this as if you were adding data for the first time.

const AWS = require('aws-sdk')
const ddb = new AWS.DynamoDB.DocumentClient({ region: 'eu-west-1' });


const email = "myemail@gmail.com"
const location = "sales"

const params = {
    TableName: 'testTable',
    Item: {
        email: email,
        sortKey: `${email}#${location}`,
        location: location,

       }
    }

ddb.put(params)
    .promise()
    .then(res => console.log(res))
    .catch(err => {
        console.log("ERROR")
        console.log(err.code, err.message);
    })

Results: enter image description here

Related