Question:
Is there any way to update only a single field on a row in a data collection that is not getting the whole row first, changing a single value and updating every field? An equivalent of the SQL command UPDATE with SET setting only a single value?
Product:
Wix Editor
What are you trying to achieve:
To create an example, lets say we have a table defined as follows
Table: MatchUp
ID: unique identifier auto added on insert
Member1: reference to member id
Member2: reference to member id
M1Received: boolean, default to false
M2Recieved: boolean, default to false
In an SQL environment, if I wanted to set that Member 1 had received the match up, I would call “UPDATE MatchUp SET M1Recieved = true WHERE ID = ”. I’ve come to quickly realize that attempting to stick close to this in wix code…
var toUpdate = { _id: <matchupid>, m1Recieved = true };
wixData.update("MatchUp", toUpdate);
… is going to blank out every single value that isn’t the ID and M1Recieved. The solution I’ve seen offered…
wixData.get("MatchUp", <matchupid>)
.then((match) => {
match.m1Recieved = true;
wixData.update("MatchUp", match);
});
… runs into issue if something common like Member 1 and 2 updating their status at the same time, because the collections seem to lack any form of transactional locking meaning that you can end up with both getting a row where both values are false, both updating their cell to true, and in the end only one having the updated value. For example…
table row at the start is (id, m1Id, m2Id, false, false)
M1 gets row (id, m1Id, m2Id, false, false)
M2 gets row (id, m1Id, m2Id, false, false)
M1 updates row (id, m1Id, m2Id, true, false)
M2 updates row (id, m1Id, m2Id, false, true)
table row at the end is (id, m1Id, m2Id, false, true)
table row was expected to be (id, m1Id, m2Id, true, true)
I’m looking for either a way to transaction-lock a collection or a way to just update the one field without having to update every field on the row.
What have you already tried:
Basically every “solution” is to get the row first to update ALL the values on the row, but that’s not really solving the issue of trying to set a single field in an environment without transactional locking.