0
votes

The documentation on how to use Sequelize is not pretty straightforward especially for new beginners who want to use config.js instead of config.json. I have tried different examples on stackoverflow but did not work for me for some reasons. However, I am putting this here so that I could find someone to assist

I am building an API Application using NodeJs & Sequelize and I have done the following

package.json file

"dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "cross-env": "^7.0.3",
    "deep-email-validator": "^0.1.18",
    "dotenv": "^10.0.0",
    "express": "^4.17.1",
    "express-fileupload": "^1.2.1",
    "express-handlebars": "^5.2.0",
    "formidable": "^1.2.2",
    "g": "^2.0.1",
    "handlebars": "^4.7.6",
    "jsonwebtoken": "^8.5.1",
    "lodash": "^4.17.21",
    "mysql2": "^2.2.5",
    "nodemailer": "^6.5.0",
    "sequelize": "^6.6.2",
    "sequelize-cli": "^6.2.0",
    "swagger-jsdoc": "^6.1.0",
    "swagger-ui-express": "^4.1.6",
    "sweetalert": "^2.1.2",
    "sweetalert2": "^10.14.0",
    "unique-string": "^2.0.0"
  },

.env file

ACCESS_PORT= 5000
ACCESS_TOKEN_SECRET=
ACCESS_REFRESH_TOKEN=
DB_USER=root
DB_PASSWORD=
DB_HOST=127.0.0.1
DB_PORT=3306
DB_NAME=db_name
DB_NAME_TEST=db_name
DB_DIALECT=mysql

config.js (resides inside config folder)

require('dotenv').config()

const { DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, DB_PORT, DB_NAME_TEST, DB_DIALECT } = process.env;

module.exports = {
    development: {
        database: DB_NAME,
        username: DB_USER,
        password: DB_PASSWORD,
        port: DB_PORT,
        options: {
            host: DB_HOST,
            dialect: DB_DIALECT
        },
    },
    test: {
        database: DB_NAME_TEST,
        username: DB_USER,
        password: DB_PASSWORD,
        host: DB_HOST,
        port: DB_PORT,
        dialect: DB_DIALECT
    },
    production: {
        database: DB_NAME,
        username: DB_USER,
        password: DB_PASSWORD,
        host: DB_HOST,
        port: DB_PORT,
        dialect: DB_DIALECT
    }
}

index.js (This file resides inside the 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('./config/config')[env];
const db = {};

let sequelize;

// if (config.use_env_variable) {
//   sequelize = new Sequelize(process.env[config.use_env_variable], config);
// } else {
//   sequelize = new Sequelize(config.database, config.username, config.password, config);
// }
sequelize = new Sequelize(config.database, config.username, config.password, config.options);

fs
  .readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
    db[model.name] = model;
  });

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

At the point of running npx sequelize-cli db:migrate, I get the error below

ERROR: Dialect needs to be explicitly supplied as of v4.0.0

Any help on how to solve this would be appreciated

1
I guess the work around worked for me. So instead of running npx sequelize-cli db:migrate, I ran npx sequelize-cli db:migrate --url 'mysql://username:@localhost/db_name' instead and it worked. - Oyedele Femi

1 Answers

0
votes

With the development environment configuration you have your dialect and host under options. Try flattening it like the other two.