使用 knexjs 连接到 SQL 服务器,无法获取连接

Connect to SQL Server with knexjs, Unable to acquire a connection

在尝试查看了数十个示例、阅读了大部分文档、尝试了许多不同的变体并更改了 SQL 服务器中的许多设置之后,我终于忍不住寻求帮助一.

我使用 SQL 服务器接受的完全相同的连接字符串成功连接到带有 mssqljs 的 tblTextKnex,但现在已经有一段时间无法使用 knexjs 了。

我收到以下警告和错误:

Knex:warning - calling knex without a tableName is deprecated. Use knex.queryBuilder() instead.

Unhandled rejection Error: Unable to acquire a connection

这是我认为应该有效的 unsuccessful/offending 代码。

  var knex = require('knex')({
    client: 'mssql',
    connectionString: "Initial Catalog=TextKnex;Data Source=localhost\TESTINSTANCE;User ID=my_user_id;Password=my_password;Integrated Security=SSPI;"
  });

  knex().connection().then(() => {
     knex('TextKnex').table('Products')
        .select('Products.Price as Price')
        .then((product) => {
            console.log('log product', product);
            console.dir('dir product', product);
            logger.info('Query Data: %j', product);
     })
  });
  knex.destroy();

我很确定,没有 connectionString 属性,并且 connection() 查询生成器方法被记录为不起作用(并且不检查池是否已连接)。在进行任何查询或连接之前,最后还同步调用 knex.destroy() 破坏你的 knex 实例。

试试这个:

  var knex = require('knex')({
    client: 'mssql',
    connection: {
      connectionString: "Initial Catalog=TextKnex;Data Source=localhost\TESTINSTANCE;User ID=my_user_id;Password=my_password;Integrated Security=SSPI;"
    }
  });

  knex('TextKnex').table('Products')
    .select('Products.Price as Price')
    .then((product) => {
      console.log('log product', product);
      console.dir('dir product', product);
      logger.info('Query Data: %j', product);
    })
    .finally(() => {
      knex.destroy();
    });

  var knex = require('knex')({
    client: 'mssql',
    connection: "Initial Catalog=TextKnex;Data Source=localhost\TESTINSTANCE;User ID=my_user_id;Password=my_password;Integrated Security=SSPI;"
  });

  ...

在 knex 测试中,mssql 连接的完成方式有点不同:https://github.com/tgriesser/knex/blob/master/test/knexfile.js#L132