0

I use the following piece of code to create some records. If I provide incorrect values, say(password and passwordConfirmation does not match), then sometimes an institute record is created without a rollback and sometimes, rollback happens properly.

I would appreciate any help. Is there a better way to do this?

create: function (req, res) {
    User.query("BEGIN TRANSACTION", function(result){
        if(result) {
            sails.log.info(result);
            return res.serverError(result);
        } else {
            Institute.create({
                name: req.param('name'),
                shortName: req.param('shortName'),
                phoneNumber: req.param('phoneNumber'),
                subdomain: req.param('subdomain'),
                managerEmail: req.param('email')
            }, function(error, institute){
                if(error) {
                    sails.log.info(error);
                    Institute.query("ROLLBACK", function(result) {
                        sails.log.info(result);
                        return res.badRequest(error);
                    });
                } else {
                    User.create({
                        email: req.param('email'),
                        password: req.param('password'),
                        passwordConfirmation: req.param('passwordConfirmation'),
                        account: institute.id
                    }, function(error, user) {
                        if(error) {
                            sails.log.info(error);
                            Institute.query("ROLLBACK", function(result) {
                                sails.log.info(result);
                                return res.badRequest(error);
                            });
                        } else {
                            User.query("COMMIT", function(result){
                                sails.log.info(result);
                                return res.created(user);
                            });
                        }
                    });
                }
            });
        }
    });
}
Derick
  • 191
  • 4

1 Answers1

1

You have a few of options, in no particular order.

1. Write a function that makes all the possible security checks before creation occurs, or use the beforeCreate life cycle call for your models.

For example, you could write a function verifyParams(params) that makes checks such as password comparison (and any other checks you want) for your user creation parameters before you create the institution, or you could just include these checks in your institution creation's beforeCreate method.

2. Delete if there is an error during your user creation

Delete theInstitute model instance in your error case of user creation:

...
User.create(..., function (error, user) {
  if (error) { 
    Institute.destroy(institute.id, function instDestroyed(err) { 
      ... 
    });
  } else {
    ...
  }
});

3. Create a user in your institute model's beforeCreate method.

module.exports = {
  attributes: { ... },
  beforeCreate: function(values, next) {
    User.create(..., function (err, user) { 
      if (err) { return next(err) }
      return next();
    });
  }
}     

Personally, I use method #2 in my own apps.

kk415kk
  • 1,227
  • 1
  • 14
  • 30
  • Thanks for the options. I had given a thought about that. But I thought that it would be apt to cancel that transaction when there is an error, rather than deleting the created model. My problem is that the code responsible for rolling back the transaction is not called at times. Most of the time, it works. – Derick Sep 10 '14 at 14:57
  • Hmm, are you sure that the code recognizes that there is a validation error? It's strange that it works sometimes but not all the times - I have a feeling that in the cases it doesn't rollback, it's some case of validation that the code isn't covering. – kk415kk Sep 10 '14 at 16:02
  • There's a known issue with sails that custom validations defined in types are not called. So I had to move all the validations to beforeCreate. – Derick Sep 11 '14 at 03:07