While trying to inject prepared statements with MSSQL (tedious) - once I'm trying to execute the request I'm getting the following error:
Database connection failed: Requests can only be made in the LoggedIn state, not the SentClientRequest state
The imports:
import { Connection, Request } from 'tedious';
The hook:
public static async afterConnect(connection, options): Promise<void> {
const client = connection['resource'] as Connection;
let request = new Request('select 42',(err, rowCount, rows) => {
console.log(`${err} ${rowCount} rows`);
});
client.execSql(request);
}
According to tedious I need to chain the request after the previous one, sequelize doesn't seem to pass that data, is there a way to work around this issue?
I've also created an issue on Sequelize GitHub
took some digging inside sequelize code, but found a work around - if anyone has a similar issue.
public static async afterConnect(connection, options): Promise<void> {
const mssql = connection as { resource: Connection; previous: Promise<unknown> };
await new Promise((resolve) => {
let request = new Request('select 42', (err, rowCount) => {
logger.debug(method, `${err} - ${rowCount} rows`);
resolve();
});
await mssql.previous; // failsafe?
mssql.resource.execSql(request);
});
}