From 11b4c05830c7b1457c0319871ad0a041283033b6 Mon Sep 17 00:00:00 2001 From: jkaplon Date: Wed, 9 Dec 2015 22:48:31 -0500 Subject: [PATCH] Change over to PostgreSQL; I think it's all working, still need to test POST/INSERT. --- package.json | 1 + server.js | 76 +++++++++++++++++++++------------------------------- 2 files changed, 32 insertions(+), 45 deletions(-) diff --git a/package.json b/package.json index e0e22d8..67c017b 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "handlebars-form-helpers": "^0.1.3", "moment-timezone": "^0.4.0", "nodemailer": "^1.3.4", + "pg": "^4.4.3", "sqlite3": "^3.0.8", "winston": "^2.1.1" }, diff --git a/server.js b/server.js index 6a31379..6979fe5 100644 --- a/server.js +++ b/server.js @@ -34,25 +34,8 @@ if(!exists) { var sqlite3 = require("sqlite3").verbose(); var db = new sqlite3.Database(file); - -db.serialize(function() { - 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)" - ); - } -}); +var pg = require("pg"); +var conString = "postgres://courtsopen:courtsopen@db/courtsopen"; 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. @@ -68,20 +51,24 @@ app.get('/', function(req, res){ var d = new Date(); console.log("GET /, " + JSON.stringify(d, 4)); 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 " + "from Alerts " + "where status in ('Open', 'Closed') " + "order by published_at desc " + "limit 2" - db.all(devIndexQry, function(err, rows){ - if(err !== null) { - console.log(err); - } else { - //console.log(rows); - + client.query(devIndexQry, function(err, result) { + //call `done()` to release the client back to the pool + done(); + if(err) { + return console.error('error running query', err); + } // 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.pubTime = courtsopenUtils.getLocTimeFromUTC(row.published_at); if(row.status.toLowerCase().indexOf('closed') > -1){ @@ -89,14 +76,14 @@ app.get('/', function(req, res){ } else { row.statusClass = 'open'; } }); - res.render('index', {values: rows}, function(err, html) { + res.render('index', {values: result.rows}, function(err, html) { if(err !== null) { console.log(err); } else { res.send(html); } }); - } + }); }); }); @@ -113,14 +100,13 @@ app.post('/', function(req, res){ var status = JSON.stringify(innerDataJSON.status, 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 stmt = db.prepare("INSERT INTO Alerts (OrigJSON, coreid, published_at, status) VALUES (?, ?, ?, ?)"); - stmt.run( - JSON.stringify(req.body, null, 4), - coreid, - pubAt, - status - ); - stmt.finalize(); + pg.connect(conString, function(err, client, done) { + if(err) { return console.error('error fetching client from pool', err); } + client.query( + "INSERT INTO alerts (origjson, coreid, published_at, status) VALUES ($1, $2, $3, $4);", + [JSON.stringify(req.body, null, 4), coreid, pubAt, status] + ); + }); } else { // parse for minimal data from cell modem. var deviceid = JSON.stringify(req.body.data, null, 4).slice(1,4); 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()); received_at = received_at.toISOString(); console.log(statusFromCode + ' ' + received_at); - var stmtCell = db.prepare("INSERT INTO Alerts (OrigJSON, coreid, published_at, status) VALUES (?, ?, ?, ?)"); - stmtCell.run( - JSON.stringify(req.body, null, 4), - deviceid, - received_at, - statusFromCode - ); - stmtCell.finalize(); + pg.connect(conString, function(err, client, done) { + if(err) { return console.error('error fetching client from pool', err); } + client.query( + "INSERT INTO alerts (origjson, coreid, published_at, status) VALUES ($1, $2, $3, $4);", + [JSON.stringify(req.body, null, 4), deviceid, received_at, statusFromCode] + ); + }); + } res.status(204).send('POST received'); });