为了保持我的node.js微服务的干净架构我有控制器,服务和例如库。
我想要一个同步数据流:controller - >服务 - >存储库 - >服务 - >控制器。在此流程中(在简单的用户故事中)存储库将数据返回到服务和服务到控制器。但是存储库应该以异步方式处理对外部存储的请求。所有异步代码只应存在于存储库中,服务应该获取原始数据。
我目前的实施:
UserController.js
module.exports.create = function() {
console.log("In controller: before service call");
let userDto = JSON.parse(this.request.body);
let savedUser = userService.createUser(userDto);
console.log("In controller: after service call");
};
UserService.js
module.exports.createUser = function createUser(userDto) {
let user = require('../domain/user/User');
user.login = userDto.login;
user.password = userDto.password;
let userRepository = require('../infrastructure/user/UserRepository');
Q.spawn(function* () {
console.log("In service before promise resolve");
let savedUser = yield userRepository.createUser(user);
console.log("In service after promise resolve");
return savedUser;
});
console.log("In service: after repository call");
};
UserRepository.js
module.exports.createUser = function createUser(user) {
console.log("In repository: before save call");
return new Q.Promise(function(resolve, reject) {
userEntity.save(function(err, savedUser) {
console.log("In repository: inside callback after save call");
if (err) {
console.log("In repository: inside callback before reject");
reject(Error('Błąd zapisu danych!'));
} else {
console.log("In repository: inside callback before resolve");
resolve(savedUser);
}
});
});
};
日志:
我想获得以下日志序列: