]>
Commit | Line | Data |
---|---|---|
649f6698 BB |
1 | 'use strict'; |
2 | ||
a2a2b553 BB |
3 | const Config = require('../config/config'); |
4 | const Mysql = require('mysql2'); | |
5 | ||
649f6698 BB |
6 | const internals = {}; |
7 | ||
8 | module.exports = internals.Record = class { | |
a2a2b553 BB |
9 | |
10 | constructor() { | |
11 | ||
12 | this.connection = Mysql.createConnection(Config.mysql); | |
13 | } | |
14 | ||
649f6698 BB |
15 | fetch() { |
16 | ||
a2a2b553 BB |
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 | }); | |
649f6698 BB |
43 | }); |
44 | } | |
45 | }; |