我想在一个bookhelf事务中更新许多数据库表。我可以使用一些帮助重构我的代码。我是节点的新手并且对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));
})};
答案 0 :(得分:4)
将promises-for-results保存到变量中,您可以在最后一次使用它们,然后保证它们被完成,从而可以.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));
})};