Transaction in Postgress on Nodejs

Asked

Viewed 117 times

1

I’m looking to encapsulate my SQL execution in the Postgress database. My initial wish is to call a method to execute a specific query and another method to execute a transaction of various queries.

exports.executaSQL = function (select, listaParametros){
    client.connect(function (err) {
        if (err) throw err;
        client.query(select, listaParametros, function (err, result) {
            if (err) throw err;
            return result.rows;
            client.end(function (err) {
                if (err) throw err;
            });
        });
    });
};

/**
var objetoListaSelect = [];
objetoListaSelect.push({
    select : 'SELECT 1 WHERE ID=$1 AND ID=$2'
    params : [1, 6]
});
objetoListaSelect.push({
    select : 'SELECT 1 WHERE ID=$1 AND ID=$2'
    params : [1, 6]
});
**/
exports.executaTransacao = function (objetoListaSelect){

};

I would like the second Function running execution in the order it was added to the array, reminding the asynchronous execution of the queries, which would be a good solution?

Grateful from the start!

1 answer

1

You can use Promises to do this. Creating a help function to make each encapsulated request in a Promise

function queryPromise(obj) {
    return new Promise(function(resolve, reject) {
        client.query(obj.select, obj.params, function(err, result) {
            if (err) reject(err);
            else resolve(result.rows);
        });
    });
}

then you can use Promise.all() to wait for all the beggars.

Would that be:

var objetoListaSelect = [];
objetoListaSelect.push({
    select : 'SELECT 1 WHERE ID=$1 AND ID=$2'
    params : [1, 6]
});
objetoListaSelect.push({
    select : 'SELECT 1 WHERE ID=$1 AND ID=$2'
    params : [1, 6]
});

exports.executaTransacao = function(objetoListaSelect) {
    return new Promise(function(resolve, reject) {
        client.connect(function(err) {
            if (err) return reject(err);
            Promise.all(objetoListaSelect.map(queryPromise)).then(function(results) {
                client.end(function(err) {
                    if (err) reject(err);
                    else resolve(results);
                });
            });
        });
    });
};

And when you call this function, you get a precedent. And to read the results would be something like:

const executaTransacao = require('./oTeuFicheiro').executaTransacao;

executaTransacao().then(function(resultados){
    console.log(resultados);
}).catch(function(erro){
    console.log(erro);
});
  • Hello Sergio, I haven’t fully tested yet, but I would like to comment that I had to add the new to create Promisse: exports.executaTransacao = function(objetoListaSelect) {
 return _new_ Promise(function(resolve, reject) {
 client.connect(function(err) {

  • @Exact Erickjpeiker, in haste I forgot the new, but that’s right. Tell me later if it worked. I use this kind of encapsulation with Mysql in Node a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.