Follow these steps using pgAdmin.
Create a database called massive_demo
: View demo.
Copy the contents of ./schema.sql, paste it into a script (using pgAdmin), and execute it: View demo.
Clone the repo (do not fork it).
npm install
npm install --save massive
Start your application by running:
node server.js
Start your Postgres server.
Begin by launching pgAdmin.
- Create a database
- Examine the tables
In server.js
, add to your list of dependencies:
var massive = require('massive')
In server.js
add code to connect to your database:
var conn = massive.connectSync({
connectionString : "postgres://massive:password@localhost/your-database-name"
});
Connect your massive instance to a db
key:
app.set('db', conn);
Set a var db
to the value of the key:
var db = app.get('db');
Use console.log
to test that you're properly connected to Postgres. Remove it when you're confident it works.
massive-js works by converting your SQL queries, held in files, into JS functions.
For example, the following file, held in the ./db
directory of your project:
db/get_all_injuries.sql
SELECT * FROM injuries;
Yields the following function:
db.get_all_injuries(function(err, injuries) {
console.log(injuries) // injuries will contain an array of injuries
});
Create the ./db
directory, and add a file, get_all_incidents.sql
(incidents, not injuries).
Now that you have a repository for SQL queries, add a query to your new file that shows you retrieves the following pieces of information for every incident in your database:
incidents.id
incidents.us_state
injuries.name
affected_areas.name
causes.name
Your query will require more than one join in a single statement (whoa!). When you're query is ready, test it in psql:
psql massive_demo < db/get_all_incidents.sql
Now that you have a way to return basic information about incidents of injuries, upgrade the GET endpoint such that an HTTP request can return the information to a client (like Angular) in your response:
Hint:
db.get_all_injuries(function(err, injuries) {
console.log(injuries) // injuries will contain an array of injuries
});
If you've made it this far, great work. Now, upgrade your endpoint again, this time accepting a new query parameter, cause=Sneezing
(e.g. any cause). When cause=Sneezing
is submitted as part of the same GET request, return the results of a different query, db/get_incidents_by_cause.sql
.
Make sure the original query works as before.
Hint:
massive-js accepts arguments as part of your SQL using $1, $2, ...
select * from products
where in_stock = $1 and price < $2;
Your arguments can be submitted as an array as the first argument in the function, before the callback.
db.products_in_stock([true, 1000], function(err, products) {
// products is a results array
});
Upgrade your GET request to accept not only by=cause
, but by=affected_area
, without breaking your previous functionality.
Upgrade the POST request to give yourself the ability to create a new incident. Here's a sample request body for Postman:
{
"us_state": "WV",
"injury_id": 1,
"cause_id": 5
}