Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda