aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBen Beltran <ben@nsovocal.com>2016-11-28 02:28:17 -0600
committerBen Beltran <ben@nsovocal.com>2016-11-28 02:28:17 -0600
commitead516a9d74f346c4556c7eaab7014f95486f29e (patch)
treebab9ac6f7e191e915082e61ff41171412c822061 /lib
parent927c9aae8b0e28b39f0ee913d9bf53a3c0687e43 (diff)
LGTM-2 Read from database
Squashed commit of the following: commit a2a2b553f69a66f1825347abde4f0028773bdbdb Author: Ben Beltran <ben@nsovocal.com> Date: Mon Nov 28 02:27:19 2016 -0600 Fetch random record in record_fetcher commit 6b98202fc8ea9c5811134cad592f11b6b89d008a Author: Ben Beltran <ben@nsovocal.com> Date: Mon Nov 28 02:27:08 2016 -0600 Add some instructions to the readme commit f5efb2b5de23844e6317a6f636abb75955e60c40 Author: Ben Beltran <ben@nsovocal.com> Date: Mon Nov 28 02:26:57 2016 -0600 Add DB setup script commit d6d8813ef55bb1297089c9dbb87a9ab86e6c5ba2 Author: Ben Beltran <ben@nsovocal.com> Date: Mon Nov 28 02:26:34 2016 -0600 Add dotenv to the configs commit 649f66982d2152d36a782577043505835f235987 Author: Ben Beltran <ben@nsovocal.com> Date: Mon Nov 28 01:29:49 2016 -0600 Use a template instead of static html index
Diffstat (limited to 'lib')
-rw-r--r--lib/record_fetcher.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/record_fetcher.js b/lib/record_fetcher.js
new file mode 100644
index 0000000..a506fe7
--- /dev/null
+++ b/lib/record_fetcher.js
@@ -0,0 +1,45 @@
+'use strict';
+
+const Config = require('../config/config');
+const Mysql = require('mysql2');
+
+const internals = {};
+
+module.exports = internals.Record = class {
+
+ constructor() {
+
+ this.connection = Mysql.createConnection(Config.mysql);
+ }
+
+ fetch() {
+
+ return new Promise((resolve, reject) => {
+ this.connection.query(`SELECT * FROM ${Config.mysql.tableName} ORDER BY RAND() LIMIT 1`, (err, results) => {
+
+ if (err) {
+ console.error('Error while fetching row');
+ console.error(err.message);
+ return reject(err);
+ }
+
+ if (results.length === 0) {
+ const error = new Error('No rows found.');
+ console.error('Error while fetching row');
+ console.error(error.message);
+ return reject(err);
+ }
+
+ const row = results[0];
+
+ return resolve({
+ looks: row.l,
+ good: row.g,
+ to: row.t,
+ me: row.m,
+ emoji: row.emoji
+ });
+ });
+ });
+ }
+};