Skip to content

Instantly share code, notes, and snippets.

@warrenlalata
Last active June 3, 2019 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save warrenlalata/d11b2f76f9ef8b3ce12fd291bf2d3534 to your computer and use it in GitHub Desktop.
Save warrenlalata/d11b2f76f9ef8b3ce12fd291bf2d3534 to your computer and use it in GitHub Desktop.
Steps to configure Sequelize ORM to work with Node Backend
# Development
DEV_DB_USERNAME=root
DEV_DB_PASSWORD=yourdbpassword
DEV_DB_NAME=apollo_development
DEV_DB_HOSTNAME=localhost
# Test
TEST_DB_USERNAME=root
TEST_DB_PASSWORD=yourdbpassword
TEST_DB_NAME=apollo_development
TEST_DB_HOSTNAME=localhost
# Prod
PROD_DB_USERNAME=root
PROD_DB_PASSWORD=yourdbpassword
PROD_DB_NAME=apollo_production
PROD_DB_HOSTNAME=localhost
// This file overrides the default path to migrations, models, seeders or config folder.
const path = require('path');
module.exports = {
'config': path.resolve('config', 'config.js'),
'models-path': path.resolve('db', 'models'),
'seeders-path': path.resolve('db', 'seeders'),
'migrations-path': path.resolve('db', 'migrations')
}
require('dotenv').config()
module.exports = {
development: {
username: process.env.DEV_DB_USERNAME,
password: process.env.DEV_DB_PASSWORD,
database: process.env.DEV_DB_NAME,
host: process.env.DEV_DB_HOSTNAME,
dialect: 'postgres'
},
test: {
username: process.env.TEST_DB_USERNAME,
password: process.env.TEST_DB_PASSWORD,
database: process.env.TEST_DB_NAME,
host: process.env.TEST_DB_HOSTNAME,
dialect: 'postgres'
},
production: {
username: process.env.PROD_DB_USERNAME,
password: process.env.PROD_DB_PASSWORD,
database: process.env.PROD_DB_NAME,
host: process.env.PROD_DB_HOSTNAME,
dialect: 'postgres'
}
};
@warrenlalata
Copy link
Author

warrenlalata commented May 2, 2019

  1. Go to the root directory of the project and create .sequelizerc file and paste the ff. Run yarn sequelize init after.
  2. Add the dotenv package. yarn add dotenv.
  3. Create a .env file in the root directory and paste the ff code from config.js
  4. Paste the ff code from .env.sample into your .env and change the values.
  5. Install sequelize-cli - http://docs.sequelizejs.com/manual/migrations.html
  6. Create the database.
    To create the database, run:
    $ yarn sequelize db:create
    To drop the database, run:
    $ yarn sequelize db:create

NOTE:
When using production databse in Heroku, add the code below after dialect property on the config js. See: https://stackoverflow.com/questions/27687546/cant-connect-to-heroku-postgresql-database-from-local-node-app-with-sequelize
dialectOptions: {
ssl: true
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment