Transactional SQL with Sails.js

后端 未结 3 1549
离开以前
离开以前 2021-02-05 17:36

So I have been playing with NodeJS/Express for a little with now and I would really like to try to rewrite a relatively large side project using a full JavaScript stack just to

相关标签:
3条回答
  • 2021-02-05 18:06

    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.)

    0 讨论(0)
  • 2021-02-05 18:12

    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

    0 讨论(0)
  • 2021-02-05 18:25

    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);
    });
    
    0 讨论(0)
提交回复
热议问题