Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do atomic update using Bookshelf.js model?

For example, if I wanted to do something like the following in MySQL query:

update foo set bar = bar + ?;

How would I achieve this using Bookshelf.js model?

like image 949
Student01 Avatar asked Sep 02 '26 23:09

Student01


1 Answers

I'm not exactly sure what you mean by atomic, perhaps transactions? You can do that in bookshelf, there's a whole section about it: http://bookshelfjs.org/#Bookshelf-transaction

However, if you're just trying to update a field, you can do that easily as follows:

var idStationIWantToUpdate = 1;
new Station({'id': idStationIWantToUpdate}).save({
    route_name: 'my updated route name'
}).then(function(station) {
    res.json(station.toJSON());
});

The important part is that you instantiate a model with the corresponding ID, then when you call save, it automatically updates the existing record.

like image 125
uglycode Avatar answered Sep 05 '26 15:09

uglycode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!