Change over to PostgreSQL; I think it's all working, still need to test POST/INSERT.

This commit is contained in:
jkaplon 2015-12-09 22:48:31 -05:00
parent 610dfd9cd6
commit 11b4c05830
2 changed files with 32 additions and 45 deletions

View File

@ -8,6 +8,7 @@
"handlebars-form-helpers": "^0.1.3", "handlebars-form-helpers": "^0.1.3",
"moment-timezone": "^0.4.0", "moment-timezone": "^0.4.0",
"nodemailer": "^1.3.4", "nodemailer": "^1.3.4",
"pg": "^4.4.3",
"sqlite3": "^3.0.8", "sqlite3": "^3.0.8",
"winston": "^2.1.1" "winston": "^2.1.1"
}, },

View File

@ -34,25 +34,8 @@ if(!exists) {
var sqlite3 = require("sqlite3").verbose(); var sqlite3 = require("sqlite3").verbose();
var db = new sqlite3.Database(file); var db = new sqlite3.Database(file);
var pg = require("pg");
db.serialize(function() { var conString = "postgres://courtsopen:courtsopen@db/courtsopen";
if(!exists) {
// Can't create multiple tables w/one statement, semicolons not allowed.
db.run(
"CREATE TABLE Alerts (" +
"origJSON TEXT," +
"coreId TEXT," +
"status TEXT," +
"published_at TEXT)"
);
db.run(
"CREATE TABLE Cores (" +
"coreId TEXT PRIMARY KEY," +
"coreName TEXT," +
"locationDesc TEXT)"
);
}
});
app.use(bodyParser.json()); // Needed for JSON POST requests from Particle Cores. app.use(bodyParser.json()); // Needed for JSON POST requests from Particle Cores.
app.use(bodyParser.urlencoded({ extended: false })); // Needed for web POST requests from edit form. app.use(bodyParser.urlencoded({ extended: false })); // Needed for web POST requests from edit form.
@ -68,20 +51,24 @@ app.get('/', function(req, res){
var d = new Date(); var d = new Date();
console.log("GET /, " + JSON.stringify(d, 4)); console.log("GET /, " + JSON.stringify(d, 4));
winston.info("GET /"); winston.info("GET /");
var devIndexQry = pg.connect(conString, function(err, client, done) {
if(err) {
return console.error('error fetching client from pool', err);
}
var devIndexQry =
"select status, published_at " + "select status, published_at " +
"from Alerts " + "from Alerts " +
"where status in ('Open', 'Closed') " + "where status in ('Open', 'Closed') " +
"order by published_at desc " + "order by published_at desc " +
"limit 2" "limit 2"
db.all(devIndexQry, function(err, rows){ client.query(devIndexQry, function(err, result) {
if(err !== null) { //call `done()` to release the client back to the pool
console.log(err); done();
} else { if(err) {
//console.log(rows); return console.error('error running query', err);
}
// Loop over elements in rows array, convert ugly UTC times to pretty local times. // Loop over elements in rows array, convert ugly UTC times to pretty local times.
rows.forEach(function(row){ result.rows.forEach(function(row){
row.pubDate = courtsopenUtils.getLocDateFromUTC(row.published_at); row.pubDate = courtsopenUtils.getLocDateFromUTC(row.published_at);
row.pubTime = courtsopenUtils.getLocTimeFromUTC(row.published_at); row.pubTime = courtsopenUtils.getLocTimeFromUTC(row.published_at);
if(row.status.toLowerCase().indexOf('closed') > -1){ if(row.status.toLowerCase().indexOf('closed') > -1){
@ -89,14 +76,14 @@ app.get('/', function(req, res){
} else { row.statusClass = 'open'; } } else { row.statusClass = 'open'; }
}); });
res.render('index', {values: rows}, function(err, html) { res.render('index', {values: result.rows}, function(err, html) {
if(err !== null) { if(err !== null) {
console.log(err); console.log(err);
} else { } else {
res.send(html); res.send(html);
} }
}); });
} });
}); });
}); });
@ -113,14 +100,13 @@ app.post('/', function(req, res){
var status = JSON.stringify(innerDataJSON.status, null, 4).slice(1,-1); var status = JSON.stringify(innerDataJSON.status, null, 4).slice(1,-1);
var coreid = JSON.stringify(req.body.coreid, null, 4).slice(1,-1); var coreid = JSON.stringify(req.body.coreid, null, 4).slice(1,-1);
var pubAt = JSON.stringify(req.body.published_at, null, 4).slice(1,-1); var pubAt = JSON.stringify(req.body.published_at, null, 4).slice(1,-1);
var stmt = db.prepare("INSERT INTO Alerts (OrigJSON, coreid, published_at, status) VALUES (?, ?, ?, ?)"); pg.connect(conString, function(err, client, done) {
stmt.run( if(err) { return console.error('error fetching client from pool', err); }
JSON.stringify(req.body, null, 4), client.query(
coreid, "INSERT INTO alerts (origjson, coreid, published_at, status) VALUES ($1, $2, $3, $4);",
pubAt, [JSON.stringify(req.body, null, 4), coreid, pubAt, status]
status );
); });
stmt.finalize();
} else { // parse for minimal data from cell modem. } else { // parse for minimal data from cell modem.
var deviceid = JSON.stringify(req.body.data, null, 4).slice(1,4); var deviceid = JSON.stringify(req.body.data, null, 4).slice(1,4);
var statusCode = JSON.stringify(req.body.data, null, 4).slice(5,7); var statusCode = JSON.stringify(req.body.data, null, 4).slice(5,7);
@ -135,14 +121,14 @@ app.post('/', function(req, res){
var received_at = new Date(Date.now()); var received_at = new Date(Date.now());
received_at = received_at.toISOString(); received_at = received_at.toISOString();
console.log(statusFromCode + ' ' + received_at); console.log(statusFromCode + ' ' + received_at);
var stmtCell = db.prepare("INSERT INTO Alerts (OrigJSON, coreid, published_at, status) VALUES (?, ?, ?, ?)"); pg.connect(conString, function(err, client, done) {
stmtCell.run( if(err) { return console.error('error fetching client from pool', err); }
JSON.stringify(req.body, null, 4), client.query(
deviceid, "INSERT INTO alerts (origjson, coreid, published_at, status) VALUES ($1, $2, $3, $4);",
received_at, [JSON.stringify(req.body, null, 4), deviceid, received_at, statusFromCode]
statusFromCode );
); });
stmtCell.finalize();
} }
res.status(204).send('POST received'); res.status(204).send('POST received');
}); });