Support Topics Documentation Slack YouTube Blog

Perform Interim Operations in Transaction

This question is in reference to this topic:

Is support for this still planned? If so, is there an anticipated release date?

Thanks!

Hi John,

There were some improvements, though they were not as encompassing due to security and query injection concerns. Could you please describe your scenario, perhaps there would be a solution already?

Regards,
Mark

Hey Mark,

Here is my use-case.

Given the following Data tables:

  1. Topic (has options that can be “voted” on - 1:n relation to TopicOption)
  2. TopicOption (An option for a Topic that can be voted on)
  3. VoteRecord (record of a user’s vote for a Topic & TopicOption)
  4. UserSettings (has a “voteRecord” column that is a 1:n relationship to VoteRecord entries)

The scenario I have in mind is to handle voting on Topics with a server transaction. User’s can vote on a Topic by selecting an option. User’s can change a vote for a Topic but not “remove” it.

Request Body

{
	"optionId": <option_id>,
	"topicId": <topic_object_id>,
	"userId": <user_object_id>,
}

Check for existing VoteRecord entries where userId = ‘userId’ and topicId = ‘topicId’

If an entry exists:
Update VoteRecord optionId with the new value
Update (decrement) the previously voted option’s “votes” count
Update (increment) the new option’s “votes” count

else:
Create a new VoteRecord
Add new VoteRecord object to user’s “voteRecord” relation.
Update (increment) TopicOption “votes” count
Update (increment) Topic “totalVotes” count

Let me know if this needs clarification/simplification. Thanks again.

Where does ‘count’ field reside?

If the ‘vote’ is a record in ‘VoteRecord’ which in its turn related to the user: you can just add or remove record to that table. And the count request to that table will give you desired number of votes.

@oleg-vyalyh both ‘Topic’ and ‘TopicOption’ objects have a column labeled ‘votes’ - that is the count I am referring to.

Correct, a vote is an object in the VoteRecord table. The reason I want to update the ‘count’ columns as mentioned above, is to have the number of votes for a Topic (and it’s individual options) available without additional requests. Hence why I am trying to update those values within a transaction.

Now I understand.
One more question. One user can vote more than once for specific pair of Topic & TopicOption ?

A user can change their vote for a Topic. So if a Topic has 3 options and the user votes for optionA, then later wants to change that vote, they can. The way I mapped it out in my head was to update the Vote record that existed for that user/Topic/TopicOption, rather than remove it and create another.

Voting for a Topic/TopicOption the user has already voted for, is handled client side.

I’m talking not about the changing the target (either topic or option) of the vote.

My question was:
Сan the user vote more than once for specific pair of Topic & TopicOption ?
If no – I would advise to try approach which I described above.
Your VoteRecord is already uniquely point to the combination of user,topic,topicoption. So, you just have to remove one record and create another one.
In such a case you receive the ability to get flexible statistics with different combinations of user,topic,topicoption.

Сan the user vote more than once for specific pair of Topic & TopicOption ?

No, they cannot.

Your VoteRecord is already uniquely point to the combination of user,topic,topicoption. So, you just have to remove one record and create another one.
In such a case you receive the ability to get flexible statistics with different combinations of user,topic,topicoption.

Correct, we’re already doing that. The Topic and TopicOption tables both have a “votes” column. These “counter” values keep track of the number of votes. For a TopicOption, this is the number of VoteRecords with the particular TopicOption objectId. For a Topic, it’s all VoteRecords with that particular Topic objectId. This way a Topic’s vote count is retrievable within the Topic object. Same for a TopicOption.

These “votes” columns are they values I want to update within the transaction - when the user casts their vote. If the client is displaying a list of Topics, I need to be able to show the number of votes as well, without additional requests.

Basically I am trying to use the votes column in Topic/TopicOption as an atomic counter. I had hoped to be able to update those values using transactions. Could this be done by passing the current values into the request body (using the scenario from above)? I feel that could also allow clients to be using incorrect data, though.

Hi @John_Admin !

Unfortunatelly currently there is no other way to do it completelly on server side in transaction. Sorry for inconvenience.
We already have internal ticket BKNDLSS-20855 for feature which would feet your need but it will take several releases to add it to server and SDKs.

Regards, Andriy

@Andriy_Konoz I see, thanks for the update!

I will have keep an eye on future releases for this, as well as BKNDLSS-24678, which is preventing RT connections on Flutter. These are the only things preventing a migration from existing services.