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 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
I accessed it with the getConnection().models method. Thank's for your interest!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.