2

I have items in a DynamoDB database table and I want to update an existing item but only a few attributes of it instead of everything. I know I can do that with for example AWS.DynamoDB.DocumentClient, using the update() function and passing UpdateExpression with the attributes I want to update in the params. I also know that I can loop through the properties of the javascript object and construct an update expression based on that but I suspect there must be an easier way to do this.

So instead of something like this (and having to implement the two fetch* functions):

const ddbDocClient = new AWS.DynamoDB.DocumentClient();
const params = {
  TableName: 'SomeTable',
  Key: { id: someId },
  UpdateExpression: fetchPropertiesOfAJavascriptObjectAndMakeAnExpressionStringOfThem(obj),
  ExpressionAttributeValues: fetchValuesOfAJavascriptObjectAndMakeAValuesStringOfThem(obj)
}
ddbDocClient.update(params, (err, data) => {/* do something */});

I would like to do something like

const ddbDocClient = new AWS.DynamoDB.DocumentClient();
const params = {
  TableName: 'SomeTable',
  Key: { id: someId },
  AttributesToUpdate: obj
}
ddbDocClient.update(params, (err, data) => {/* do something */});

1 Answer 1

1

You cannot do so directly using the low level API, as you need to set the UpdateExpression.

You would need to use an object mapper type client which is not directly available via the SDK but can be obtained here:

https://github.com/awslabs/dynamodb-data-mapper-js

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

Comments

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.