Reputation: 342
The case:
I'm creating an api with SAILS.js, sails.js uses waterline for ORM. The api returns lets say photographs, many users are able to vote for a picture. The pictures will be ordered on number of votes.
Procedure: When a user votes for a song, I've to check the number of votes ("SELECT"|| picture.findById()) AND after that I have to increment that number by one ("UPDATE" picture.update).
Problem: transaction/ locking in Sails.js, these two queries should be excecuted without having a other query modifying the picture data within the select and update query of our vote system.
How should we perform locking/ transition in sails.js (node js framework)
THANKS
Upvotes: 5
Views: 856
Reputation: 376
Sails does support transactions.
Here is an example of a transaction in sails.js:
await sails.getDatastore().transaction(async db=> {
await Model
.create({foo: bar})
.usingConnection(db);
if (somethingWentWrong) {
throw 'error happened - transaction rollback';
}
await Model
.update({id: 1}))
.set({votes: 1})
.usingConnection(db);
}).intercept('Error', () => res.serverError());
Upvotes: 0