Sunday, 28 May 2017

Parsing csv (with csv npm) and inserting into db

I'm trying out csv npm with the following in mind:

  • re-arranging/re-naming csv columns
  • inserting each csv row into db (one row at a time)

Following is my code:

var fs = require('fs');
var csv = require('csv');
var input = fs.createReadStream('./csv/test.csv');
var parser = csv.parse({
    delimiter: ',',
    columns: true
})
var transform = csv.transform(function(row) {
    var resultObj = {
        letter1: row.title1,
        letter2: row.title3,
        letter3: row.title5,
        num1: row.title2,
        num2: row.title4,
        num3: row.title6
    }
    // insert each resultObj into db here       
})
input.pipe(parser).pipe(transform)

My thought is to include db insertion code within var transform. Is this good practice from a design standpoint? If not, what would be a better structure?

Any other comments welcome!



via artze

No comments:

Post a Comment