Eventonica - Part 6 - Postgres Database

Optional Step -1

Feel free to do this work however you want, but keep in mind that having API tests is going to make this work much easier, because your API responses should not change as a result of this work. If they do, you’ll need to modify your jQuery AJAX calls in your code.

See the API Test Activity (WIP) for more details about API testing.

Step 0

Before doing anything else, create a new folder Eventonica-Part-6 and seed it with a copy of your part 5 code. This will make it easier to have a reference point.

Overview

Previously, your data was stored in memory in Express, so your data would disappear if the application restarted. For production applications, data must be stored in a more permanent way. In this lesson, you will move your EventRecommender data to a Postgres database and connect to that database in your Express APIs.

How to Submit

In addition to the usual steps:

Instructions

  1. Ensure that you have Postgres installed on your machine and that you can use either PGAdmin or psql - see instructions here.

  2. Create a new database named eventonica.

  3. In your eventonica database, create a table named users that contains the same fields as your User class in eventRecommender.js.

  4. Create a table named events that contains the same fields as your Event class in eventRecommender.js. Create the id column like you did for the users table.

  5. Install pg-promise in your project folder - this module connects your Express application to a Postgres database.

  6. Copy the setup instructions for pg-promise in your index.js file. Your connection string is probably something like postgres://localhost:5432/eventonica. You should not need a username or password if you setup posgres correctly.

  7. Update your EventRecomender methods (addEvent,etc) to use SQL commands.

    Ex: Adding a user

  8. Test that your new APIs work using Postman and your webpage. Using PGAdmin or psql, check that the database contains the information you would expect.

  9. Restart your Express application - your data from previous sessions should still be there! Your database is independent of your application and continues to store the data even when the application is not running.

Additional Requirements After the Basics are Working

  1. Create a user_events table in your database with two columns: user_id and event_id. Use this table to store which events have been saved for each user, replacing whichever method you used before. When creating the table,

  2. (Only if you created the user_events table): Now, when displaying users and their events on the webpage, can you use SQL joins to get a list of event names that each user has saved?

Troubleshooting

If you are getting HTTP 304 back from your GET requests, it means that the contents of the JSON is identical to when the browser fetched it before. If you’re seeing this and you believe the data should be different, i.e. you’ve added or deleted data in the database, it may be a timing issue. Make sure you are waiting for the database calls to resolve their promises before sending back your Express response.

Challenge

Reviewing Instructions

TL;DR - they are taking their in-memory backend data objects from Part 5 and using Postgres to store them!

Common Issues