Node Three

Heroku, Dotenv, and Massive

Heroku Set-up

Heroku is a platform for hosting databases, and we will use it to host our postgreSQL databases. Use the link below and create an account:

SQL Tabs

SQL Tabs is an application for interacting with your hosted database. Download it here:

Dotenv

When dealing with sensitive information, such as API Keys and Database URL's, you want to ensure that information stays private. Dotenv is a package that allows you to create environmental variables that allow you to keep your data safe. To install it, run the following command:

npm install dotenv

Dotenv Setup

Once installed, create a '.env' file at the root of your project:

As soon as it is created, put the .env file in your .gitignore file to avoid pushing it to GitHub.

Dotenv Setup

In your '.env' file, you can store environmental variables:

SERVER_PORT=4040
CONNECTION_STRING=database_url_here + ?ssl=true

Note: Do NOT put semicolons at the end of these variables. ?ssl=true adds extra protection to your database URL.

Dotenv Setup

Once environmental variables have been created, require dotenv at the top of your main server file like so:

require('dotenv').config()

Then you can destructure values off of the 'process.env' object that dotenv creates for you.

const {SERVER_PORT, CONNECTION_STRING} = process.env;

Massive

Massive is a package that allows you to interact with your hosted database. To install it, run the following command:

npm install massive

Once installed, require it to your main server file

const massive = require('massive');

Massive

Once massive is installed and required, it can then be invoked to create a constant connection with your database:

massive(CONNECTION_STRING).then((dbInstance) => {
    app.set('db', dbInstance);
});

DB Folders

Once a connection to your database has been established, you can begin to create SQL queries in your project. First, create a 'db' folder at the root of your project:

DB Folders

Inside of the 'db' folder, you will want to include a seed file. The seed file should contain the create statements for your database schema, should you need them for any reason. Other SQL queries can also be added and included in this folder.

Node Three

By Matthew Bodily

Node Three

  • 253