Transactional SQL with Sails.js

后端 未结 3 1550
离开以前
离开以前 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: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);
    });
    

提交回复
热议问题