0

I have a value I am trying to increment by 1 in a DynamoDB row. The first time this is executed, the parameter does not exist, so it would be set to one. However, I want it to fail if it gets up to a specific value so it can only be updated so many times.

I am using the correct Expected syntax, but I can't seem to figure out a way to allow this currently without a double update: One to update if it's not there, and one do it if it is there and below the threshold. Is this possible in a single update command?

Please note, this is not the same thing as simply wanting a value to be set to 1 or add up from there as suggested in the comments below, but instead, this single expression must handle the property being there or a number, and then continue to add up a certain threshold, and then fail and not allow an update once the threshold is met.

David
  • 1,648
  • 1
  • 16
  • 31
  • I think this question is about the same scenario: https://stackoverflow.com/questions/32777374/is-it-possible-to-do-a-conditional-put-or-update-in-dynamodb – OARP Dec 10 '21 at 04:27
  • Does this answer your question? [Is it possible to do a conditional put or update in DynamoDB?](https://stackoverflow.com/questions/32777374/is-it-possible-to-do-a-conditional-put-or-update-in-dynamodb) – Ermiya Eskandary Dec 10 '21 at 05:36
  • @ErmiyaEskandary - No this does not answer my question and is a different situation. This is not failing to write at a certain threshold like I want, but simply setting it to 1 or adding on from there. – David Dec 10 '21 at 06:01
  • @OmarRosadio - No this does not answer my question and is a different situation. This is not failing to write at a certain threshold like I want, but simply setting it to 1 or adding on from there. In my situation, it would always succeed in writing (if the property is there or not), but only fail when it adds up to a certain value. – David Dec 10 '21 at 06:02
  • What did you try that didn't work? It sound's the "obvious" combination of ConditionExpression and UpdateExpression should work: The ConditionExpression will ask for attribute_not_exists(v) or v < :ten (wirth :ten being set being the maximum you wanted), and the UpdateExpression will "add v :one" (or you can also use set and if_not_exists). Since I don't have time to try this myself right now, I'm not going to enter this as an answer, but if it works, please do write the answer yourself :-) – Nadav Har'El Dec 10 '21 at 10:54

1 Answers1

1

Ok, I believe that this is possible, but only if you switch the whole update command over to using expressions instead of using the "Expected" and "AttributeUpdates" field. This does make it much harder to use, but then you should be able to make compound conditions such as

params.ConditionExpression = 'attribute_not_exists(#A) OR #A < ' + maxValue.toString();
David
  • 1,648
  • 1
  • 16
  • 31