Run npm install
to install the pre-requisites
Look over the index.js file to get familiar with your starting point.
Make sure postgres is running on your computer.
Run npm install --save massive
Require massive at the top of your index file.
var massive = require('massive');
Make a new database in postgres called sandbox
Add a new table to that database ( pgAdmin tutorial on how to do both of these ) :
CREATE TABLE airplanes (
planeid SERIAL PRIMARY KEY NOT NULL, -- The primary key
planetype varchar(40) NOT NULL, -- The IP of the host
passengercount integer NOT NULL -- The name of the host
);
We need to get a copy of massive ot use. We need : connectionString > connection > db
Add a connection string under your call to require massive. Change the part that says jeremyrobertson to have your user name. (If you included password it will look like username:password@localhost/sandbox
connectionString
var massive = require('massive');
var connectionString = "postgres://jeremyrobertson@localhost/sandbox";
Use our connection string to get a copy/instance of massive to use. Then add it to our app as a variable called db.
connection
var massiveInstance = massive.connectSync({connectionString : connectionString})
app.set('db', massiveInstance);
Next we can get our db back out of the app
db
var db = app.get('db');
Now that we have our db we can use it to add a new plane:
db.new_plane(function(err, planes){
console.log(err, "plane added")
});
This works by looking in the /db
folder in our app fore a file called new_plane.sql
We've added a plane, comment those 3 lines of code out so we don't add duplicates.
Do the same thing to get all planes using the get_planes file
db.get_planes(function(err, planes){
console.log(err, planes)
})
To use our db in different files we need to do 2 things:
- export the app made by express
- import it and use it in our other file
- call our controller
export our app
//Replace
var app = express();
//With
var app = module.exports = express();
This makes it so index.js exports our app.
__ import it and use it in another file__
Create a controller.js and put this code inside.
var app = require('./index');
module.exports = {
getPlanes: function(){
var db = app.get('db');
db.get_planes(function(err, planes){
console.log(err, planes);
})
}
}
call our controller
Comment out call calls to db in your index.js and replace them with a call to the controller and the get planes function.
var controller = require('./controller.js');
controller.getPlanes();
In get_planes.sql uncomment out where passengercount > $1
by removing the two --
in front of it.
The $1 acts as a place holder for the 'first' parameter passed in.
To pass that in change the query in controller.js to take parameters before the function.
db.get_planes([25], function(err, planes){
console.log(err, planes);
})
We are now getting all planes with a passenger count greater than 25.
© DevMountain LLC, 2016. Unauthorized use and/or duplication of this material without express and written permission from DevMountain, LLC is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to DevMountain with appropriate and specific direction to the original content.