使用 mssql 续集 afterConnect 挂钩(乏味)

sequelize afterConnect hook with mssql (tedious)

在尝试使用 MSSQL 注入准备好的语句时(乏味)- 尝试执行请求后出现以下错误:

Database connection failed: Requests can only be made in the LoggedIn state, not the SentClientRequest state

进口:

import { Connection, Request } from 'tedious';

钩子:

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);
  }

根据繁琐,我需要在前一个请求之后链接请求,sequelize 似乎没有传递该数据,有没有办法解决这个问题?

我还在 Sequelize GitHub

上创建了一个问题

深入研究了 sequelize 代码,但找到了解决方法 - 如果有人遇到类似问题。

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);
    });
}