Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
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
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.
about 4 years ago · Juan Pablo Isaza Report

0

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
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!