我們一直試圖在環回中實現ACID事務,但未取得成功。文檔中的唯一示例使用「創建」方法。我們已經嘗試完全覆蓋事件以及操作掛鉤的多種變體。我們只能得到創建的例子才能工作。如何在環回中實現ACID事務
核心訴求:我們需要能夠(與SQL或者使用回送的ORM功能或直接)開始在創建和更新方法交易的具體型號,然後在交易中更新多個表與能力基於業務規則進行提交或回滾。例如例如,我們需要端點能夠接受包含其頭和明細交易的發票,驗證它,更新各種表(如庫存和客戶),然後保存(或回滾)所有更改一個ACID事務。
使用數據庫事務: https://docs.strongloop.com/display/public/LB/Using+database+transactions)
下面是從文檔的例子:
Post.create({title: 't1', content: 'c1'}, {transaction: tx}, function(err, post) {
post.updateAttributes({content: 'c2', {transaction: tx}, function(err, newPost) {
//
newPost.reviews.create({content: 'r1'}, {transaction: tx}, function(err, newPost) {
});
}
});
操作鉤: https://docs.strongloop.com/display/public/LB/Operation+hooks
我們只能夠成功地重寫幾個核心方法:
Item.create = function(id, cb){
console.log('create'); // Success!
}
//
Item.find = function(id, cb){
console.log('find'); // Success!
}
Item.findById = function(id, cb){
console.log('findById'); // Success!
}
Item.getById = function(id, cb){
console.log('updateAttributes'); // Did not work!
}
Item.all = function(id, cb){
console.log('all'); // Did not work!
};
Item.findOrCreate = function(id, test, cb){
console.log('findOrCreate'); // Did not work!
}
Item.replicate = function(id, test, cb){
console.log('replicate'); // Did not work!
}
Item.save = function(id, test, cb){
console.log('save'); // Did not work!
}
Item.updateAll = function(id, test, cb){
console.log('updateAll'); // Did not work!
}
Item.upsert = function(id, test, cb){
console.log('upsert'); // Did not work!
}
Item.updateAttribute = function(id, cb){
console.log('updateAttribute'); // Did not work!
};
Item.updateById = function(id, test, cb){
console.log('updateById'); // Did not work!
}
實現保存,更新等操作中鉤沒有工作之一:
Item.observe('before save', function(ctx, next) {
console.log('before save');
ctx.Model.beginTransaction({isolationLevel: ctx.Model.Transaction.READ_COMMITTED}, function(err, tx) {
// Now we have a transaction (tx)
console.log('begin transaction', err);
//tx.commit(function(err) {
// console.log('commit', err);
//})
//tx.rollback(function(err) {
// console.log('rollback', err);
//})
next();
});
})
到目前爲止,我們一直無法找到任何完整的文檔(包括Loopback文檔)或使用Loopback的完整ACID事務的任何完整示例。此功能是否實際完全發佈? – ASA2
經過很多挫折之後,我們決定不使用Loopback中的數據庫事務功能,並實施了Knex SQL查詢生成器。 Knex有一個非常好的,獨立於數據庫的接口,支持多表交易。 – ASA2
嗨,我評論只是爲了添加loopback 2文檔的新鏈接:http://loopback.io/doc/en/lb2/Using-database-transactions.html和http://loopback.io/doc/en /lb2/Operation-hooks.html –