Quick review session for 1702 FSA/GH students for Express
and Sequelize
and Sequelize
association methods
To view the express only version of this repo, check out this branch
To view the express-sequelize version of this repo, check out this branch
Playlist for the video review of this repo being built can be found here
Sequelize Part 1 - basic structure Part 2 - associations
NB: This was all live review, there may be some small inaccuracies in how things are described but I tried to repeat most of the questions for viewers
To start up:
createdb puppies
in the command line to create your postgres databasenpm install
to install node modulesnpm run seed
to seed your DB with the latest and greatest puppies/parks/locations/etc (this will clear/reset the db)npm run seed:associations
to seed some semi-randomized associations in your puppy instances- or
npm run seed:all
to seed the instances and also the associationsnpm start
starts the server, you can now make requests tolocalhost:3000
The server will start listening on localhost:3000
by default
Fire up an easy http client like Postman and try some routes like:
GET: /puppies
POST: /puppies
GET: /puppies/:id
Check out all the routers in /routes
for more options!
Dealing with queries and how to route them?
Dealing with multiple :params in the URI? How to make sure the right route?
What is static middleware???
Express is basically a big nested queue. Express goes through middleware and attempts to match the request path to middleware. If it matches an app.use
sub-router on the way, it enters that sub-router and attemps to match against its
try {
// JS array notation is just shown here as a demo of the queue structure - express iterates through this and tries to match
[
//matches this route
app.use,
//matches this route
app.use,
//if the path matches this /path, ener the router within which is another queue
app.use('/path' [app.get, app.post]),
// if the path matches this /path, enter
app.use('/path2'),
// match all paths and handle errors callback with 4 arguments
app.use('*', function(4 arguments),
// defaults to sending a 404 if no routes match
app.use('*', function(req, res, next){ res.sendStatus(404)})
]
// if errors are caught, express helps handle
} catch(error) {
res.status(500).send(error) // internal server error
}
Cool article on Netflix using express servers and running into erro http://techblog.netflix.com/2014/11/nodejs-in-flames.html
Questions:
-
Remind us which aspects are coming from Sequelize vs Express - where's the logic living? Where do I look in the docs - express or sequelize?
- General answer: anything dealing with
req
orres
is in express docs. Anything in a Sequelize promise chain that's not touching eitherreq
orres
- look to the Sequelize Docs
- General answer: anything dealing with
-
Sequelize methods - what do we get and how do we use them?
Model.findAll()
finds all instancesModel.findAll({where: {.......}})
finds all instances that match the where conditionModel.findOne({where: {....}})
finds first match for the where conditionModel.findById(id)
finds by an id number. Sequelize will coerce strings into ints, so you can simply use thereq.params.id
and not worry about casting to aNumber
before sending to SequelizeModel.findOrCreate({where: {.....}})
will find an entry OR create it if necessary. returnsInstance
as first argument, andcreated
boolean as the second. Good for making sure you don't make a bunch of duplicates- if the above queries return nothing, return value will be
null
For this segment of the review, we significantly changed the starting point from the previous ending point in order to save time during the live review. 3 new models were defined: Park
, Location
, and Food
, with corresponding basic routers. The file structure was expanded so that there are separate /routes
and /models
folders. Seed data was also added in the seed.js
and seedAssociations.js
This document explains more about the Sequelize
methods and a bit more about what we aimed to do with this review session
You'll notice some new seeding files in this branch - this is to illustrate a bit about how to construct seed files, with or without associations