Search code examples
node.jspostgresqlnode-postgres

Postgres + NodeJS inserts, but doesn't end the script


I have this script, that consumes a SOAP XML, makes it a JSON, and use it to load data on a PostgreSQL DB for grafana.

On my main script, I do this

... do stuff

const req = http.request(conn, (res) =>
                         {
                             let data='';
                             res.on('data', (chunk) =>
                                    {
                                        data += chunk;
                                    });
                             res.on('end', () =>
                                    {
                                        var dados=processaDados.processaDados(processedXML);
                                        cargaBanco.gravaDados(dados);  
                                    });
                         }
                        );
... do stuff

req.end();

My problem is on cargaBanco.gravaDados(dados)

dados is an JSON Object I pass for a function that will load the JSON data on a PostgreSQL DB. I'm using node-postgres DB.

This code below is from cargaBanco.gravaDados


var pg = require('pg');
var config = require('./config');

module.exports = {
  gravaDados: dados => {
    var agora = new Date();

    var totalDados = dados.length;

    var connection = new pg.Pool({
      connectionString: config.conn_string,
      max: 1,
      idleTimeoutMills: 5000,
      connectionTimeoutMills: 2000
    });

    if (typeof connection === 'unindentified') console.err('ERRO!');

    const parameterQuery =
      'INSERT INTO taskstatus VALUES(Default, CAST($1 as timestamp), $2, $3, $4, $5, $6, $7) returning *';

    connection.connect(async (err, client, done) =>
      // connection.connect( (err, client, done) =>
      {
        if (err) {
          console.log(err.stack);
        }
        for (linha of dados) {
          const dadosInserir = [
            agora,
            linha['Resource_Name'][0],
            linha['System'][0],
            linha['Resource_Type'][0],
            linha['Compound_Status'][0],
            linha['Observed_Status'][0],
            linha['Desired_Status'][0]
          ];

          await client.query(parameterQuery, dadosInserir, async (err, results) =>
            // client.query(parameterQuery,dadosInserir, (err, results)) =>
            {
              if (err) {
                console.log(err.stack);
              }
              // await client.release;
            }
          );
          // await client.release;
          // done();
        }
      }
    );
    connection.end();
  }
};

Running this script results on the data being loaded on the DB, but the script locks, doesn't getting out back to shell and even getting no timeout...

I tried to use also await client.release before... But nothing...

Any help? Anything wrong I'm doing?

Sorry about including only code snippets, but it's internal job so I needed to somehow decharacterize...


Solution

  • pg-promise is a better library to work with promises for Postgres with nodejs if you are not familiar with callbacks.