Dialect needs to be explicitly supplied as of v4.0.0 - node.js

I have been working on a NodeJS project which uses PostgreSQL database.
I am trying to implement migration to the database. Also, using Sequelize. After setting up the migration folder and config, it throws error while running db:migrate
The error is:
"Dialect needs to be explicitly supplied as of v4.0.0"

Solution for me was based on what I had set for my NODE_ENV variable.
echo $NODE_ENV
If you do not have anything set for that variable, try setting it with the following:
export NODE_ENV=development
If a value is present, make sure you have an entry in your config file for that value. For me, I like to use local. So I had to update my config to this:
{
local: {
username: 'root',
password: null,
database: 'database_dev',
host: '127.0.0.1',
dialect: 'postgres'
},
development: {
username: 'root',
password: null,
database: 'database_dev',
host: '127.0.0.1',
dialect: 'postgres'
},
test: {
username: 'root',
password: null,
database: 'database_test',
host: '127.0.0.1',
dialect: 'postgres'
},
production: {
username: 'root',
password: null,
database: 'database',
host: '127.0.0.1',
dialect: 'postgres'
}
}

Check the dialect once.
const Sequelize = require('sequelize');
// Option 1: Passing parameters separately
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */
});

I was facing this error, as it turns out, because of typescipt's transformation/compilation.
A little background: I am using sequelize in a typescript project. And the database config file was in a database.ts file.
const config = {
development: {
username: env.PG_USERNAME,
password: env.PG_PASSWORD,
database: 'sample_db',
host: env.PG_HOST,
port: env.PG_PORT,
dialect: 'postgres',
},
test: {
username: env.PG_USERNAME,
password: env.PG_PASSWORD,
database: 'sample_db',
host: env.PG_HOST,
port: env.PG_PORT,
dialect: 'postgres',
},
production: {
username: env.PG_USERNAME,
password: env.PG_PASSWORD,
database: 'sample_db',
host: env.PG_HOST,
port: env.PG_PORT,
dialect: 'postgres',
},
};
export default config;
In .sequelizerc file, I was pointing to the transpiled version of the database.ts file i.e. the dist/config/database.js file. As shown below:
const path = require('path');
module.exports = {
env: process.env.NODE_ENV || 'development',
config: path.resolve('dist', 'config', 'database.js'),
...
};
But after inspecting the transpiled version of the database.ts file, i noticed that the config was exported as:
module.exports.default = config
But sequelize is expecting the config to be at module.exports.
So, I modified the database.ts file by appending this one line to the end of the file and that resolved it for me.
...
module.exports = config;

I got same error and I saw this mistake in the code.
title: {
type: Sequelize,
allowNull: false,
},
Changed my code with this and problem is solved:
title: {
type: Sequelize.STRING,
allowNull: false,
},

Check your config file (env names)
{
development: {
username: 'root',
password: null,
database: 'database_development',
host: '127.0.0.1',
dialect: 'mysql'
},
test: {
username: 'root',
password: null,
database: 'database_test',
host: '127.0.0.1',
dialect: 'mysql'
},
production: {
username: 'root',
password: null,
database: 'database_production',
host: '127.0.0.1',
dialect: 'mysql'
}
}

My issue was that I wasn't pointing to the config file properly. It said "successfully loaded" but I wasn't pointing to the right file.
It should say "Loaded configuration file "[some path]/config.js"."

I think you have missed .env file in your project

In my case issue was the way I was exporting config from config.ts
export const = {} did not work
but module.exports worked:
module.exports = {
development: {
dialect: process.env.DB_DIALECT,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME_DEVELOPMENT,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
},
test: {
dialect: process.env.DB_DIALECT,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME_DEVELOPMENT,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
},
production: {
dialect: process.env.DB_DIALECT,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME_DEVELOPMENT,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
},
};

I had a config.js file from a previous project with a non-standard value for my environment.
It was called current.
I changed it to development and the error went away.
const main = require('./main');
module.exports = {
development: { // this was set to `current` in my case, and it was causing the error
username: main.db.user,
password: main.db.password,
database: main.db.name,
host: main.db.host,
port: main.db.port || 3306,
dialect: 'mysql'
}
};

After pulling my hair out for a couple hours, I realized I was doing cd src; node app.js when really I was supposed to do node src/app.js... Gooooooodness

