]> git.r.bdr.sh - rbdr/lgtm/blob - lib/record_fetcher.js
a506fe7a9d1a84fa3a0ee5d10d59c464fcd72f97
[rbdr/lgtm] / lib / record_fetcher.js
1 'use strict';
2
3 const Config = require('../config/config');
4 const Mysql = require('mysql2');
5
6 const internals = {};
7
8 module.exports = internals.Record = class {
9
10 constructor() {
11
12 this.connection = Mysql.createConnection(Config.mysql);
13 }
14
15 fetch() {
16
17 return new Promise((resolve, reject) => {
18 this.connection.query(`SELECT * FROM ${Config.mysql.tableName} ORDER BY RAND() LIMIT 1`, (err, results) => {
19
20 if (err) {
21 console.error('Error while fetching row');
22 console.error(err.message);
23 return reject(err);
24 }
25
26 if (results.length === 0) {
27 const error = new Error('No rows found.');
28 console.error('Error while fetching row');
29 console.error(error.message);
30 return reject(err);
31 }
32
33 const row = results[0];
34
35 return resolve({
36 looks: row.l,
37 good: row.g,
38 to: row.t,
39 me: row.m,
40 emoji: row.emoji
41 });
42 });
43 });
44 }
45 };