Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Record' events block any other operation #9

Open
sedenardi opened this issue Jul 20, 2014 · 1 comment
Open

'Record' events block any other operation #9

sedenardi opened this issue Jul 20, 2014 · 1 comment

Comments

@sedenardi
Copy link

Love your library, very easy to use and works pretty well.

I'm having an issue where if I call another function from the 'record' event that performs some sort of operation, like a mysql query, it only performs the operation for the first record. It won't perform the function for subsequent records until after the 'end' event.

For example:

parser.on('start', function() {
  console.log('start parsing');
});
parser.on('record', function(record) {
  var sql = 'insert into table(field1,field2) values(?,?);';
  var inserts = [];
  inserts.push(record.field1);
  inserts.push(record.field2);
  console.log('inserting');
  var cmd = connection.format(sql, inserts);
  connection.query(cmd, function(err, res) {
    if (err) {
      console.log('MYSQL: ' + sql + ' \n' + err);
      return;
    }
    console.log('inserted successfully');
  });
});
parser.on('end', function() {
  console.log('end parsing');
});

If I run the parser on a dataset of 100 records, I'll see 'start parsing', 'inserting' x 100, 'end parsing', then 'inserted successfully' x 100. In my real world application I'm reading datasets with several 100,000 rows, so this issue is much more apparent.

I'm not sure what could be causing the issue, perhaps reading the file as a stream would allow the db operation to flow through. Let me know if you would like any more specific details or troubleshooting, I'd be more than happy to help. Again, thanks for your work on this project, it's proven very useful to me.

@sedenardi
Copy link
Author

To clarify what I mean by "it only performs the operation for the first record". If you follow what I said my console.log() output was, between the time when 'inserting' is outputted and 'end parsing' is outputted, I only see 1 record in the database when I query it separately. After the 'end parsing' event, I then see more records being inserted. Like I said, this is more apparent with my real world dataset.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant