title: pass argument to mysql connection string
body: <p>Below is my code (under file db.js)</p>
  
  <pre><code>function connection() {
    try {
      const mysql = require('mysql2');
  
      const pool = mysql.createPool({
        host: "mydb.abcd1234.ap-southeast-1.rds.amazonaws.com",
        database: "mydatabase",
        user: "user",
        password: "password", 
        connectionLimit: 11,
        waitForConnections: true,
        queueLimit: 0,
        namedPlaceholders: true
      });
  
      const promisePool = pool.promise();
  
      return promisePool;
    } catch (error) {
      return console.log(`Could not connect - ${error}`);
    }
  }
  
  const pool = connection();
  
  module.exports = {
    connection: async () =&gt; pool.getConnection(),
    execute: (...params) =&gt; pool.execute(...params)
  };
  </code></pre>
  
  <p>And to use it, I simply add <code>var db = require('./../db.js');</code> at the top of the files that requires it. Additionally, below is how I'd execute any sql statements</p>
  
  <pre><code>const myResult = await db.execute("Select COUNT(*) AS dataCount from teachers WHERE username = ?", [username]);
  </code></pre>
  
  <p>But now I need to separate my database according to countries. So my database would be named with <code>mydatabase_</code> + the country code, for example <code>mydatabase_my</code> , <code>mydatabase_jp</code>, <code>mydatabase_uk</code> etc etc. So how do I pass the country code to the <code>function connection()</code> ? </p>
  
  <p>I tried something like </p>
  
  <pre><code>function connection(country) {
    try {
      const mysql = require('mysql2');
  
      const pool = mysql.createPool({
        host: "mydb.abcd1234.ap-southeast-1.rds.amazonaws.com",
        database: "mydatabase_" + country,
        user: "user",
        password: "password", 
        connectionLimit: 11,
        waitForConnections: true,
        queueLimit: 0,
        namedPlaceholders: true
      });
  
      const promisePool = pool.promise();
  
      return promisePool;
    } catch (error) {
      return console.log(`Could not connect - ${error}`);
    }
  }
  
  const pool = connection(country);
  
  module.exports = {
    connection: async (country) =&gt; pool.getConnection(country),
    execute: (...params) =&gt; pool.execute(...params)
  };
  </code></pre>
  
  <p>and at the calling page, I did</p>
  
  <pre><code>var db = require('./../db.js');
  db.connection("my")
  </code></pre>
  
  <p>but for the above, I will get <code>ReferenceError: country is not defined</code> which refers to country at the line <code>const pool = connection(country);</code></p>