title: NodeJS MSSQL multiple queries promised
body: <p>I have a lot of problem using the <code>mssql</code> npm library.</p>
  
  <p>Here is my db class :</p>
  
  <pre><code>"use strict"
  const mssql = require('mssql');
  const moment = require("moment");
  let pool_historian = null;
  let connexion_historian = null;
  
  function mapBarChart(array) {
      return new Promise((resolve, reject) =&gt; {
          let result = {}
          result.old = []
          result.this = []
          let currentYear = null;
          for (let i = 0; i &lt; array.length; i++) {
              if (parseInt(moment().format("YYYY")) !== array[i].Annees) {
                  result.old.push(array[i]);
              } else {
                  result.this.push(array[i]);
              }
          }
          resolve(result);
      })
  };
  
  class Historian {
      constructor(data) {
          pool_historian = new mssql.ConnectionPool({
              server: data.host,
              user: data.username,
              password: data.password,
              database: data.historian_db,
              pool: {
                  max: 50,
                  min: 1
              }
          });
      }
  
      getBarChart(sensor, from, to) {
          return pool_historian.connect().then(connector =&gt; {
              return connector.query`SELECT Annees=YEAR(DateTime),Mois=MONTH(DateTime), Valeur=ROUND(sum(AnalogHistory.Value),2) FROM AnalogHistory WHERE AnalogHistory.TagName IN (${sensor}) AND Quality = 0 AND wwVersion = 'Latest' AND wwRetrievalMode = 'Full' AND DateTime &gt;= ${from} AND DateTime &lt;= ${to} AND AnalogHistory.Value &gt; 0 GROUP BY YEAR(AnalogHistory.DateTime),MONTH(AnalogHistory.DateTime) ORDER BY Annees, Mois`.then(result =&gt; {
                  connector.close();
                  return mapBarChart(result.recordset).then(result =&gt; { return result });
                  //return result.recordset;
              }).catch(err =&gt; {
                  return err;
              })
          })
      }
      getLineChart() {
          return pool_historian.connect().then(connector =&gt; {
              let variable = "A_000000000000000000000000000045.PV";
              return connector.query`SELECT Annees=YEAR(DateTime),Mois=MONTH(DateTime),day=DAY(DateTime), Valeur=ROUND(sum(AnalogHistory.Value),2) FROM AnalogHistory WHERE AnalogHistory.TagName IN (${variable}) AND Quality = 0 AND wwVersion = 'Latest' AND wwRetrievalMode = 'Cyclic' AND DateTime &gt;= '20160101 00:00:00.000' AND DateTime &lt;= '20170809 00:00:00.000' AND AnalogHistory.Value &gt; 0 GROUP BY YEAR(AnalogHistory.DateTime),MONTH(AnalogHistory.DateTime), Day(AnalogHistory.DateTime)ORDER BY Annees, Mois`.then(result =&gt; {
                  connector.close();
                  return result.recordset;
              }).catch(err =&gt; {
                  return err;
              })
          })
      }
  
      close() {
          pool_historian.close()
      }
  }
  </code></pre>
  
  <p>This class is used in this "business class" :</p>
  
  <pre><code>const Historian = require(`${__dirname}/historian-query`)
  const Fdedb = require(`${__dirname}/fdedb-query`)
  const moment = require('moment');
  
  moment.locale("fr-FR");
  
  class Graph_Tasks {
      constructor() {
          this.historian = new Historian({ host: "192.168.1.16", username: "******", password: "w***", historian_db: "R******e" })
          this.fdedb = new Fdedb({ host: "192.168.1.16", username: "*****", password: "*****", fde_db: "S*****" })
      }
  
      createGraphForBuilding(code) {
          return new Promise((resolve, reject) =&gt; {
              this.fdedb.getList(code).then(list =&gt; {
                  console.log(list)
                  let datas = [];
  
                  //Foreach item on the list perform these 2 queryes
  
                  Promise.all([this.historian.getLineChart("A_000000000000000000000000000045.PV", moment().subtract(1, "years").startOf("year").format(), moment().format()), this.historian.getBarChart("A_000000000000000000000000000045.PV", moment().subtract(1, "years").startOf("year").format(), moment().format())]).then(results =&gt; {
                      let datas = []
                      datas = { "lineChart": null, "barChart": results[0] };
                      console.log(datas)
                      res.render('index', { title: 'WebGraph', message: 'Yo Yo', datas });
                  })
  
                  console.log(datas)
                  resolve(datas)
  
              }).catch(console.log);
          });
  
      }
  }
  module.exports = Graph_Tasks;
  </code></pre>
  
  <p>As you can see, what I'm trying to do is performing a simultaneous database request. As I read in the documentation, the connection pool must let me do this properly. So when the program arrives at <code>Promise.all</code>, I expected that the 2 requests will be launched simultaneously.</p>
  
  <p>But I get an error :</p>
  
  <blockquote>
    <p>Une exception s'est produite : Error<br>
    Promise Rejection (ConnectionError: Already connecting to database! Call close before connecting to different database.)<br>
    ConnectionError: Already connecting to database! Call close before connecting to different database.<br>
    at ConnectionError (d:\repositories\fde\node_modules\mssql\lib\base.js:1428:7)<br>
    at ConnectionPool._connect (d:\repositories\fde\node_modules\mssql\lib\base.js:235:23)<br>
    at EventEmitter.connect.PromiseLibrary (d:\repositories\fde\node_modules\mssql\lib\base.js:217:19)<br>
    at ConnectionPool.connect (d:\repositories\fde\node_modules\mssql\lib\base.js:216:12)<br>
    at Historian.getBarChart (d:\repositories\fde\class\historian-query.js:39:31)<br>
    at __dirname.createGraphForBuilding.Promise.fdedb.getList.then.list (d:\repositories\fde\class\graph_tasks.js:21:188)<br>
    at process._tickCallback (internal/process/next_tick.js:109:7)</p>
  </blockquote>
  
  <p>So my question is: how to adapt the code to let me perform several queries at the same time (the promise.all for each of my list item)?</p>