N
N
Nik_Set_72019-10-23 17:56:45
JavaScript
Nik_Set_7, 2019-10-23 17:56:45

How to restore connect with Client after disconnection with postgres?

When I have a disconnect with postgres, I re-create the client and re-create connect with it
. But after the connection, I cannot make requests.
Question, what could be the matter (requests are queued from my promises)?
The only hint I found is
https://github.com/brianc/node-postgres/issues/1558

const { Client: pgClient } = require('pg');

...restart() {
   this.db = new pgClient({
      host: ...,
      port: ...,
      database: ...,
      user: ...,
      password: ...
   });

   this.db.connect()
      .then(() => {
          ...
      })
      .catch((err) => {
          ...
      })  

    this.db.on('error', (err) => {
      this.db.query('SELECT NOW() as now')
         .then(() => {
             ...
         })
         .catch(e => {
             ...
            setTimeout(...restart.bind(this), 1000);
         })
    })
}

...methodQuery = function() {      
   return new Promise((resolve, reject) => {
      this.db.query('SELECT NOW() as now')
         .then(() => {
            resolve()
         })
         .catch((err) => {
            reject(err) 
         })
   }).then(() => {
      ...
   });
};

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question