Suppose I'm storing records with the following structure in DynamoDB:
{
"id": "574edeac445084d8d5aacc99b941d9d6",
"status": "NEW",
"blocked": false
}
Is it possible to achieve the following in one request?
If the record with the given id is blocked update status to BLOCKED or else to APPROVED
Unfortunately, no. In a single request, you can include a ConditionExpression that will either:
For your implementation it seems like you would need support for some kind of if ... else
UpdateExpression function, which according to the list of comparison and function references for update expressions, is not available.
If this is not an extremely frequent operation, multiple requests would likely be fine. I can think of two options there:
For example: if you think that one condition expression will evaluate true 3/4's of the time, then you're only making two update requests 25% of the time. If you use the GET method above you're making a GET and an UPDATE 100% of the time. If there's a 50/50 chance that the first "blind" UPDATE attempt will fail, it might be easier to just use option number 1.
If this is an extremely popular operation, you may want to rethink your table design. You might be able to leverage sparse indexes to accomplish your goal.