Configure API for SQL Server Database

Asked

Viewed 630 times

2

I have an API that I used with Mysql, but I had to switch to SQL Server. Unfortunately I never used SQL Server, I do not know if you need to download something to get. The API was made in JS. The sequelize configuration of my API with MYSQL was like this:

const Sequelize = require('sequelize');    

const config = {
    user: "root",
    database: "api",
    password: "",
    host: "localhost",
    port: 3306,
    dialect: 'mysql',
    max: 10000,
    idleTimeoutMillis: 3000
};

const seq = new Sequelize(
    config.database,
    config.user,
    config.password,
    {
        host:config.host,
        dialect:config.dialect,
        port:config.port,
        logging: false,
        pool: {
            max: config.max,
            min: 0,
            idle: config.idleTimeoutMillis
        }
    }
);

module.exports = seq

I made the following change to test with sql server:

const Sequelize = require('sequelize');    

const config = {
    user: "sa",
    database: "dbPortal",
    password: "",
    host: "localhost",
    port: 1433,
    dialect: 'mssql',
    max: 10000,
    idleTimeoutMillis: 3000
};

const seq = new Sequelize(
    config.database,
    config.user,
    config.password,
    {
        host:config.host,
        dialect:config.dialect,
        port:config.port,
        logging: false,
        pool: {
            max: config.max,
            min: 0,
            idle: config.idleTimeoutMillis
        }
    }
);

module.exports = seq

When I turn on the API it says it was connected, but right away it says it wasn’t. When I test her, she is POST, keeps giving some errors in red at the prompt.

Does anyone know how to configure it to work on SQL Server?

  • You should fix this part: host: "localhost", should be something like host: "localhost\sqlexpress" or host: "localhost\sql(sua versao)"

  • Mine is SQL server management studio, so it would host: "localhost\sqlmanagementstudio" ?

  • when you open your sql management which path to Serve Name this must be your localhost pattern

  • No Maria, SQL Server Management Studio is only the program used to interact with an instance of SQL Server, the instance of SQL Server may be running without the need of Management Studio, but you need Management Studio to view its bases, and its tables, etc. I think @Hudsonph was referring to the instance name. When you enter Management Studio, in Object Explorer, if you right-click on the first item and go to Properties you can see the instance name (General > Name).

  • But I think what you will need is an additional property for the Call Sequelize dialectOptions, see this article: http://raathigesh.com/Connecting-To-MSSQ-with-Sequelize/

  • I did, but when I test the API it gives the following error at the prompt: Unhandled rejection SequelizeHostNotFoundError: Failed to connect to MSSQLSERVERMARIA:1433 - getaddrinfo ENOTFOUND MSSQLSERVERMARIA ...

  • Placed MSSQLSERVERMARIA right on the property host or on the property dialectOptions?

  • placed : dialectOptions: {
 instanceName: 'MSSQLSERVERMARIA'
 } in host I think I’m putting it wrong

  • And, as @Hudsonph said, when you open Management Studio the value that appears in Server name is localhost\MSSQLSERVERMARIA?

  • no, it’s: GUI000123\MSSQLSERVERMARIA

  • then it is not localhost and only GUI000123\MSSQLSERVERMARIA

  • test with host: "GUI000123\MSSQLSERVERMARIA"

  • That, localhost means the instance is in the machine itself, GUI000123 seems to be another machine on the network, so apparently SQL Server is running on another machine and not the one you are using (localhost).

  • I did that, but when I test the API on Postman it loads nothing and at the prompt the error Unhandled rejection SequelizeHostNotFoundError: Failed to connect to GUI000123MSSQLSERVERMARIA:1433 - getaddrinfo ENOTFOUND GUI000123MSSQLSERVERMARIA ....

  • So try to do as it was in the article: host: 'GUI000123', dialectOptions: { instanceName: 'MSSQLSERVERMARIA' }, apparently he doesn’t accept the host+instance name in the field host.

  • That way it doesn’t work either

  • Apparently keeps making mistake of host

  • What is the exact error?

  • of that mistake: Unhandled rejection SequelizeConnectionError: Failed to connect to GUI000624:1433 - Could not connect (sequence)
 at Connection.connection.on.err (C:\Users\Administrador\Documents\APIGuido-master\node_modules\sequelize\lib\dialects\mssql\connection-manager.js:99:22)
 at emitOne (events.js:116:13)
 at Connection.emit (events.js:211:7)
 at Connection.socketError (C:\Users\Administrador\Documents\APIGuido-master\node_modules\tedious\lib\connection.js:875:14)
 at C:\Users\Administrador\Documents\APIGuido-master\node_modules\tedious\lib\connection.js:740:25

  • at SequentialConnectionStrategy.connect (C:\Users\Administrador\Documents\APIGuido-master\node_modules\tedious\lib\connector.js:153:9)
 at Socket.onError (C:\Users\Administrador\Documents\APIGuido-master\node_modules\tedious\lib\connector.js:169:16)
 at emitOne (events.js:116:13)
 at Socket.emit (events.js:211:7)
 at emitErrorNT (internal/streams/destroy.js:64:8)
 at _combinedTickCallback (internal/process/next_tick.js:138:11)
 at process._tickCallback (internal/process/next_tick.js:180:9)

Show 15 more comments

1 answer

1


With the help of comments on my question I managed to resolve:

const config = {
    user: "sa",
    database: "Nomedb",
    password: "",
    host: "GUI122", //NOME QUE APARECE ANTES DA INSTANCIA DO MEU SQL
    port: 1433, //PORTA PADRAO SQL SERVER
    dialect: 'mssql',
    max: 10000,
    idleTimeoutMillis: 3000,
    instanceName: 'MSSQLSERVERMARIA' //INSTANCIA DO MEU SQL SERVER

};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.