如何与sails.js / waterline进行交易?

时间:2015-01-15 11:54:20

标签: node.js transactions sails.js waterline

我正在尝试将我的查询放入事务中,而我在运行时失败了。我得到的错误是:

Object #<bound> has no method 'transaction'

我尝试关注this&#34;文档&#34;。

简而言之,我的模型看起来像这样:

updateOrCreate: function (profile_id, positive,negative) {
  var deferred = Q.defer();

  Reputation.transaction().findOne().where({profile: profile_id}).then(function (rep) {
    if (rep) {
      // Reputation logic

      rep.save(function (err) {deferred.resolve();});
    } else {
      // Reputation does not exist. Create.
      Reputation.create({profile: profile_id, positive: positive,negative:negative}).exec(function (e, rep) {
        deferred.resolve();});
    }
  }).fail(function (err) {deferred.reject()});

  return deferred.promise;
}

任何想法我做错了什么?

感谢。

瓦特

3 个答案:

答案 0 :(得分:9)

帆船v1现已支持此功能(2017年6月26日尚未正式发布)。 您可以点击此链接获取next.sailsjs.com中的文档: Datastore.transaction()

上面的doc是以下示例:

sails.getDatastore()
.transaction(function (db, proceed) {

  BankAccount.findOne({ owner: req.session.userId }).usingConnection(db)
  .exec(function (err, myAccount) {
    if (err) { return proceed(err); }
    if (!myAccount) { return proceed(new Error('Consistency violation: Database is corrupted-- logged in user record has gone missing')); }

    BankAccount.findOne({ owner: req.param('recipientId') }).usingConnection(db)
    .exec(function (err, recipientAccount) {
      if (err) { return proceed(err); }
      if (!recipientAccount) {
        err = new Error('There is no recipient with that id');
        err.code = 'E_NO_SUCH_RECIPIENT';
        return proceed(err);
      }

      // Do the math to subtract from the logged-in user's account balance,
      // and add to the recipient's bank account balance.
      var myNewBalance = myAccount.balance - req.param('amount');

      // If this would put the logged-in user's account balance below zero,
      // then abort.  (The transaction will be rolled back automatically.)
      if (myNewBalance < 0) {
        err = new Error('Insufficient funds');
        err.code = 'E_INSUFFICIENT_FUNDS';
        return proceed(err);
      }

      // Update the current user's bank account
      BankAccount.update({ owner: req.session.userId })
      .set({
        balance: myNewBalance
      })
      .usingConnection(db)
      .exec(function (err) {
        if (err) { return proceed(err); }

        // Update the recipient's bank account
        BankAccount.update({ owner: req.param('recipientId') })
        .set({
          balance: recipientAccount.balance + req.param('amount')
        })
        .usingConnection(db)
        .exec(function (err) {
          if (err) { return proceed(err); }
          return proceed();
        });
      });
    });
  });

}).exec(function(err){
  // At this point, we know that, if our code above passed through
  // an error to `proceed`, Sails took care of rolling back the
  // transaction.  Otherwise, it committed it to the database.
  if (err && err.code === 'E_INSUFFICIENT_FUNDS') {
    return res.badRequest(err);
  }
  else if (err && err.code === 'E_NO_SUCH_RECIPIENT') {
    return res.notFound();
  }
  else if (err) {
    return res.serverError(err);
  }

  // All done!
  return res.ok();

});

答案 1 :(得分:4)

&#34;文档&#34;您关注的是如何将事务支持添加到Sails的提案。 Sails中没有本机事务支持。有关如何使用.query方法为MySQL或Postgres适配器执行事务的示例,请参阅this answer

答案 2 :(得分:0)