Reputation: 2754
I currently have a List attribute in my dynamodb table:
name: "Test User"
recommendations: []
I would like to add new item to the recommendations attribute using the UpdateExpression
const params = {
TableName: 'insiders',
Key:{
"uuid": event.uuid, // WHERE uuid is event.uuid
},
UpdateExpression: "SET #recommendations = :recommendation",
ExpressionAttributeNames: {
"#recommendations": "recommendations",
},
ExpressionAttributeValues: {
":recommendation": [{
"uuid": `ir_${uuidv4()}`,
"recommendation": event.recommendation
}]
},
ReturnValues:"UPDATE_NEW"
};
dynamodb.update(params, function(err, data) { }
I managed to add an object map to recommendations list but when I want to add another one it will replace the object in the recommendation list.
I also tried to use the ADD in UpdateExpression
const params = {
TableName: 'insiders',
Key:{
"uuid": event.uuid,
},
UpdateExpression: "ADD #recommendations :recommendation",
ExpressionAttributeNames: {
"#recommendations": "recommendations",
},
ExpressionAttributeValues: {
":recommendation": [{
"uuid": `ir_${uuidv4()}`,
"recommendation": event.recommendation,
}]
},
ReturnValues:"ALL_NEW"
};
dynamodb.update(params, function(err, data) { }
but Im getting an error
"Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: LIST",
Upvotes: 1
Views: 3403
Reputation: 2754
Okay I already figured out how to add an object to existing map attribute of an item. I used list_append
this will add an object to existing map attribute
const params = {
TableName: 'insiders',
Key:{
"uuid": event.uuid,
},
UpdateExpression: "SET #attrName = list_append(#attrName, :attrValue)",
ExpressionAttributeNames: {
"#attrName": "recommendations",
},
ExpressionAttributeValues: {
":attrValue": [{
"uuid": `ir_${uuidv4()}`,
"recommendation": event.recommendation
}]
},
ReturnValues:"ALL_NEW"
};
Upvotes: 1