Parthiva
Parthiva

Reputation: 290

DynamoDB UPDATE if existing else PUT - Boolean

I need to have a table with the structure

{
userId : 123,
Tracking: true
}

It is possible that the user does not exist for the first operation. So, by default a false should be set. The next request makes this value true, 3rd request makes it false again and so on. Similar to NOT(Tracking) i.e writing a negation to the value.

I could do this by reading the table, negating the value in the my lambda function and updating the table with new attributes.

This would mean a GET and UPDATE request for the DB. I am looking for a way to send a negation flag instead. In this way I just write a false if the user is not existing. If the user is existing, I would toggle between true and false depending on the already existing boolean value.

Just wondering if there is a way to do this. It would be then a single update request to the DB. Any pointers would be helpful.

I did not find much help from the documentation https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#update-property

Upvotes: 0

Views: 1285

Answers (1)

cementblocks
cementblocks

Reputation: 4596

Instead of a boolean you can use an integer. Increment it by 1 each time and then use even as true, odd as false (or reversed)

Upvotes: 2

Related Questions