Sequelize connection and logging issues

2.8k views Asked by At

I am trying to use Sequelize to connect with a SQL Server 2012 database. When my connection string was clearly wrong, I was seeing ECONN REFUSED messages and timeout. Now, I am not getting any response, despite logging on success and fail, per this code:

import * as Sequelize from 'sequelize';

-- connection string redacted
let seqConn = new Sequelize("mssql://**;Initial Catalog=**;Persist Security Info=True;Integrated Security=SSPI; User ID=**; Password=**")

seqConn
    .authenticate()
    .then(function(err) {
        console.log('Connection has been established successfully.');
    })
    .catch(function (err) {
        console.log('Unable to connect to the database:', err);
    });

I was previously using the syntax:

let seqConn = new Sequelize("DB", "Username", "Password", 
    {
        dialect: 'mssql',
        dialectOptions: {
            instanceName: 'dev'
        },
        host: '**'
    };

But I couldn't find a setting for integrated security or other fancy SQL Server things.

Regardless, my current connection string isn't erroring. But also, it is not saying the connection was established.

I tried passing my seqConn to a model to use it to retrieve a model:

getModel(seqConn): void {
    console.log("Getting");
    var model = seqConn.define("dbo.Model", {
        modelID: Sequelize.INTEGER,
        modelNo: Sequelize.INTEGER,
        modelName: Sequelize.STRING(50),
        modelAge: Sequelize.DATE
    });

    seqConn.sync().then(function() {
        model.findOne()
        .then( function (modelRes){
            console.log("got a result!?");
            console.log(modelRes.get("modelName"));
        })
        .catch( function (error){
            console.log("it didn't work :( ");
        });

    });
    console.log('after getter');
    return;
}

I'm not confident that this is the right way to use sequelize, and I'm still establishing my project structure, but for now I expect to either see a "got a result" or an error, but nothing is logging here from either the then() or the catch(). The before/after getter logs are logging fine.

I have considered that it takes a long time to connect, but my IT says that he saw my successful connection in our SQL logs, and previously I was getting timeouts in about 15,000 ms, I think.

1

There are 1 answers

0
dckuehn On BEST ANSWER

The problem was solved by including another NPM module: `sequelize-msnodesqlv8'

And then the code to connect to Sequelize looked like:

var Sequelize = require('sequelize');

var seqConn = new Sequelize({
    dialect: 'mssql',
    dialectModulePath: 'sequelize-msnodesqlv8',
    dialectOptions: {
        connectionString: 'Driver={SQL Server Native Client 11.0};[REDACTED]'
    }
});

seqConn
    .authenticate()
    .then(function(err) {
        console.log('Connection has been established successfully.');
    })
    .catch(function (err) {
        console.log('Unable to connect to the database:', err);
});