title: Is this a good way to run multiple mysql queries with Promise.all
body: <p>So I am running a few insert queries using the mysql npm package and I'm doing so with Promise.all to try and run them at the same time. I have a function that creates a new connection for each query and returns a promise. I am also wrapping the mysql queries with promises. Here is the code that builds the queries and calls them with Promise.all </p>
  
  <pre><code>const Database = require('./database');
  
  const queryBuilder = record =&gt;
    new Promise((resolve, reject) =&gt; {
      try {
        const filename = record.s3.object.key.split('/').pop();
        const filesize = record.s3.object.size;
        const s3path = `${record.s3.bucket.name}/${record.s3.object.key}`;
        const createdAt = record.eventTime.split('T').shift();
        const sql = 'INSERT INTO raw_files (filename, filesize, s3path, created_at, client_subdomain) ' +
         `VALUES ('${filename}', ${filesize}, '${s3path}', '${createdAt}', '${record.s3.bucket.name}')`;
        resolve(sql);
      } catch (err) {
        reject(err);
      }
    });
  
  const connectionWrapper = (params, record) =&gt;
    new Promise((resolve, reject) =&gt; {
      const db = new Database(params);
      db.connect()
        .then(res =&gt; queryBuilder(record))
        .then(sql =&gt; db.query(sql))
        .then((res) =&gt; {
          db.close();
          resolve(res);
        })
        .catch((err) =&gt; {
          db.close();
          reject(err);
        });
    });
  
  exports.handler = (event, context, callback) =&gt; {
    const connectionParams = {
      host: '127.0.0.1',
      port: 3306,
      user: 'root',
      password: 'some_password',
      database: 'space_util',
    };
  
    Promise.all(event.Records.map(record =&gt; connectionWrapper(connectionParams, record)))
      .then(res =&gt; callback(null, res))
      .catch(err =&gt; callback(err));
  };
  </code></pre>
  
  <p>And then here is the wrapper module </p>
  
  <pre><code>const mysql = require('mysql');
  
  // Promise-ify the mysql connection
  const Database = function Database(connectionParams) {
    this.connection = mysql.createConnection(connectionParams);
  };
  
  Database.prototype.connect = function connect() {
    return new Promise((resolve, reject) =&gt; {
      this.connection.connect((err) =&gt; {
        if (err) reject(err);
        resolve();
      });
    });
  };
  
  Database.prototype.query = function query(sql) {
    return new Promise((resolve, reject) =&gt; {
      this.connection.query(sql, (err, results, field) =&gt; {
        if (err) reject(err);
        resolve(results);
      });
    });
  };
  
  Database.prototype.close = function close() {
    return new Promise((resolve, reject) =&gt; {
      this.connection.close((err) =&gt; {
        if (err) reject(err);
        resolve('Connection closed');
      });
    });
  };
  
  module.exports = Database;
  </code></pre>
  
  <p>It works, but I am curious if the way I am doing this seems like a hack or not? Specifically the way I am calling the Promise.all with a map function as the argument. I didn't know how to make an array of connectionWrappers with their params without invoking them, hence the map function in Promise.all(). Any advice would be appreciated!</p>