Transactional SQL with Sails.js

心不动则不痛 提交于 2019-12-03 03:23:09

You can still write SQL queries directly using Model.query(). Since this is an asynchronous function, you'll have to use promises or async to reserialize it. For instance, using the MySQL adapter, async, and a model called User:

async.auto({
  transaction: function(next){
    User.query('BEGIN', next);
  },
  user: ['transaction', function(next) {
    User.findOne(req.param('id')).exec(next);
  }],
  // other queries in the transaction
  // ...
}, function(err, results) {
  if (err) {
    User.query('ROLLBACK', next);
    return next(err);
  }
  User.query('COMMIT', next);
  // final tasks
  res.json(results.serialize);
});

We're working on native support for transactions at the ORM level: https://github.com/balderdashy/waterline/issues/62

Associations will likely come first, but transactions are next. We just finished GROUP BY and aggregations (SUM, AVG, etc.)

Transactions in SailsJS turned out to be much trickier than anticipated. The goal is to let the ORM adapter know that two very disparate controller actions on models are to be sent through a single MySQL connection.

The natural way to do it is two write a new adapter that accepts an additional info to indicate that a query belongs to a transaction call. Doing that requires a change in waterline (sails ORM abstraction module) itself.

Checkout if this helps - https://www.npmjs.com/package/sails-mysql-transactions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!