Vini
Vini

Reputation: 39

sequelize - not able to make connection with MSSQL

I am trying to setup connection with MS SQL but for some reason, sequelize is not able to pass the hostname correctly, Instead of pass 'localhost\SQLEXPRESS' it pass 'localhostSQLEXPRESS'. Any idea where to fix it ?

'use strict';
const _ = require('lodash');
var Sequelize = require('sequelize');
var sequelize = new Sequelize('mydb', 'db_user', 'db_user', {
    host: 'localhost\SQLEXPRESS',
    dialect: 'mssql',
    pool: {
        max: 5,
        min: 0,
        idle: 10000
    },
});
// define model 
var batchDetails = sequelize.define('batchDetails', {
    id: {
        type: Sequelize.STRING,
        autoIncrement: true,
        field:'id',
        primaryKey: true
    },
    batch_no: {
        type: Sequelize.STRING,
        field:'batch_no',
    },
    date: {
        type: Sequelize.DATE,
        field:'date',
    }
})

batchDetails.sync({force: true}).then(function () {
    // Table created
    return User.batchDetails({
      id: 1,
      batch_no: 'CASFR342'
    });
  });

error log:-

sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators node_modules\sequelize\lib\sequelize.js:242:13 tedious deprecated The default value for options.encrypt will change from false to true. Please pass false explicitly if you want to retain current behaviour. node_modules\sequelize\lib\dialects\mssql\connection-manager.js:69:26 Unhandled rejection SequelizeHostNotFoundError: Failed to connect to localhostSQLEXPRESS:1433 - getaddrinfo ENOTFOUND localhostSQLEXPRESS at Connection.connection.on.err (C:\Node JS Workspace\db\node_modules\DB\node_modules\sequelize\lib\dialects\mssql\connection-manager.js:97:22) at emitOne (events.js:116:13) at Connection.emit (events.js:211:7) at Connection.socketError (C:\Node JS Workspace\db\node_modules\DB\node_modules\tedious\lib\connection.js:1016:14) at C:\Node JS Workspace\db\node_modules\DB\node_modules\tedious\lib\connection.js:861:25 at GetAddrInfoReqWrap.callback (C:\Node JS Workspace\db\node_modules\DB\node_modules\tedious\lib\connector.js:69:18) at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:104:17)

Upvotes: 2

Views: 2639

Answers (2)

Jurica Smircic
Jurica Smircic

Reputation: 6455

This configuration works on my local sql server express:

var sequelize = new Sequelize('db', 'db_user', 'db_pwd', {
    host: 'localhost',
    dialect: 'mssql',
    dialectOptions: {
     options: { instanceName: "sqlexpress" }
   }
});

The trick was in the dialectOptions.instanceName property.

Upvotes: 2

Michael Lean
Michael Lean

Reputation: 1

Update your host path.

  • Before: localhost\SQLEXPRESS
  • After: localhost\\SQLEXPRESS

Upvotes: 0

Related Questions