【问题标题】:Rollback Transaction for Multiple Models in Waterline ODM in Sails (For Mongo DB )Sails 中 Waterline ODM 中多个模型的回滚事务(对于 Mongo DB )
【发布时间】:2015-12-14 11:36:44
【问题描述】:

我在 Mongo 数据库中使用 Sails 和 Waterline ORM。

我有两个模型 User 和 Profile 具有一对一的关系。
下面是我为带有回滚逻辑的事务编写的代码。我认为可能有比这更好的逻辑,因为当前的逻辑非常笨拙。

问题:

  1. Waterline 或 Sails 是否提供任何用于回滚的功能?

有没有更好的方法?

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

【问题讨论】:

    标签: mongodb transactions sails.js waterline sails-mongo


    【解决方案1】:

    对问题 1:没有。

    我很想这样做:

    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!
        }
    })
    

    我不知道它是否更好,但它似乎更具可读性,并且它可以处理三个写入阶段中的任何一个的错误。

    【讨论】:

      猜你喜欢
      • 2014-07-18
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-11
      • 2016-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多