0

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: "[email protected]"
   location: "sales"
} 

and then update the item to be

{
   email: "[email protected]"
   location: "sales"
   sortKey: "[email protected]#sales"
}

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

1

1 Answer 1

1

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 = "[email protected]"
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

Sign up to request clarification or add additional context in comments.

2 Comments

I shouldn't have used sortKey, it's a global secondary index that is blank right now
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.