Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda