Search code examples
node.jstransactionssails.jswaterline

How to make transactions with sails.js/waterline?


I am trying to put my queries into transaction and I am failing in runtime. Error I am getting is :

Object #<bound> has no method 'transaction'

I tried to follow this "documentation".

In short my model looks like that :

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;
}

any ideas what did I do wrong?

thanks.

w.


Solution

  • The "documentation" you're following is a proposal for how transaction support could be added to Sails. There is no native transaction support in Sails. See this answer for an example of how to use the .query method for the MySQL or Postgres adapters to perform transactions.