node.js Knex 事务与承诺

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22957032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:07:46  来源:igfitidea点击:

Knex Transaction with Promises

databasenode.jstransactionspromise

提问by david

I am getting the correct output, and indeed, these two operations are being treated as a single transactional unit; where if one fails, both fail.

我得到了正确的输出,实际上,这两个操作被视为一个事务单元;如果一个失败,两个都失败。

In this code example: i am doing a transaction of

在这个代码示例中:我正在做一个交易

(1) insert (2) update

(1) 插入 (2) 更新

The way I approach it is to nest my db operations inside the .then. My question is if this code is correct by accident? i am new to promises and knex.

我处理它的方法是将我的数据库操作嵌套在 .then 中。我的问题是这段代码是否偶然正确?我是 promises 和 knex 的新手。

knex.transaction(function(t) {
   knex('foo')
   .transacting(t)
   .insert({id:"asdfk", username:"barry", email:"[email protected]"})
   .then(function() {
       knex('foo')
       .where('username','=','bob')
       .update({email:"[email protected]"})
       .then(t.commit, t.rollback)
   })
})
.then(function() {
 // it worked
},
function() {
 // it failed
});

This works, but I feel like I am doing something wrong still. Looking for comments.

这有效,但我觉得我仍然做错了什么。寻找评论。

回答by Esailija

You need to return a promise from the inner query in order for the outer chain to be chained with that.

您需要从内部查询返回一个承诺,以便将外部链与它链接起来。

You also swallow any errors because you don't rethrow them - it's better to use .catch()for this reason because it makes it more clearer what is happening - that is what would happen with normal try-catchstatement.

您还可以吞下任何错误,因为您不会重新抛出它们 -.catch()出于这个原因,最好使用它,因为它可以更清楚地了解正在发生的事情 - 这就是正常try-catch语句会发生的情况。

knex.transaction(function(t) {
   return knex('foo')
   .transacting(t)
   .insert({id:"asdfk", username:"barry", email:"[email protected]"})
   .then(function() {
        return knex('foo')
           .where('username','=','bob')
           .update({email:"[email protected]"});
   })
   .then(t.commit)
   .catch(function(e) {
        t.rollback();
        throw e;
   })
})
.then(function() {
 // it worked
})
.catch(function(e) {
 // it failed
});

To understand it better, here's the synchronous version that is being "emulated":

为了更好地理解它,这里是“模拟”的同步版本:

try {
    var t = knex.transaction();
    try {
        knex("foo")
            .transacting(t)
            .insert({id:"asdfk", username:"barry", email:"[email protected]"});
        knex("foo")
            .where('username','=','bob')
            .update({email:"[email protected]"});
        t.commit();
    }
    catch (e) {
        t.rollback();
        // As you can see, if you don't rethrow here
        // the outer catch is never triggered
        throw e;
    }
    // It worked
}
catch (e) {
    //It failed
}