question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Does knex support Windows Authentication for MS SQL?

See original GitHub issue

I’m trying to connect to MS Sql using a config that looks like:

{
    client: "mssql",
    connection: `Server=0.0.0.0,1433;Database=MyApp;User Id=MyApp;Password=my_password`,
}

But am receiving the error:

The "config.server" property is required and must be of type string.

It looks like the connection string is getting parsed into:

{
  "client": "sqlite3",
  "connection": {
    "filename": "Server=0.0.0.0,1433;Database=MyApp;User Id=MyApp;Password=my_password"
  }
}

Does knex support an MS SQL connection string? Based on the error I’m receiving and this issue my sense is that the answer is no, but I wanted to be certain.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
FabianTecommented, Aug 30, 2019

Maybe this page might interest some of you: https://www.connectionstrings.com/sql-server-2016/

Edit: I looked a bit further into this. What I don’t understand is, that you guys say that you snippets work. Currently, knex.js uses the tedious driver hard-coded.

I do know, how to create a connection with the nodedesqlv8 driver (see below) but I don’t have any idea on how to force knex to use that driver. (I would be willing to change the knex implementation, even on a fork if I have to)

    const mssql = require('mssql/msnodesqlv8');
    const con = {
        server: 'localhost',
        database: 'mydb',
        domain:'mylocalserver',
        user: 'Admin',
        options: {
            trustedConnection: true,
            instanceName: 'myinstance'
        }
    }
    // con.options.trustedConnection = true;
    console.dir(con);
    const client = await mssql.connect(con);
2reactions
hrmartinezcommented, Nov 8, 2021

I was able to connect using msnodeqlv8 by making a new dialect based on this example

I just added the msnodesqlv8 module and used the following code, hopefully it helps someone out:

require('mssql/msnodesqlv8');

let Knex = require("knex");

let Dialect = require(`knex/lib/dialects/mssql/index.js`);
Dialect.prototype._driver = () => require('mssql/msnodesqlv8');

let sql = Knex({
  client: Dialect,
  connection: {
    server: "<sql server>",
    port: 1433,
    database: "<database name>",
    driver: "msnodesqlv8",
    options: {
        trustedConnection: true
      }
  }
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to connect to SQL Server with Windows authentication ...
knex uses mssql which in turn uses either tedious or msnodesqlv8 . tedious doesn't support Windows Authentication. The default is tedious .
Read more >
Connecting To Microsoft SQL Server From Node.js Using ...
Connecting To Microsoft SQL Server From Node.js Using Windows Authentication. I usually use Knex.js to connect to databases from Node.js ...
Read more >
Authenticate User from SQL Server with Nodejs, Promises and ...
This videos demonstrates as to how we can authenticate user credentials from SQL Server with the help of Nodejs, Promises and Knex Library....
Read more >
Connecting Nodejs with MS SQL Server through Knex npm ...
This videos demonstrates how to connect Nodejs with MS SQL Server with Knex npm package. It explains how to pass on the configuration ......
Read more >
Overriding Knex tedious driver with msnodesqlv8 : r/node
Does anyone know how to override tedious? We use Windows authentication to connect to our sql server, unfortunately, this isn't possible ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found