Connecting the Database (Manual)
Now it's time to get our app talking to our development database. To do this, we will need to put usernames/passwords in our app. To prevent these falling into the wrong hands, we want to avoid committing them to Github.
We also want to make sure that our app connects to the development database when we run it on our computer, the test database when we run the tests, and the live database when we deploy it to the internet.
To do this, we will store our credentials and secrets in our environment. To help with this, we will use a package called dotenv.
Dotenv
dotenv is a package which lets us load environment variables from a file. Environment variables are simply variables that are stored in the terminal of your computer. You can set one with export VARIABLE_NAME=value.
Try it, in your terminal export a variable called NAME, and set the value to your name. You can then print that variable with echo $NAME. After you close the terminal the variable will be forgotten.
We can access these variables in node with the process.env object. For example, you could print that NAME variable in JavaScript with console.log(process.env.NAME).
- 
Install dotenvas adev dependencywithnpm i -D dotenv
- 
We need to configure our startscript to usedotenvto load ourenvironment variables. Change yourstartscript inpackage.jsontonodemon -r dotenv/config index.js
- 
You will now need a file to store your environment variables. Make a new file called .env. Store the following inside it:
- 
We will also need a second database for when we start running tests. Make another file called .env.testand store the same environment variables in it. Change thePGDATABASEtomusic_library_test.
- 
To test the app is loading the environment variables, we are going to run the app on the port that we have stored in the .envfile. To do this, change theAPP_PORTdeclaration to:
- 
Run your app again (you will need to restart it for the environment variables to be loaded). Your app should now be running on port 3000. If you delete that line from your .envand rerun your app, you will see that it goes back to running on 4000.
- 
Finally, make sure .envand.env.testare in your.gitignore, so your credentials don't get commited to Github.
Making The Database Scripts
When starting the application or running the tests, we want to ensure that there is a database available to connect to. Let's add two utility scripts:
- First script - create-database.jswill run before our app or tests start, to make sure the database exists and contains the correct tables.
- The second one - drop-database.jswill run after our tests to tear down the test database. This will ensure our tests have a fresh database each time they run.
Ready? 🚀 Let's start:
- 
We will use the pgmodule to connect to our databases. Install it withnpm i -S pg
- 
Make a new directory in your project folder called scripts.
- 
Make two new files in this directory, create-database.jsanddrop-database.js.
create-database.js
Put the following in your create-database script:
Comments have been added to help you understand what the code is doing. You can remove them from your version if you prefer. Feel free to try out some refactors to make the code a bit more modular.
drop-database.js
The drop database script is a lot less complex. In this script, we simply want to connect to the database and run a DROP DATABASE statement. Can you use the content of create-database.js to write this script?
This script will only run after our tests, so there is no need to load anything other than .env.test.
You can find a solution to this challenge here.
Running the Scripts
Now that the scripts are in place, we want to configure package.json to run them before the app runs and after the tests finish.
We can use the prestart, pretest and posttest hooks to do this. Node will run these commands automatically whenever we run npm start or npm test.
Change the scripts in your package.json to look like this:
We will add the test and posttest commands in the next step.
When you are done, run npm start again. Check your console for errors. If none appear, then check pgAdmin to see if your database was created.
Commit your work to git and move on to the next step.