This post is more focus on MySQL database with Sequelize.js ORM. Last time, I have already talked about how to deploy node.js app with MySQL database to Heroku. So, I will run through those similar steps. If you need detail instruction, please visit the previous post.
First of all, open your command line application. You will find me referring Command-line application as terminal since I feel more comfortable with this term.
In your terminal, run the following command to setup a Heroku app
# go to your development folder. Here, I am assuming you already have # a git repo with your application code.
$ heroku login
# follow the prompts to login to Heroku
$ heroku create <<your_app_name>>
# if success, Creating <your_app_name>.. done message will be shown
After your have created your app, in your web browser, go to your Heroku Dashboard.
In your Heroku Dashboard, add JAWSDB under resources tab
Open your model/index.js file in your code editor and change your database connection statement to the following.
// DB setup
const db = {};
let sequelize;
if (process.env.JAWSDB_URL) {
// for Heroku
sequelize = new Sequelize(process.env.JAWSDB_URL, {});
} else {
const env = process.env.NODE_ENV || "development";
const config = path.resolve(__dirname, "..", "config", "config.json")[env];
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
Normally, you can just added Heroku’s database credentials to “production” object in config.json file. But I do not like to commit such information to GitHub, Heroku or any Open Environment for security purposes. Hence, normally config.json file is in my .gitignore file and never commit to git at all.
If you have sequelize migration files like I do, modify “production” object your config.json file with JAWSDB settings.
In your terminal,
# Go to your project folder
# run the following commands
$ npx sequelize db:migrate --env=production
$ npx sequelize-cli db:seed:all --env=production
References:
- https://sequelize.readthedocs.io/en/1.7.0/articles/heroku/#running-migrations
- https://medium.com/@ng.eric314/node-js-setting-up-sequelized-for-herokus-jawsdb-while-using-environmental-variables-3f4a0535c0fa