5

I'm currently migrating an old PHP project to Node.js and are now struggeling with the mysqljs/mysql module.

I have to execute a bulk insert - so far so good, it works great. But I also need all insertId's. Unfortunately I only get the FIRST insertId in the callback.

Aussuming the variable "nestedDataArray" has four data arrays to be inserted - so we should get 4 new rows. Here is an example:

db.query({
    sql: 'INSERT INTO `table` (`field1`, `field2`, `field3`) VALUES ?;',
    timeout: 5000,
    values: [ nestedDataArray ] // This works as intended.
}, function(error, result){
    if (error) {
        // ... handle error
    } else {
        var id = result.insertId; // Here I get only the first one. What about the other three inserts?

        db.query( /* Yes, there are nestes queries. */ );
    }
});

Moving the db.query in a for-loop doesn't work, because in my "real" code I have to nest multiple queries which depend on the query of the previously executed queries.

Is it possible to get all insertId's from a bulk insert?

Thank you in advance!

Morph
  • 173
  • 2
  • 5
  • 15

0 Answers0