I recently migrated a node.js project from mysqljs to mysql2 (https://www.npmjs.com/package/mysql2) because it had some additional feature that I need. For my database calls, I am exporting a connection that was created by running mysql.createConnection({CONFIG})
that is then imported by other files and then queries are executed using the execute command.
Previous to my transition, in the file where I set up my database connection I had this:
const mysql = require("mysql2");
const dbConfig = require("../config/db.config");
const connection = mysql.createConnection({
host: dbConfig.HOST,
user: dbConfig.USER,
password: dbConfig.PASSWORD,
database: dbConfig.DB,
port: dbConfig.PORT
});
connection.connect(error => {
if (error) throw error;
console.log("DB connect")
});
module.exports = connection;
My code runs fun without the connection.connect function. Does removing it have any impact on performance, or does mysql2 maintain an open connection regardless or is the creation of the connection negligible? Is it better to keep this line prior to the export or remove it?