1

I am using Sails and Waterline ORM with Mongo Database .

I have two models User and Profile with One to One relationship.
Below is the code I've written for Transaction with Rollback logic. I think there can be a much better logic than this as the current logic is very clumsy.

Questions :

  1. Does Waterline or Sails provide any functionality for Rollback purpose?

Is there any better way of doing this ?

User.create(newUser).then(function (data) {
    var newProfile = {
        displayName: data.name,
        email: data.email,
        user: data._id
    }
    return Profile.create(newProfile);
  }).then(function (profileData) {
    sails.log.info("Profile Data " + JSON.stringify(profileData));

    // Update the user with Profile Info
    User.update(newUser._id, {profile: profileData._id}).then(function (updatedUser) {
      return updatedUser;
    }, function (err) {
      // TODO Rollback logic if the User Updation Fails
    })
  }, function (err) {
    sails.log.error("Failed to Create Profile for the User . Deleting the created User");
    var criteria = {
      email: data.email
    }
    User.destroy(criteria).then(function (user) {
      sails.log.error("Deleted the Created User " + JSON.stringify(user));
      throw new Error("ERROR CREATING User");
    }, function (err) {
      sails.log.error("ERROR DELETING USER");
      throw new Error("ERROR DELETING USER", err);
    })
  });
Darin Kolev
  • 3,401
  • 13
  • 31
  • 46
Shiva MSK
  • 484
  • 1
  • 6
  • 16

1 Answers1

1

To question 1 : no.

I would be tempted to do something like this:

async.waterfall([
    function(callback){
        User.create(newUser).then(function(data){       
            callback(null, data);
        })
        .catch(function(err){
            callback(err, null, null); // don't need to rollback anything
        })
    },
    function(data, callback){
        Profile.create({
            displayName: data.name,
            email: data.email,
            user: data._id
        })
        .then(function(profile){
            callback(null, data, profile)
        })
        .create(function(err){
            callback(err, data._id, null); // only need user's id
        })
    },
    function(userData, profileData, callback){
        User.update(userData._id, {profile: profileData._id})
            .then(function(updatedUser){
                callback(null, updatedUser);
            })
            .catch(function(err){
                callback(err, userData._id, profileData._id); // can roll back both
            })
    }
], function(err, userData, profileData){
    if(err) {
        sails.log.error(err);
        // do rollback, userData is user's ID, profileData is profile id
        // if either one is undefined, then it doesn't exist
    } else {
        // userData is user object from the last update, return it!
    }
})

I don't know if it is better, but it seems more readable, and it handles the errors for any of the three writing phases.