Sequelize and Tedious Breaking Change on version 10.0.0 in NodeJS API

Viewed 367

I'm upgrading npm modules sequelize and tedious to the latest versions (6.6.2 and 11.0.7 respectively). I can get Sequelize upgraded and working with tedious up to version 9.2.3 but the next version is 10 and there is a breaking change that I can't seem to find much documentation on (see link)

tedious breaking change

Where it seems to be breaking is when I am creating my connection pools. This line "_sqlConnection[p] = new Sequelize(sqlConfig[p]);"

const initSqlConnectionPool = async () => {
try 
{
    // loop through each defined connection in the sql config file
    for (var p in sqlConfig)
    {
        if (Object.prototype.hasOwnProperty.call(sqlConfig,p)) 
        {
            // setup SQL server connection
            try {
                // Believe issue is here because after this statement _sqlConnnection is empty
                _sqlConnection[p] = new Sequelize(sqlConfig[p]);

            } catch (err) {
                debug('Pooling Error', err);
            }

            // test connection and output status
            await _sqlConnection[p]
                .authenticate()
                .then(() => {
                    debug(`Opened SQL connection pool to ${p}`);
                })
                .catch(err => {
                    debug(`Unable to form SQL Connection on ${p}`, err);
                });

        }
    }
    
    // attach to node.js termination events and kill connection pool
    process.once('SIGTERM',await closeSqlConnectionPool).once('SIGINT',await closeSqlConnectionPool);
} 
catch (err) 
{
    console.error(err.message);
}

};

The sqlConfig is imported and it looks like so...

module.exports = {
Pool1: {
    host: process.env.SERVER,
    database: process.env.DB,
    username: process.env.USERNAME,
    password: process.env.PASSWORD,
    dialect: 'mssql', // the sql dialect of the database
    logging: false, // disable logging or provide a custom logging function; default: console.log
    options: {
      enableArithAbort: true,
      encrypt: true
    },
    pool: {
        max: 10, // Maximum number of connection in pool
        idle: 10000, // The maximum time, in milliseconds, that a connection can be idle before being released.
        acquire: 30000 // The maximum time, in milliseconds, that pool will try to get connection before throwing error
    }
}

};

The error I am getting is TypeError: connection.query is not a function when calling the an execute function to run a statement. I've confirmed that the _sqlConnection object is empty, which is what I'm guessing is the problem. Here is the code for the execute function

const execSQL = async (sql,options,host) => {

let result;
let connection;
let errMessage;

try 
{
    // get an SQL server connection
    connection = await getSqlConnection(host);
    result = await connection.query(sql,options);
} 
catch (error)
{
    console.error(error);
    debug(error);
    errMessage = await utils.removeSpecialChars(error.toString());
    result = [{ status: constants.HTTP_INTERNAL_SERVER_ERROR, message: errMessage }];
}
finally
{
    // debug('result', result);
    return new Promise((resolve,reject) => { resolve(result); });
}

}

Here is the getSqlConnection function logic being called also, which is where the _sqlConnection is supposed to be populated

const getSqlConnection = async (host) => {
// check if host parameter was provided
if (host === undefined)
{
    // default the host to first configuration in connection object if not provided
    host = Object.keys(_sqlConnection)[0];
}

return new Promise((resolve,reject) => {
    // check if the sql connection is active
    if (_sqlConnection[host]) {
        debug(`Connection opened to ${host}`);
        resolve(_sqlConnection[host]);
    }
    else {
        resolve({ Error: constants.CONNECTION_OPEN_ERROR + ' (SQL)' });
    }
});

};

0 Answers
Related