WriteStream Node.js

JS
S
JavaScript

Simple example of how to use writeToStream to write a 5 billion record iteration (represented as variable csvRow) onto a file (named from syncFeedId variable)

1import * as fs from 'fs';
2import * as stream from 'stream';
3
4public writeToStream(stream: any, csvRow: any, position: number): any {
5  const header = !(Boolean(position));
6  const json2csvParser = new Json2csvParser({ fields: Object.keys(csvRow), header: header, encoding: 'utf-8', quote: '', delimiter: '\t' });
7  const csv = json2csvParser.parse(csvRow);
8  stream.write(csv);
9  stream.write(`\n`);
10}
11
12const writeStream = fs.createWriteStream(syncFeedId);
13writeStream.once('open', (fd) => {
14  this.writeToStream(writeStream, csvRow, idx);
15});
16// ...
17writeStream.end();

Created on 11/5/2018