如何修复 bookshelfjs 事务中的嵌套结构

How to fix nested structure in bookshelfjs transaction

我想在单个书架事务中更新多个数据库表。我需要一些帮助来重构我的代码。我是 node 的新手,对 promises 不是很了解,但是下面的嵌套结构不是很漂亮,我希望有一种更简洁的方法。任何帮助将不胜感激。

function insertUser(user, cb) {
  bookshelf.transaction(function(t) {
  var key = user.key;
  Developer.forge({key: key})
  .fetch({require: true, transacting: t})
  .then(function(developerModel) {
    var devID = developerModel.get('id');
    Address.forge(user.address)
    .save(null, {transacting: t})
    .then(function(addressModel) {
      var addressID = addressModel.get('addressId');
      Financial.forge(user.financial)
      .save(null, {transacting: t})
      .then(function(financialModel) {
        var financialID = financialModel.get('financialId');
        var userEntity = user.personal;
        userEntity.addressId = addressID;
        userEntity.developerId = devID;
        userEntity.financialId = financialId;
        User.forge(userEntity)
        .save(null, {transacting: t})
        .then(function(userModel) {
          logger.info('saved user: ', userModel);
          logger.info('commiting transaction');
          t.commit(userModel);
        })
        .catch(function(err) {
          logger.error('Error saving user: ', err);
          t.rollback(err);
        });
      })
      .catch(function(err) {
        logger.error('Error saving financial data: ', err);
        t.rollback(err);
      })
    })
    .catch(function(err) {
      logger.error('Error saving address: ', err);
      t.rollback(err);
    })
  })
  .catch(function(err) {
    logger.error('Error saving business : ', err);
    t.rollback(err);
  })
})
.then(function(model) {
  logger.info(model, ' successfully saved');
  return Promise.resolve(respond.success({userId: model.get('userId')}));
})
.catch(function(err) {
  logger.error(err, ' occurred');
  return Promise.reject(new DatabaseError('Unable to write user to database due to error ', err.message));
})};

将结果的承诺保存到变量中,您可以稍后在决赛中使用它们,然后保证它们得到满足,因此可以使用 .value() 同步检索它们的值

function insertUser(user, cb) {
  return bookshelf.transaction(function(t) {
    var key = user.key;

    var devID = Developer.forge({key: key})
      .fetch({require: true, transacting: t})
      .call("get", "id");

    var addressID = devID.then(function() {
      return Address.forge(user.address).fetch({require: true, transacting: t})
    }).call("get", "addressId");

    var financialID = addressModel.then(function() {
      return Financial.forge(user.financial).save(null, {transacting: t})
    }).call("get", "financialId");

    var userModel = financialID.then(function() {
      var userEntity = user.personal;
      userEntity.addressId = addressID.value();
      userEntity.developerId = devID.value();
      userEntity.financialId = financialID.value();
      return User.forge(userEntity).save(null, {transacting: t});
    });

    return userModel.then(function(userModel) {
      logger.info('saved user: ', userModel);
      logger.info('commiting transaction');
      t.commit(userModel);
    }).catch(function(e) {
      t.rollback(e);
      throw e;
    });
  });
}
.then(function(model) {
  logger.info(model, ' successfully saved');
  return Promise.resolve(respond.success({userId: model.get('userId')}));
})
.catch(function(err) {
  logger.error(err, ' occurred');
  return Promise.reject(new DatabaseError('Unable to write user to database due to error ', err.message));
})};

另一种方法是使用 Promise.join:

function insertUser(user, cb) {
  return bookshelf.transaction(function(t) {
    var key = user.key;

    var devID = Developer.forge({key: key})
      .fetch({require: true, transacting: t})
      .call("get", "id");

    var addressID = devID.then(function() {
      return Address.forge(user.address).fetch({require: true, transacting: t})
    }).call("get", "addressId");

    var financialID = addressModel.then(function() {
      return Financial.forge(user.financial).save(null, {transacting: t})
    }).call("get", "financialId");

    var userModel = Promise.join(devID, addressID, financialID,
     function(devID, addressID, financialID) {
      var userEntity = user.personal;
      userEntity.addressId = addressID;
      userEntity.developerId = devID;
      userEntity.financialId = financialID;
      return User.forge(userEntity).save(null, {transacting: t});
    });

    return userModel.then(function(userModel) {
      logger.info('saved user: ', userModel);
      logger.info('commiting transaction');
      t.commit(userModel);
    }).catch(function(e) {
      t.rollback(e);
      throw e;
    });
  });
}
.then(function(model) {
  logger.info(model, ' successfully saved');
  return Promise.resolve(respond.success({userId: model.get('userId')}));
})
.catch(function(err) {
  logger.error(err, ' occurred');
  return Promise.reject(new DatabaseError('Unable to write user to database due to error ', err.message));
})};