简体   繁体   中英

How to make dynamic number of mysql queries in nodejs?

I'm struggeling a bit with how to shoot several SQL update queries to the MySQL server from NodeJS. I need some kind of synchronous execution.

Situation :

  • I have an array of objects. (Lets say "formValues")
  • Some of these objects might have a corresponding row in a database table, others don't.
  • I need to do a MySQL query for each object in the array to know for which object a new row in MySQL needs to be created and which one only needs to be updated.
  • at the end I need to update / create the rows in MySQL table.
  • I need kind of one callback for the whole process.

This is more or less a general question on how to solve situations like this with NodeJS. As I understood, MySQL queries are executed asynchronously.

How can it be achieved, that I can loop through the array to build a list of entries which need to be updated an other to be created in MySQL table? Is there any "synchronous" MySQL query?

Regards

Jens

You can probably really benefit from switching to an async/await MySQL client. Chances are that the client you are using already has support for this.

But even if yours doesn't, and you can't switch it's still by far the easiest to write a helper function that converts your callback-based mysql to a promise-based one.

Effectively the approach becomes:

for(const formValue of formValues) {
   await mysql.query('....');
}

Doing this without async/await and with callbacks is significantly harder.

I imagine one approach might be something like:

function doQueriesForFormValue(formValues, cb) {

   const queries = [];
   for(const formvalue of formValues) {
     queries.push('...');
   }

   runQueries(queries, cb);

}

function runQueries(queries, cb) {

  // Grab the first query
  const currentQuery = queries[0];

  mysql.query(currentQuery, (res, err) {

    if (err) {
       cb(null, err);
    }

    if (currentQueries.length>1) {
      // Do the next query
      runQueries(currentQueries.slice(1), cb);
    } else {
      // call final callback
      cb(true);
    }  

  });

}

I wrote an article with some effective patterns for working with MySQL in Node.js. Perhaps it's helpful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM