'asyncronous Mysql.createConnection({})

I need to create a mysql connection in nodejs , but the credentials for the mysql comes from a third party credential manager service. Can somebody suggest me a way to achieve this?

database.js - i am using connection from this file in all other database operations

const mysql = require("mysql");
const {env} = require('./globals')

const connection = mysql.createConnection({
    host: env.DATABASE.HOST,
    user: env.DATABASE.USER,
    password: env.DATABASE.PASSWORD,
    database: env.DATABASE.NAME,
    multipleStatements: true
});

connection.connect(function (err) {
    if (err) {
        console.log("Error in DB connection");
        console.log("err", err);
    } else console.log("Connected!");
});

module.exports = connection

globals.js

const {getSecret} = require('../src/service')
require("dotenv").config();

async function getCredentials() {
    const result = await getSecret()
    return JSON.parse(result?.SecretString || {})
}

const credentials =  getCredentials() // not working, and i can't use await here
const env = {
    DATABASE: {
        HOST: credentials.ip_address,
        PASSWORD: credentials.password,
        NAME: credentials.dbname,
        USER: credentials.username,
    },
    SKU: process.env.SKU
}

module.exports.env = env


Solution 1:[1]

Your 2 main options are:

  1. Don't export connection but export an async function that returns a connection.
  2. Write an init() function that sets up your database connection, and ensure it's one of the first things your application calls before anything else.

Solution 2:[2]

Well first, you need to fix up that globals.js file. Logic that depends on an async function must be async itself. You can just move everything into the async function like so:

const {getSecret} = require('../src/service')
require("dotenv").config();

async function getCredentials() {
    const result = await getSecret()
    const credentials = JSON.parse(result?.SecretString || {})

    return {
        DATABASE: {
            HOST: credentials.ip_address,
            PASSWORD: credentials.password,
            NAME: credentials.dbname,
            USER: credentials.username,
        },
        SKU: process.env.SKU
    }
}

module.exports = { getCredentials }

And since even your database connection in database.js depends on this async function, it will have to be async as well:

const mysql = require("mysql");
const {getCredentials} = require('./globals')

const getConnection = getSecret().then(function (env) {

    const connection = mysql.createConnection({
        host: env.DATABASE.HOST,
        user: env.DATABASE.USER,
        password: env.DATABASE.PASSWORD,
        database: env.DATABASE.NAME,
        multipleStatements: true
    });

    connection.connect(function (err) {
        if (err) {
            console.log("Error in DB connection");
            console.log("err", err);
        } else console.log("Connected!");
    });
    
    return connection;

})

module.exports = getConnection

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Evert
Solution 2 skara9