After reading through all the answers and possible solutions to problems people had and nothing worked for me here is another additional solution for an environment that uses:
Typescript (Having the sequelize config in .ts)
Sequelize
Problem: Not being able to run "sequelize db:migrate" (getting the error from this thread) due to sequelize not getting access to the .env file and cannot properly read the config
Example config
config: any = {
"development": {
"username": dbUser,
"password": dbPassword,
...
module.exports = config
dbUser is undefined for Sequelize
Create a ".sequelizerc" file in the root of your project and give it the path to the already built .js config file:
const path = require('path');
module.exports = {
'config': path.resolve('./dist', 'src/config/config.js')
};
In your config file add
module.exports = config
as sequelize does not like export default config (as mentioned above). I have both types of exports.
Install the dotenv-cli (I did it as a dev dependency, but globally should work fine as well)
Run the migration command while suppling the .env file with:
npx dotenv -e /path/to/.env sequelize db:migrate
Also had to make sure my NODE_ENV is set to development:
export NODE_ENV=development

did you forget to add the dialect to your config?
see: http://docs.sequelizejs.com/manual/tutorial/migrations.html

if you have not setup any .env variables before running your npm server
You are likely to get that error. so each time you restart app for changes.
you will have to export again
export DATABASE_URL=<your-db-url>

This error can also be caused if there is an error in your model schema.
I had:
middle_name: {
type: Sequelize.Sequelize,
allowNull: false,
}
Which should had been:
middle_name: {
type: Sequelize.STRING,
allowNull: false,
}

In my case, I declared config.js like as:
module.exports = {
production: {
database: process.env.DB_PROD_DATABASE,
username: process.env.DB_PROD_USERNAME,
password: process.env.DB_PROD_PASSWORD,
options: {
host: process.env.DB_PROD_HOST,
port: process.env.DB_PROD_PORT,
dialect: 'postgres',
define: {
paranoid: true,
timestamp: true,
freezeTableName: true,
underscored: false
}
}
},
development: {
database: process.env.DB_DEV_DATABASE || 'database_name',
username: process.env.DB_DEV_USERNAME || 'user_name',
password: process.env.DB_DEV_PASSWORD || 'pass',
host: process.env.DB_DEV_HOST || 'localhost',
port: process.env.DB_DEV_PORT || 5432,
dialect: 'postgres',
define: {
paranoid: true,
timestamp: true,
freezeTableName: true,
underscored: false
}
}
}
But it should be like as:
module.exports = {
production: {
database: process.env.DB_PROD_DATABASE,
username: process.env.DB_PROD_USERNAME,
password: process.env.DB_PROD_PASSWORD,
options: {
host: process.env.DB_PROD_HOST,
port: process.env.DB_PROD_PORT,
dialect: 'postgres',
define: {
paranoid: true,
timestamp: true,
freezeTableName: true,
underscored: false
}
}
},
development: {
database: 'database_name',
username: 'user_name',
password: 'pass',
host: 'localhost',
port: 5432,
dialect: 'postgres',
define: {
paranoid: true,
timestamp: true,
freezeTableName: true,
underscored: false
}
}
}
without process.env.DB_DEV_DATABASE || 'database_name' eg.

If you are facing this error then,you have to add additional argument after password_for_rootUser in form of object and need to specify these property according to your RDBMS
var Sequilize=require('sequelize');
var connection =new Sequilize('db_name','root_user_name','password_for_rootUser',{
host:'localhost',
dialect:'mysql'|'mariadb'|'sqlite'|'postgress'|'mssql',
pool:{
max:5,
min:0,
idle:10000
},
//for sqlite only
storage:path/to/database.sqlite
});
var Article=connection.define('tableName',{
title:Sequilize.STRING, // your cloumn name with data type
body:Sequilize.TEXT // your cloumn name with data type
});
connection.sync();
hope this solves your problem

You can fix this by running this command
export NODE_ENV=development; npx sequelize db:migrate
cheers!

In my case, I forgot to uncomment the DB_CONNECTION value from .env which is used to establish postgreSQL connection in my code

use below code in config/config.js to load env
import dotenv from 'dotenv'
dotenv.config()
my .sequelizerc
module.exports = {
'config': path.resolve('config', 'sequelize.js'),
'models-path': path.resolve('src', 'models'),
'seeders-path': path.resolve('src', 'seeders'),
'migrations-path': path.resolve('src', 'migrations')
};
and config/sequelize.js
import dotenv from 'dotenv'
dotenv.config()
export default {
development: {
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: process.env.DB_DIALECT,
dialectOptions: {
bigNumberStrings: true
}
},
test: {
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: process.env.DB_DIALECT,
},
production: {
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: process.env.DB_DIALECT,
dialectOptions: {
bigNumberStrings: true,
}
}
};

Related

Configuration property "dialect" is not defined

I'm having trouble setting up a project with the config npm package.
My config.js looks like this:
require('dotenv').config();
const { DB_HOST, DB_USERNAME, DB_PASSWORD, SENDGRID_API_KEY } = process.env;
const env = process.env.NODE_ENV;
const config = {
development: {
username: DB_USERNAME,
password: DB_PASSWORD,
database: "database_development",
host: DB_HOST,
dialect: "postgres",
sendgrid: {
base_url: 'https://localhost:3002',
sendgrid_api_key: SENDGRID_API_KEY,
sender_email: '',
enabled: true,
},
},
test: {
username: DB_USERNAME,
password: DB_PASSWORD,
database: "database_test",
host: DB_HOST,
dialect: "postgres",
sendgrid: {
base_url: 'https://localhost:3002',
sendgrid_api_key: SENDGRID_API_KEY,
sender_email: '',
enabled: true,
},
},
production: {
username: DB_USERNAME,
password: DB_PASSWORD,
database: "database_production",
host: DB_HOST,
dialect: "postgres",
sendgrid: {
base_url: 'https://localhost:3002',
sendgrid_api_key: SENDGRID_API_KEY,
sender_email: '',
enabled: true,
},
}
};
module.exports = config[env];
In 1 service driver file I have the following line:
const dialect = config.get('dialect');
I get the following error: "Error: Configuration property "dialect" is not defined".
I also tried using 'development.dialect' but that doesn't help either.
Is it possible that the require('config'); doesn't work?
In my Sequelize's index.js file I've got
const config = require(__dirname + '/../config/config.js'); and that seems to work fine.
This github repository provides a good example of using config package.
https://github.com/basarbk/tdd-nodejs
Notes:
for diffrent configs, you must have a file named whatever you use for NODE_ENV. for example for "start": "cross-env NODE_ENV=production node index" create a file called production.js
check config folder in root directory
if you are using window you should use cross-env.

Nestjs Typeorm query still using 'postgres' database

When I try to query using repository for login api, I receive this error
QueryFailedError: relation "user" does not exist
After debuging, I realize the nestjs still using database with name "postgres". eventhough I set my database name in my .env as "nestjs". (I can confirm because when I migrate and seed the database "postgres" the login function works)
DB_TYPE=postgres
DB_HOST=db
DB_USER=postgres
DB_PASS=postgres
DB_NAME=nestjs
DB_NAME_TEST=nestjs_test
DB_PORT=5432
All my migration and seed process already using database "nestjs". Below is my files to configure my database connection
configuration.ts
import { registerAs } from '#nestjs/config';
export default registerAs('database', () => ({
type: process.env.DB_TYPE,
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
name: process.env.DB_NAME,
nameTest: process.env.DB_NAME_TEST,
port: process.env.DB_PORT,
}));
database/config.service.ts
...
get name(): string {
return this.configService.get<string>(
this.configService.get<string>('app.env') === 'test'
? 'database.nameTest'
: 'database.name',
);
}
...
database/config.migration.ts
import { SnakeNamingStrategy } from 'typeorm-naming-strategies';
export = {
type: process.env.DB_TYPE,
host: process.env.DB_HOST,
port: +process.env.DB_PORT,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database:
process.env.NODE_ENV === 'test'
? process.env.DB_NAME_TEST
: process.env.DB_NAME,
entities: [__dirname + '/../../**/*.entity{.ts,.js}'],
migrations: [__dirname + '/../../database/migrations/*{.ts,.js}'],
cli: {
migrationsDir: __dirname + '/../../database/migrations',
},
extra: {
charset: 'utf8mb4_unicode_ci',
},
synchronize: false,
logging: true,
keepConnectionAlive: true,
namingStrategy: new SnakeNamingStrategy(),
};
database/provider.module.ts
import { Module } from '#nestjs/common';
import { TypeOrmModule, TypeOrmModuleOptions } from '#nestjs/typeorm';
import { SnakeNamingStrategy } from 'typeorm-naming-strategies';
import { DatabaseConfigModule } from '../../config/database/config.module';
import { DatabaseConfigService } from '../../config/database/config.service';
#Module({
imports: [
TypeOrmModule.forRootAsync({
imports: [DatabaseConfigModule],
inject: [DatabaseConfigService],
useFactory: async (
service: DatabaseConfigService,
): Promise<TypeOrmModuleOptions> => ({
type: service.type,
host: service.host,
port: service.port,
username: service.user,
password: service.password,
name: service.name,
entities: [__dirname + '/../../**/*.entity{.ts,.js}'],
migrations: [__dirname + '/../../database/migrations/*{.ts,.js}'],
cli: {
migrationsDir: __dirname + '/../../database/migrations',
},
extra: {
charset: 'utf8mb4_unicode_ci',
},
synchronize: false,
logging: true,
keepConnectionAlive: true,
namingStrategy: new SnakeNamingStrategy(),
}),
}),
],
})
export class DatabaseProviderModule {}
And here is my databases:
If you guys need more file for information please let me know. I'm new to nestjs and typeorm, So please tell me what's wrong with my code. Thank you!
TypeormModule has the database property which provides the database name. You are using name in provider.module.ts:
Wrong name : databaseName
Right database : databaseName
TypeOrmModule.forRootAsync({
useFactory: () => ({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'root',
database: 'test',
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
}),
});

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,
},
},
});

unexpected token 'export' when using typeorm in heroku

I am follwing this thread on how to get TypeORM and PostgreSQL to work in Heroku.
Using ormconfig.js and module.exports I get this error
MissingDriverError: Wrong driver: "undefined" given. Supported drivers are: "cordova", "expo", "mariadb", "mongodb", "mssql", "mysql", "oracle", "postgres", "sqlite", "sqljs", "react-native".
const env = require('dotenv')
env.config()
module.exports = {
name: 'default',
type: process.env.DATABASE_TYPE,
host: process.env.DATABASE_HOST,
port: 5432,
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
synchronize: true,
dropSchema: false,
logging: true,
entities: ['/src/**/*.entity.ts', 'dist/**/*.entity.js'],
extra: {
ssl: true,
},
};
The above link says switch to .ts and use export
so..using ormconfig.ts and export = config I get this error:
2020-03-25T05:07:57.946988+00:00 app[web.1]: export = config
2020-03-25T05:07:57.946988+00:00 app[web.1]: ^^^^^^
2020-03-25T05:07:57.946988+00:00 app[web.1]:
2020-03-25T05:07:57.946989+00:00 app[web.1]: SyntaxError: Unexpected token 'export'
const config = {
name: 'default',
type: process.env.DATABASE_TYPE,
host: process.env.DATABASE_HOST,
port: 5432,
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
synchronize: true,
dropSchema: false,
logging: true,
entities: ['/src/**/*.entity.ts', 'dist/**/*.entity.js'],
extra: {
ssl: true,
},
};
export = config;
I've also tried using export default but had same error as with export...
Not exactly sure where to go from here...anyone had this issues?
does babel need to be included to run my node.js apps in heroku?
Do it this way to get rid of (2020-03-25T05:07:57.946989+00:00 app[web.1]: SyntaxError: Unexpected token 'export') error:
exports.config = {
name: 'default',
type: process.env.DATABASE_TYPE,
host: process.env.DATABASE_HOST,
port: 5432,
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
synchronize: true,
dropSchema: false,
logging: true,
entities: ['/src/**/*.entity.ts', 'dist/**/*.entity.js'],
extra: {
ssl: true,
},
};
ormconfig MUST keep the .js extension.
Also, I am parsing the DATABASE_URL from Heroku and adding all the variables into their appropriate placeholders in ormconfig like so:
const parse = require('pg-connection-string').parse;
const env = require('dotenv')
env.config()
const config = parse(process.env.DATABASE_URL)
const pgConnection = {
type: "postgres",
host: config.host,
port: config.port,
username: config.user,
password: config.password,
database: config.database,
synchronize: true,
dropSchema: false,
logging: true,
entities: ['/src/**/*.entity.ts', 'dist/**/*.entity.js'],
extra: {
ssl: true,
}
}
module.exports = pgConnection;

Resources