0

I am writing a simple CMS with Node.js and Sequelize.js. I want to get database information from user like Wordpress and create database connection. I wrote the codes below for this but my problem is application don't start before database connection.

Controllers

// Get Database Setup Page
exports.getSetup = (req, res) => {
  return res.render("pages/account/setup");
};

// Post Database Information
exports.postSetup = (req, res) => {

  // Set posted values as ENV Variable
  setEnvValue("DB_NAME", req.body.name);
  setEnvValue("DB_USERNAME", req.body.username);
  setEnvValue("DB_PASSWORD", req.body.password);
  setEnvValue("DB_HOST", req.body.host);

  return res.redirect("/login");
};

// Error occurs here. Application don't start before database connection.
exports.getLogin = async (req, res) => {
  const general = await Models.General.findByPk(1);

  // Check Authenticate
  if (req.session.isAuth) return res.redirect("/admin");

  // Render Page
  return res.render("pages/account/login", {
    title: "Login",
    general: general,
  });
};

.env File After Post

DB_NAME="db-name"
DB_USERNAME="root"
DB_PASSWORD="password"
DB_HOST="localhost"

Database Connection file

I use this in several places like defining models.

const Sequelize = require("sequelize");

const sequelize = new Sequelize(
  process.env.DB_NAME,
  process.env.DB_USERNAME,
  process.env.DB_PASSWORD,
  {
    dialect: "mysql",
    host: process.env.DB_HOST,
  }
);

module.exports = sequelize;

1 Answers1

1

You can just wrap creating Sequelize instance into a function and export it to use in login route like this:

const Sequelize = require("sequelize");

let sequelize = null;

function initializeConnection() {
  sequelize = new Sequelize(
    process.env.DB_NAME,
    process.env.DB_USERNAME,
    process.env.DB_PASSWORD,
    {
      dialect: "MySQL",
      host: process.env.DB_HOST,
    }
  );
  // here should be model and their associations' registration
  ...
}

function getConnection() {
  return sequelize;
}

module.exports = { 
  initializeConnection,
  getConnection
}
// Error Occurs here. Application don't start before database connection.
exports.getLogin = async (req, res) => {
  if (req.session.isAuth) {
    return res.redirect("/admin");
  }

  try {
    await intializeConnection();
  catch (err) {
      res.status(500).send('Internal server error')
      return;
  }

  const general = await Models.General.findByPk(1)
  res.render("pages/account/login", {
      title: "Login",
      general: general,
    });
};

Don't forget to disconnect from DB on logout

Anatoly
  • 20,799
  • 3
  • 28
  • 42
  • Thank's a lot your suggestion. I defined Models at external file like `const General = sequelize.define... module.exports = {Admin, General etc.} `. I don't know how to register their associations in the function and how to access them. Can you explain it for me. – Enes Kılıç Jan 20 '22 at 23:18
  • See mu other answer https://stackoverflow.com/a/61710568/1376618 – Anatoly Jan 21 '22 at 15:29
  • I accessed it with the getConnection().models method. Thank's for your interest! – Enes Kılıç Jan 21 '22 at 15:37