David Przybilla
David Przybilla

Reputation: 828

Appending Item to DynamoDB list atomically

I have a field which is a list of Dictionaries.

{
 "messages": [item1, item2....]
}

Multiple hosts will be appending items to that list.

Is there any consideration to take into account to ensure that atomic appends?

Upvotes: 2

Views: 948

Answers (1)

idbehold
idbehold

Reputation: 17168

Use list_append() and if_not_exists() together to append to a potentially non-existent list column:

var params = {
  TableName: "yourTableName",
  Key: { hash: "someId" },
  UpdateExpression: "set #messages = list_append(if_not_exists(#messages, :empty_list), :message)",
  ExpressionAttributeNames: {
    "#messages": "messages"
  },
  ExpressionAttributeValues: {
    ":message": [{ "id": "1","foo": "bar" }],
    ":empty_list": []
  }
};

Upvotes: 4

Related Questions