Connect to a local SQL Server db with sequelize

柔情痞子 提交于 2019-12-30 13:19:52

问题


Having completed the SQL Server installer, the given connection string is Server=localhost\MSSQLSERVER01;Database=master;Trusted_Connection=True;, which seems like a strange format, and if I try to connect to the db with sequelize using that connection string:

var sequelize = new Sequelize(process.env.DB_STRING);

I get the error:

TypeError: Cannot read property 'replace' of null

at new Sequelize (C:\Users\George\Source\Repos\TestProj\node_modules\sequelize\lib\sequelize.js:132:40) at Object. (C:\Users\George\Source\Repos\TestProj\models\index.js:13:21) at Module._compile (module.js:570:32)


回答1:


Based on this article you should install sequelize-msnodesqlv8:

var sequelize = new Sequelize({
  dialect: 'mssql',
  dialectModulePath: 'sequelize-msnodesqlv8',
  dialectOptions: {
    instanceName: 'MSSQLSERVER01',
    trustedConnection: true
  },
  host: 'localhost',
  database: 'master'
});

or perhaps better:

var sequelize = new Sequelize({
  dialect: 'mssql',
  dialectModulePath: 'sequelize-msnodesqlv8',
  dialectOptions: {
    connectionString: 'Server=localhost\MSSQLSERVER01;Database=master; Trusted_Connection=yes;'
  },
});

But you should not leave default database as Master. Use your database name instead.

Mark this:

There are many node mssql clients and sequelize defaults to using tedious, but being pure javascript,tedious lacks support for integrated security. msnodesqlv8 is a client that interfaces with a native odbc library. This allows integrated security to be used. It does require additional binaries to deploy, but fortunately, msnodesqlv8 is distributed with binaries for the most common architectures

You are using integrated security, so you need to deal with that problem.

See also this question.



来源:https://stackoverflow.com/questions/45903574/connect-to-a-local-sql-server-db-with-sequelize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!