Last active
November 18, 2016 03:56
-
-
Save MiladNazeri/b55ecbc4eaa5a8f7f75fc53da58ad76d to your computer and use it in GitHub Desktop.
Going over some basic Sequelize/Express stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//npm i body-parser express pg pg-hstore sequelize | |
//postgres - RDBMS - our database server for handeling SQL over TCP/IP | |
//PG - our connection between postgress and PG | |
//Sequelize - our ORM that behind the scenes uses pg but gives us the power of javascript objects to use our database with properties and methods | |
const express = require('express'); | |
//Create instance of express | |
const app = express(); | |
//To help us turn the body from a request into a JS object | |
const bodyParser = require('body-parser'); | |
const sequelize = require('sequelize'); | |
const Port = 8080; | |
//db is our connection to the postgres server and it's database | |
const db = new sequelize('postgres://localhost:5432/miladsDB'); | |
//db.define is how we create a new table Model. The first argument is it's name, the second is definining it's properties, and the third is an object that defines methods we can use on the model or instance | |
const Student = db.define('student', { | |
name: { | |
type: sequelize.TEXT, | |
allowNull: false | |
}, | |
age: { | |
type: sequelize.INTEGER, | |
allowNull: false | |
}, | |
city: { | |
type: sequelize.STRING, | |
allowNull: false | |
} | |
}, | |
{ | |
hooks: { | |
beforeValidate: function (student) { | |
//runs before validation so that if there isn't this property set when we go to .create(), it will make it for us. | |
if (!student.age) { | |
student.age = 25; | |
} | |
} | |
}, | |
getterMethods: { | |
awesomeAge: function () { | |
//this is used like let awesomeAge = student.awesomeAge. this referes to the instance | |
return `Awesome you are ${this.age}` | |
} | |
}, | |
classMethods: { | |
//Methods on the Model itself. Student.findByCity('Dallas'); | |
findByCity: function (city) { | |
return Student.findAll({ | |
where: { | |
city: city | |
} | |
}) | |
} | |
}, | |
instanceMethods: { | |
findSimilar: function () { | |
//Methods on an instance of a model. student.findSimilar() | |
return Student.findAll({ | |
where: { | |
age: { | |
age: this.age | |
}, | |
id: { | |
$ne: this.id | |
} | |
} | |
}) | |
} | |
} | |
}) | |
//Setting up our middleware for express to parse the body | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({extended: false})); | |
//Route to find all the Students. Array returned even if it's just one item. | |
app.get('/students', (req,res,next)=>{ | |
Student.findAll({}) | |
.then((students) => { | |
res.send(students) | |
}) | |
.catch(next); | |
}) | |
//route to find just one instance of student. | |
app.get('/students/:id', (req,res,next)=>{ | |
let id = req.params.id; | |
Student.findById(id) | |
.then((student) => { | |
res.send(student); | |
}) | |
}) | |
//Syncing our model to make sure this table exists on the database | |
// | |
Student.sync({force: true}) | |
.then(()=>{ | |
app.listen(Port, ()=>{ | |
console.log(`Listening on ${Port}`) | |
}); | |
}) | |
.catch(console.error); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment