Pipe () to Json or Postgrsql file

I have this code:

var MetaUtil = require('osm-meta-util');
var meta = MetaUtil({
                      'delay': 1000,
                      'start': '000598424', //file number
                      'end': '001122000' //file number
                   }).pipe(process.stdout) //outputs to console

How can I transfer the output to a Json file so that I can later upload it to the Postgresql database.

Yours faithfully

Andrei

+4
source share
1 answer

Create a stream of recording and writing to the file in it:

var fs = require('fs');
var file = fs.createWriteStream('./output.json')
MetaUtil({
    'delay': 1000,
    'start': '000598424', //file number
    'end': '001122000' //file number
}).pipe(file);

Conversely, use a readable stream to read data from a file and insert it into the database according to the database you are using.

I am not familiar with postres, but pg-copy-streamsit seems to provide this functionality to it:

var fs = require('fs');
var pg = require('pg');
var copyFrom = require('pg-copy-streams').from;

pg.connect(function(err, client, done) {
  var stream = client.query(copyFrom('COPY my_table FROM STDIN'));
  var fileStream = fs.createReadStream('some_file.tsv')
  fileStream.on('error', done);
  fileStream.pipe(stream).on('finish', done).on('error', done);
});
+2
source

Source: https://habr.com/ru/post/1619853/


All Articles