Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
Get all returning values from a transaction containing multiple queries - knex.js

I'm trying to update some rows at once.

I'm waiting for all queries to be written before executing them all in one query. However, I need some values to be returned by the query after the update. I'm using the returning() method for that.

I can't manage to get all the returned values at once as an array after the query has been executed. If I use a then() directly in the transaction function it returns the returned values one by one and my promise won't work. It also considerably increases the execution time.

How do I get all the returning values from my update request at once in a list.

return await knex.transaction(trx => {
        const queries = [];
        data.forEach(pair => {
            const query = knex('pair')
                .where('symbol', pair.s)
                .update({
                    last_price: pair.c,
                })
                .returning(['id'])
                .transacting(trx)
                //.then(function (updated) {
                // ... doing it one by one here
                //})
            queries.push(query);
        });

        return Promise.all(queries) // Once all queries are written
            .then(() => { 
                trx.commit // We try to execute them all

            .catch((e) => {
                trx.rollback // And rollback in case any of them goes wrong
                console.error(e)
            }); 
    })
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I found a solution without using trx.commit():

async function updateDB(data) {
    return await knex.transaction(async (trx) => {
        const queries = [];

        data.forEach(item => {
            const query = knex('core_exchangepair')
                .where('symbol_slug', item.s)
                .transacting(trx) 
                .update({
                    last_price: item.c,
                })
                .returning(['id'])
            queries.push(query);
        });

        try {
            return await Promise.all(queries);
        } catch (e) {
            console.error(e);
            return e;
        }
    })
}
about 4 years ago · Juan Pablo Isaza Report

0

The await after the return is unnecessary. You need to await your Promise.all before returning it, so your trx function should be async.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!