Reputation: 373
The Dynamo DB document API allows put operation using a json payload
Item item = Item.fromJSON(payload);
table.putItem(item);
However I couldn't find a similar way for performing an updateItem with a Json payload.
Is there a Dynamo DB support for that?
Upvotes: 4
Views: 3011
Reputation: 515
Here is a snippet as to how you would do it in Javascript.
let updateExpression = 'SET ';
let expressionAttributeValues = {};
const keys = Object.keys(payload);
for (const key of keys) {
updateExpression += `${key}=:${key},`;
expressionAttributeValues[`:${key}`] = payload[key];
}
updateExpression=updateExpression.slice(0,-1);
let params = {
TableName : 'your_table_name',
Key: {
id: id
},
UpdateExpression : updateExpression,
ExpressionAttributeValues : expressionAttributeValues,
ReturnValues: "ALL_NEW"
}
db.update(params,(err, result)=>{});
Upvotes: 5
Reputation: 56
I was struggling with this for a while in the end you have to use a map.
UpdateItemOutcome updateItemOutcome = table.updateItem(
new UpdateItemSpec()
.withPrimaryKey("id", "yourId")
.withUpdateExpression("SET document.field = :field")
.withValueMap(new ValueMap()
.withMap(":field", "map of key value pairs that will get serialized to json")));
Upvotes: 1
Reputation: 1373
The UpdateItemSpec currently doesn't accept Item as input -- you will have to parse the json content first (presumably via Item.fromJSON) and then use either AttributeUpdate or UpdateExpression to specify the update action on each individual field (SET, ADD etc.)
Here is the documentation to using UpdateExpression to update an item http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaDocumentAPIItemCRUD.html#JavaDocumentAPIItemUpdate
Upvotes: 0