A
A
Anton Anton2017-03-12 22:27:20
MySQL
Anton Anton, 2017-03-12 22:27:20

How to get data from a table right after inserting into it?

I am writing a simple application on node + mysql https://github.com/mysqljs/mysql
Everything seems to work, but there is one caveat:
When inserting several rows and getting data from a table after inserting - in some cases, some of the data may be missing (as like insert delayed or getting data before the inserts are done).

var pool = mysql.createPool(settings.mysql());

pool.queryPromise = function (sqlquery, params) {
  return new Promise((resolve, reject) => {
    this.getConnection(function (err, connection) {
      // Use the connection
      connection.query(sqlquery, params, function (error, results, fields) {

        connection.release();
        if (error) {
          return reject(new Error(error));
        } else {
          return resolve(results);
        }

      });
    });
  });
}
//....
    Promise.all(sheduleResult.map(elem => {
      return pool.queryPromise('insert into table set ?', [elem]);
    })).then( () => {
      return pool.queryPromise('select * from table', [])
    }).then( result => {
      //тут работаем с результатом, так вот, там не всегда все вставленные строки
    });

I insert one line at a time, because it's more convenient, I don't care about performance. And I use the connection pool so that there are no critical errors from disconnecting the connection by timeout. How to make Promice.all() resolve when all nested promises are actually fulfilled? Or is this not the reason?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question