Hi. I think this can be solved something like this:
var pg = require('pg');
//bancoA
var conString = "postgres://username:password@localhost/database";
var client1 = new pg.Client(conString);
//bancoB
var conString = "postgres://username:password@localhost/database";
var client2 = new pg.Client(conString);
You now have a connection to both banks.
To return all records from A:
var query = client1.query('select * from table');
//Acho que isso teria que ser feito tabela por tabela
var rows = [];
query.on('row', function(row) {
rows.push(row);
});
//Esse codigo precisa ser testado.
//Nao tenho absoluta certeza de que isso funciona dessa forma.
//Teoricamente a variavel rows estaria com todos os registros
//que o postgres tinha na tabela.
From this point on, we have to define what logic we are going to use to define which values will be entered in Bank B, since only records that are not present will be present.
I think the best way to do this is to do the same select in client2
. From there we would have a value for the variable rows
with the values already inserted in the table of BancoB. And it would only take a simple logic to delete the values of rowsDeA
that already exist in rowsDeB
.
Remember that depending on the size of the tables and the periodicity that this will happen, this code can be extremely costly. If the idea is to make it a continuous run (to maintain a backup server, or anything like that), it might be better to create a logic that inserts the values in BancoB at the same time as in BancoA.
One more disclaimer: I'm not sure this will work, but worth the test :)