Node.js Sequelize connection - node.js

const Sequelize = require('sequelize');
const sequelize = new Sequelize('abc', 'abc', 'abc', {
host: 'localhost',
dialect: 'mssql'
});
I am trying to connect the SQLServer from using above code, but getting below error
on sequelize.authenticate()
Unable to connect to the database: { SequelizeConnectionError: Invalid arguments: "instanceName" must be a string
Note: I am using the latest version of sequelize

Example of my connection
const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
operatorsAliases: Sequelize.Op.Aliases,
freezeTableName: true,
logging: console.log,
dialect: 'mssql',
host: 'host'
});

Related

Sequelize Migration Error: Dialect needs to be explicitly supplied as of v4.0.0

This is my config.js file
require('dotenv').config();
module.exports = {
test: {
username: process.env.DB_USER_2,
password: process.env.DB_PASS_2,
database: process.env.DB_TABLE,
host: process.env.DB_HOST_2,
dialect: "mysql"
},
production: {
username: process.env.DB_USER_PRODUCTION,
password: process.env.DB_PASS_PRODUCTION,
database: process.env.DB_TABLE_PRODUCTION,
host: process.env.DB_HOST_PRODUCTION,
dialect: "mysql"
}
}
This is my index.js file within my models folder
'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.js')[env];
const db = {};
let sequelize;
if (config.use_env_variable) {
console.log(process.env)
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
I have a .env file with the current environment of testing, I'm trying to run a migration right now, Right now I emptied the migration file incase anything there was messing up the sequelize. When I run db:migrate:status it throws the error about the database dialect. Does anyone know why the dialect error gets thrown?
The issue i faced with this was that the index file was set for development, yet there was no development option in my config.js, the issue went away once I updated that.
Update your example to look like this:
require('dotenv').config();
module.exports = {
development: {
username: process.env.DB_USER_2,
password: process.env.DB_PASS_2,
database: process.env.DB_TABLE,
host: process.env.DB_HOST_2,
dialect: "mysql"
},
test: {
username: process.env.DB_USER_2,
password: process.env.DB_PASS_2,
database: process.env.DB_TABLE,
host: process.env.DB_HOST_2,
dialect: "mysql"
},
production: {
username: process.env.DB_USER_PRODUCTION,
password: process.env.DB_PASS_PRODUCTION,
database: process.env.DB_TABLE_PRODUCTION,
host: process.env.DB_HOST_PRODUCTION,
dialect: "mysql"
}
}
update your config file and you should be good to go.

Cannot connect to mysql remote database

I try to connect to a remote database using node.js and mysql:
const mysql = require('mysql2/promise');
(async () => {
try {
db = await mysql.createConnection({
host: 'jdbc:mysql://REMOTE_IP',
port: '3306',
user: '***',
password: '***',
database: '***',
});
console.log('DB connection established.');
} catch(e) {
console.log(`DB connection error: ${e}.`);
}
})();
But I get in console:
DB connection error: Error: getaddrinfo ENOTFOUND jdbc:mysql://REMOTE_IP.
I've tried to connect with the same details via workbench and I could connect.
Why doesn't this node.js code work?
According to the documentation, you should only set the host name or IP address for host:
const db = await mysql.createConnection({
host: 'REMOTE_IP',
port: '3306',
user: '***',
password: '***',
database: '***',
});
(Note: I also added a const in front of the db variable.)

Sequelize dont use the db name in config and try to connect to "postgres"

I'm trying to connect to a elephantSQL server, but sequelize try to connect to a database with the name "postgres" ignoring the name declared in the config.
Here is the error, note that the database are "postgres":
ERROR: no pg_hba.conf entry for host "168.196.104.204", user "bxutatfx", database "postgres", SSL off
and the config:
module.exports = {
dialect: "postgres",
host: "tuffi.db.elephantsql.com",
username: "bxutatfx",
password: "dbPassword",
database: "bxutatfx",
define: {
timestamps: true,
underscored: true,
},
};
and here the connection:
const Sequelize = require("sequelize");
const dbConfig = require('../config/database');
const connection = new Sequelize(dbConfig);
module.exports = connection;
If more information is needed just ask, Thanks :)

Sequelize Dialect needs to be explicitly supplied as of v4.0.0 (getting started way not CLI)

I am trying to connect ot mssql with Sequelize using
const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'mssql'
});
or
const sequelize = new Sequelize('msssql://user:pass#example.com:5432');
and getting Dialect needs to be explicitly supplied as of v4.0.0
I do not use CLI just getting started code.
regars

sequelize-postgres Error code 91

If you use sequelize as your ORM with postgres database, you may see this error code, especially for first-timer, including me. Found out the problem from this discussion .
Just define the 'port' property with 5432 (default postgres port) will fix the problem.
var orm = new sequelize('database', 'user', 'password', {
dialect: 'postgres',
host: 'localhost',
port: '5432' /* define database port */
});

Resources