Reputation: 5525
I know I can modify rows using the following code:
const data = fs.createReadStream('api.csv')
.pipe(csv.parse({columns: true}))
.pipe(csv.transform(record => {
// can modify row here
return record;
}));
.pipe(csv.stringify({header: true}))
.pipe(process.stdout);
But how do I add new rows? I think I have to write data to the pipeline, probably before csv.stringify
, but I am not sure how to.
Upvotes: 1
Views: 494
Reputation: 19345
If the data to be added depends on the input data, as you indicated in your original question, you can call the transform with a callback function. For example, the following will duplicate each record.
const data = fs.createReadStream('api.csv')
.pipe(csv.parse({columns: true}))
.pipe((data, callback) => setImmediate(() => callback(null, data, data))))
.pipe(csv.stringify({header: true}))
.pipe(process.stdout);
Upvotes: 1
Reputation: 5525
I can write to the stream returned by the pipe
method between the transform
and stringify
.
const data = fs.createReadStream('api.csv')
.pipe(csv.parse({columns: true}))
.pipe(csv.transform(record => {
// can modify row here
return record;
}));
// Can add rows here
data.write({
col1: 1,
col2: 2
});
data.pipe(csv.stringify({header: true})).pipe(process.stdout);
Upvotes: 0