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!
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) {
– Erick Jonatha Peiker
@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.– Sergio