Connecting to (LocalDB)\MSSQLLocalDB Sequelize - node.js

I have a question about sequelize and sql server.
So I can connect to my database with "localhost" or name of my computer, but i cant to "(LocalDB)\MSSQLLocalDB". This is my connection parameter.
PASSWORD: "sw",
DB: "BusinessDB",
CONFIG: {
host: '(LocalDB)\\MSSQLLocalDB',
dialect: 'mssql',
dialectOptions: {
options: {
encrypt: true,
}
},
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
define:{
timestamps: false,
}
}
```
And this is the error when i'm trying to connect with this config
> Failed to connect to (LocalDB)\MSSQLLocalDB:1433 - getaddrinfo ENOTFOUND (LocalDB)\MSSQLLocalDB
Someone have the solutions for that. I search on google but I doesn't find a solution.
Thanx

I found a solution with the help of "msnodesqlv8" module.
Now i'm using this configuration for connect to my DB.
dialect: 'mssql',
dialectModule: require('msnodesqlv8/lib/sequelize'),
bindParam: false,
/*logging: false,*/
dialectOptions: {
options: {
connectionString: 'Driver={ODBC Driver 17 for SQL Server};Server= (LocalDB)\\MSSQLLocalDB;Database=MyDB;Trusted_Connection=yes;',
},
},
define:{
timestamps: false,
}
The driver version can be found on ODBC data sources software.(type in windows search bar)

Related

Cpanel SequelizeConnectionError: no pg_hba.conf entry for host X, user X, database X, SSL inactive

With sequelize migrations , i'm using the below config for connection :
production: {
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB,
host: process.env.POSTGRES_HOSTNAME,
port: process.env.POSTGRES_PORT,
dialect: 'postgres',
logging: false,
dialectOptions: {
bigNumberStrings: true,
},
},
When i try to run i't , im getting the error,
SequelizeConnectionError: no pg_hba.conf entry for host X, user X,
database X, SSL off
I tried to solved it with ssl like below:
ssl: true,
dialectOptions = {
ssl: {
require: true,
rejectUnauthorized: false
}
},
But the error becomes
Server does not allow ssl connexion
So 'im in trouble, as the host was not able to help me.
I would appreciate any help.

Node-mssql not able to connect to the server but with tedious it connects

currently i'am using tedious package to connect to the database and do operations but i would like to switch to node-mssql (seems less messy).
The problem i'm getting is connection timeout:
originalError: ConnectionError: Failed to connect to yyy:1433 in 15000ms
code: 'ETIMEOUT',
isTransient: undefined
}
My config with tedious :
const config = {
server: process.env.HOST, // update me
authentication: {
type: 'default',
options: {
userName: process.env.USER, // update me
password: process.env.PASS, // update me
},
},
options: {
// If you are on Microsoft Azure, you need encryption:
database: process.env.DB,
rowCollectionOnDone: true, // update me
},
};
My config with mssql :
const configMssql = {
user: process.env.USER,
password: process.env.PASS,
server: process.env.HOST, // update me
database: process.env.DB,
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30000,
},
options: {
encrypt: false, // for azure
trustServerCertificate: false, // change to true for local dev / self-signed certs
},
};
or
const configMssqlString = `Server=${process.env.HOST},1433;Database=${process.env.DB};User Id=${process.env.USER};Password=${process.env.PASS};Encrypt=false`;
Can't figure out whats wrong

Got AlreadyHasActiveConnectionError error while trying to run migration in Typeorm

I got this error while trying to run Typeorm migration:
AlreadyHasActiveConnectionError: Cannot create a new connection named "default", because connection with such name already exist and it now has an active connection session.
It works on one machine, but doesn't work on another and on Heroku. Maybe there is a specific command to kill all existing connections?
keepConnectionAlive didn't help.
Here is my ormconfig file:
module.exports = {
type: "postgres",
host: process.env.DB_HOST,
port: +process.env.DB_PORT,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
entities: ["dist/**/*.entity.js"],
synchronize: true,
autoLoadEntities: true,
migrations: ["dist/migration/**/*.js"],
cli: {
migrationsDir: "src/migration",
},
ssl: process.env.DB_ENV === 'development' ? false : {
rejectUnauthorized: false
}
};

Migration with Sequelize CLI to DigitalOcean Postgres Database Throwing SSL Error

Connecting to my my DigitalOcean database with Sequelize works fine when I'm not migrating. For example, attempting to create a new table works just fine; the code below successfully connects and creates a new table.
sequelize = new Sequelize(config.use_env_variable, config);
sequelize.authenticate().then(console.log('success')).catch((error) => console.log(error));
sequelize.define('test-table', {
test_id: {
type: Sequelize.INTEGER,
},
});
sequelize.sync();
I have a CA certificate .crt file I downloaded from DigitalOcean that I'm passing in with the Sequelize options. My config.js looks like
development: {
use_env_variable: 'postgresql://[digitalocean_host_url]?sslmode=require',
ssl: true,
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false,
ca: fs.readFileSync(`${__dirname}/../.postgresql/root.crt`),
},
},
},
However when I try to create tables using migrations with
npx sequelize-cli db:migrate
I receive the following output and error:
Parsed url postgresql://[digitalocean_host_url]?sslmode=require
ERROR: no pg_hba.conf entry for host [host], user [user], database [database], SSL off
Which is very strange, because SSL is working when I create a table using just Sequelize sync. I have a .sequelizerc file for the sequelize-cli configurations, which looks like this:
const path = require('path');
const env = process.env.NODE_ENV || 'development'
const config = require('./config/config')[env];
module.exports = {
'config': path.resolve('config', 'config.js'),
'url': config.use_env_variable,
'options-path': path.resolve('config', 'sql-options.json')
}
inside my sql-options.json I have the following
{
"use_env_variable": "postgresql://[digitalocean_host_url]?sslmode=require",
"dialect":"postgres",
"ssl": true,
"dialectOptions": {
"ssl": {
"required": true,
"rejectUnauthorized": true,
"ca": "/../.postgresql/root.crt"
}
}
}
I've tried a lot of the advice from various resources, including the sequelize/cli repo. But none of it seems to work. Any advice would be helpful.
I had the same issue and the fix was to add the code below in the migrations config file even though you already have it in the database connection file.
The following code is in the config/config.js file for migrations.
production: {
username: ****,
password: ****,
database: ****,
host: ****,
dialect: ****,
port: ****,
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false,
},
},
},
This is how my DB connection looks like that was working normally.
const sequelize = new Sequelize({
host: ****,
database: ****,
username: ****,
password: ****,
dialect: ****,
port: ****,
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false,
},
},
});

How to convert UTC to local time zone in PostgreSQL while fetching data from database

I have used all the options to use local time zone instead of UTC but not work properly as my database connection below any body help to solve this.
const sequelize = new Sequelize(process.env.POSTGRES_DBNAME, process.env.POSTGRES_USERNAME, process.env.POSTGRES_PASS, {
dialect: 'postgres',
logging: false,
host: process.env.POSTGRES_HOST,
port: process.env.POSTGRES_PORT,
pool: {
max: Number(process.env.MAX_DB_CONNECTIONS) || 100,
min: Number(process.env.MIN_DB_CONNECTIONS) || 50,
},
dialectOptions: {
useUTC: true, // -->Add this line. for reading from database
},
timezone: "Asia/Karachi"
});

Resources