voterunner

quick and dirty votes and discussions
git clone https://git.ce9e.org/voterunner.git

commit
2512ebe0abeb750dc27cc5cf67c0b95c1866464b
parent
d50d0f79c73b3595f7ac4ae3954f18b5e19de355
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2017-11-18 09:27
wrap db query in promise

Diffstat

M app.js 32 ++++++++++++++++++++++----------

1 files changed, 22 insertions, 10 deletions


diff --git a/app.js b/app.js

@@ -34,12 +34,20 @@ process.on('exit', (code) => {
   34    34 	db.close();
   35    35 });
   36    36 
   37    -1 var throwErr = function(err) {
   38    -1 	if (err) throw err;
   -1    37 var queryDB = function(sql, data) {
   -1    38 	return new Promise(function(resolve, reject) {
   -1    39 		var q = db.query(sql, data, function(err, res) {
   -1    40 			if (err) {
   -1    41 				reject(err);
   -1    42 			} else {
   -1    43 				resolve(res);
   -1    44 			}
   -1    45 		});
   -1    46 	});
   39    47 };
   40    48 
   41    49 // setup table
   42    -1 db.query('CREATE TABLE IF NOT EXISTS nodes (topic TEXT, id TEXT, name TEXT, comment TEXT, delegate TEXT, UNIQUE (topic, id))', throwErr);
   -1    50 queryDB('CREATE TABLE IF NOT EXISTS nodes (topic TEXT, id TEXT, name TEXT, comment TEXT, delegate TEXT, UNIQUE (topic, id))');
   43    51 
   44    52 var escapeHTML = function(unsafe) {
   45    53 	return unsafe
@@ -87,9 +95,10 @@ app.get('/:topic.json', function(req, res) {
   87    95 	var topic = req.params.topic;
   88    96 	var sql = 'SELECT id, name, comment, delegate FROM nodes WHERE topic = $1';
   89    97 
   90    -1 	db.query(sql, [topic], function(err, result) {
   91    -1 		if (err) return res.status(500).send(err.toString());
   -1    98 	queryDB(sql, [topic]).then(function(result) {
   92    99 		res.json(result.rows);
   -1   100 	}).catch(function(err) {
   -1   101 		res.status(500).send(err.toString());
   93   102 	});
   94   103 });
   95   104 
@@ -98,9 +107,10 @@ app.get('/:topic/:id?', function(req, res) {
   98   107 	var topic = req.params.topic;
   99   108 
  100   109 	var sql = 'SELECT id, name, comment, delegate FROM nodes WHERE topic = $1';
  101    -1 	db.query(sql, [topic], function(err, result) {
  102    -1 		if (err) return res.status(500).send(err.toString());
   -1   110 	queryDB(sql, [topic]).then(function(result) {
  103   111 		tpl('app.html', {'nodes': result.rows, 'topic': topic}, res);
   -1   112 	}).catch(function(err) {
   -1   113 		res.status(500).send(err.toString());
  104   114 	});
  105   115 });
  106   116 
@@ -111,7 +121,9 @@ io.sockets.on('connection', function(socket) {
  111   121 
  112   122 	var handleMsg = function(action, sql, v1, v2) {
  113   123 		// make sure that node exists, ignore error
  114    -1 		db.query('INSERT INTO nodes (topic, id) VALUES ($1, $2)', [topic, id], function() {
   -1   124 		return queryDB('INSERT INTO nodes (topic, id) VALUES ($1, $2)', [topic, id]).catch(function() {
   -1   125 			return;
   -1   126 		}).then(function() {
  115   127 			log.debug('Handeling:', action, topic, id, v1, v2);
  116   128 			io.to(topic).emit(action, id, v1, v2);
  117   129 
@@ -125,7 +137,7 @@ io.sockets.on('connection', function(socket) {
  125   137 				if (n >= 3) params.push(v1);
  126   138 				if (n >= 4) params.push(v2);
  127   139 
  128    -1 				return db.query(s, params, throwErr);
   -1   140 				return queryDB(s, params);
  129   141 			}));
  130   142 		});
  131   143 	};
@@ -169,7 +181,7 @@ io.sockets.on('connection', function(socket) {
  169   181 	socket.on('testClear', function(done) {
  170   182 		if (topic.substr(0, 4) === 'test') {
  171   183 			log.debug('Handeling:', 'testClear', topic);
  172    -1 			db.query("DELETE FROM nodes WHERE topic = $1", [topic], done);
   -1   184 			queryDB("DELETE FROM nodes WHERE topic = $1", [topic]).then(done);
  173   185 		} else {
  174   186 			done();
  175   187 		}