Last active
November 21, 2022 01:37
-
-
Save leaena/8174127 to your computer and use it in GitHub Desktop.
Simple database node module (Node/Express/Sequelize/PostgreSQL)
This file contains hidden or 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 dependencies. | |
*/ | |
var express = require('express'); | |
var routes = require('./routes'); | |
var database = require('./controllers/database'); | |
var http = require('http'); | |
var path = require('path'); | |
var cors = require('cors'); | |
var app = express(); | |
// all environments | |
app.set('port', process.env.PORT || 3000); | |
app.set('views', __dirname + '/views'); | |
app.set('view engine', 'jade'); | |
app.use(express.favicon()); | |
app.use(express.logger('dev')); | |
app.use(express.bodyParser()); | |
app.use(cors()); | |
app.use(express.methodOverride()); | |
app.use(app.router); | |
app.use(require('less-middleware')({ src: __dirname + '/public' })); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
// development only | |
if ('development' == app.get('env')) { | |
app.use(express.errorHandler()); | |
} | |
app.get('/', routes.index); | |
app.post('/votes', database.createVote); | |
app.get('/votes/:vidID', database.getVotes); | |
http.createServer(app).listen(app.get('port'), function(){ | |
console.log('Express server listening on port ' + app.get('port')); | |
}); |
This file contains hidden or 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
var Sequelize = require('sequelize'); | |
var pg = require('pg').native; | |
var config = require('../db_config'); | |
var sequelize = new Sequelize(config.database, config.username, config.password, { | |
host: config.host, | |
port: config.port, | |
dialect: config.dialect, | |
native: config.native | |
}); | |
var voteTable = sequelize.define('votes', { | |
video_id: Sequelize.STRING, | |
timestamp: Sequelize.INTEGER, | |
vote: Sequelize.INTEGER | |
}); | |
voteTable.sync(); | |
var sendResponse = function(res, query){ | |
voteTable.findAll(query, { raw: true }).success(function(votes){ | |
res.send(votes); | |
}); | |
}; | |
var newVote = function(json, res){ | |
voteTable.create(json).success(function() { | |
sendResponse(res, {}); | |
}); | |
}; | |
module.exports.createVote = function(req, res){ | |
var voteCreate = { | |
video_id: req.body.video_id, | |
timestamp: Math.floor(req.body.timestamp), | |
vote: req.body.vote | |
}; | |
newVote(voteCreate, res); | |
}; | |
module.exports.getVotes = function(req, res){ | |
var query = {where: {'video_id': req.params.vidID}, attributes: ['timestamp', 'vote']}; | |
sendResponse(res, query); | |
}; |
This file contains hidden or 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 = { | |
database: 'database_name', | |
username: 'username', | |
password: 'secret_password', | |
host: 'host)url', | |
port: 5432, | |
dialect: 'postgres', | |
native: true | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment