My understanding is that they both create writeable streams, but I'm unsure of what the point of parsing the data is if you just go and format it afterwards like in the example below.
fs.createReadStream(path.resolve(__dirname, 'assets', 'snake_case_users.csv'))
.pipe(csv.parse({ headers: true }))
.pipe(csv.format({ headers: true }))
.transform((row, next) => {
User.findById(row.id, (err, user) => {
if (err) {
return next(err);
}
return next(null, {
id: row.id,
firstName: row.first_name,
lastName: row.last_name,
address: row.address,
// properties from user
isVerified: user.isVerified,
hasLoggedIn: user.hasLoggedIn,
age: user.age,
});
});
})
.pipe(process.stdout)
.on('end', () => process.exit());
I think the reason for the misunderstanding is that the example you've given is a bit wrong. The actual code should have the
formatstream piped after the transform.The code should look a bit like this:
The idea of this is:
I also corrected the code so that is has the
transformbased oncsv.transform.See more here in their API docs.