'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 Promise.resolve({
- looks: 'Looks',
- good: 'Good',
- to: 'To',
- me: 'Me',
- emoji: '👍'
+ 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
+ });
+ });
});
}
};