javascript - When to use Knex transacting() vs chaining off the trx object - Stack Overflow

Knex's documentation for transactions has code that looks like this:knex.transaction(function(trx)

Knex's documentation for transactions has code that looks like this:

knex.transaction(function(trx) {
    var books = [
        {title: 'Canterbury Tales'},
        {title: 'Moby Dick'},
        {title: 'Hamlet'}
    ];

    return trx
    .insert({name: 'Old Books'}, 'id')
    .into('catalogues')
    .then(function(ids) {
    return Promise.map(books, function(book) {
        book.catalogue_id = ids[0];

        // Some validation could take place here.

        return trx.insert(info).into('books');
    });
    });
})

Here on SO I've seen extensive use of a function transacting() with examples that look like this:

knex.transaction(function(trx) {
   knex('foo')
  .transacting(trx)
  .insert({id:"bar", username:"bar"})
  // etc
 })

Knex describes transacting() with examples similar to above:

Used by knex.transaction, the transacting method may be chained to any query and passed the object you wish to join the query as part of the transaction for.

My question is:

What is the difference between trx.insert().into('foo') and knex('foo').transacting(trx).insert() and why would you use one instead of the other?

Knex's documentation for transactions has code that looks like this:

knex.transaction(function(trx) {
    var books = [
        {title: 'Canterbury Tales'},
        {title: 'Moby Dick'},
        {title: 'Hamlet'}
    ];

    return trx
    .insert({name: 'Old Books'}, 'id')
    .into('catalogues')
    .then(function(ids) {
    return Promise.map(books, function(book) {
        book.catalogue_id = ids[0];

        // Some validation could take place here.

        return trx.insert(info).into('books');
    });
    });
})

Here on SO I've seen extensive use of a function transacting() with examples that look like this:

knex.transaction(function(trx) {
   knex('foo')
  .transacting(trx)
  .insert({id:"bar", username:"bar"})
  // etc
 })

Knex describes transacting() with examples similar to above:

Used by knex.transaction, the transacting method may be chained to any query and passed the object you wish to join the query as part of the transaction for.

My question is:

What is the difference between trx.insert().into('foo') and knex('foo').transacting(trx).insert() and why would you use one instead of the other?

Share Improve this question edited Oct 18, 2017 at 14:44 Mark asked Oct 18, 2017 at 7:34 MarkMark 92.5k8 gold badges114 silver badges155 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

It is convenient to use .transacting(trx) when you want to perform multiple operations in the same transaction:

knex.transaction(function (trx) {
    return Promise.all([
        knex('foo').insert({ name: 'My Name' }).transacting(trx),
        knex('bar').insert({ field: 'Value' }).transacting(trx)
    ])
    // ---- or something like ----
    return Promise.all(SOME_INPUT_VALUES.map(function (value) {
        return knex('foo_bar').update('lul', value.lul).where('id', value.id).transacting(trx)
    }))
})

Don't know really of a particular usage of the other method. It might be just a matter of style. You got two interfaces and you can pick one whatever you like most. As for me, I'm used to .transacing(trx)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743734736a4498047.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信