This is my code to get an array of records and write them as CSV string one by one in a file. I cannot get the writable stream to write in the file. It just erases whatever is in the file and does not write the new data in.
const writeStream = fs.createWriteStream(process.env.SAVED_FILE);
const transformStream = new Stream.Transform({
writableObjectMode: true,
readableObjectMode: true,
})
transformStream._transform = (chunk, encoding, callback) => {
console.log(chunk);
transformStream.push("\"" + chunk.join("\",\"") + "\"");
callback();
}
const readable = new Stream.Readable({objectMode: true})
.pipe(transformStream)
.pipe(writeStream);
let records = [["a", "b"], [1, 2], ["c", "d"], [8, 9]];
records.forEach(record => readable.push(record));
readable.push(null);
writeStream.on("error", function (err) {
console.log("there is an error!")
});
writeStream.on("end", function () {
console.log("reached the end of the stream!")
});
Any help appreciated on this basic example !