Last active
August 29, 2015 14:16
-
-
Save rahul1346/cb8e93796c8af9ebd77e to your computer and use it in GitHub Desktop.
express/sequelize
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
'use strict'; | |
var Sequelize = require('sequelize'); | |
module.exports = function (sequelize, DataTypes) { | |
var Employer = sequelize.define("Employer", { | |
username: DataTypes.STRING, | |
password: DataTypes.STRING | |
}); | |
return { | |
Employer: Employer | |
}; | |
}; |
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
module.exports = function (DataTypes) { | |
var model = require("../models/employer_model.js")(DataTypes); | |
var Employer = model.Employer; | |
return { | |
create: function (req, res) { | |
var newEmployer = { | |
username: req.body.username, | |
password: req.body.password | |
} | |
Employer.create(newEmployer).success(function () { | |
res.send(200); | |
res.json(req.dataValues); | |
}); | |
}, | |
get: function (req, res) { | |
Employer.findAll().success(function (employers) { | |
res.send(employers); | |
res.json(employers); | |
}); | |
} | |
}; | |
}; |
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
"use strict"; | |
var fs = require("fs"); | |
var path = require("path"); | |
var Sequelize = require("sequelize"); | |
var env = process.env.NODE_ENV || "development"; | |
var config = require(__dirname + '/../config/config.json')[env]; | |
var sequelize = new Sequelize(config.database, config.username, config.password, config); | |
var db = {}; | |
fs.readdirSync(__dirname).filter(function(file) { | |
return (file.indexOf(".") !== 0) && (file !== "index.js"); | |
}).forEach(function(file) { | |
var model = sequelize["import"](path.join(__dirname, file)); | |
db[model.name] = model; | |
}); | |
Object.keys(db).forEach(function(modelName) { | |
if ("associate" in db[modelName]) { | |
db[modelName].associate(db); | |
} | |
}); | |
db.sequelize = sequelize; | |
db.Sequelize = Sequelize; | |
module.exports = db; |
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
// app dependencies | |
var express = require("express"); | |
var Sequelize = require("sequelize"); | |
var bodyParser = require('body-parser'); | |
var morgan = require('morgan'); | |
var app = express(); | |
var models = require("./models"); | |
//sequalize initialization | |
var sequelize = new Sequelize("postgres://rahul:jobletics@localhost:5432/jobletics"); | |
var employerRoute = require("./routes/employer_route")(sequelize); | |
//middleware | |
app.use(bodyParser()); | |
app.use(morgan('dev')); | |
//sync the model with the database | |
models.sequelize.sync().then(function() { | |
var server = app.listen(app.get('port'), function() { | |
console.log('Express server listening on port ' + server.address().port); | |
}); | |
}); | |
sequelize.sync().success(function (err) { | |
app.get("/employer", employerRoute.get); | |
app.post("/employer", employerRoute.create); | |
app.listen(3000); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment