-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (38 loc) · 1.03 KB
/
index.js
File metadata and controls
43 lines (38 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const DataStore = require('hubot/es2015').DataStore;
const mysql = require('promise-mysql');
let json_typecast = function(field, next) {
if (field.type == 'JSON') {
return (JSON.parse(field.string()));
}
return next();
}
class MysqlDataStore extends DataStore {
constructor (host, user, password, database, charset, port) {
super();
this.pool = mysql.createPool({
connectionLimit: 2,
host,
user,
password,
database,
charset,
port,
});
}
_get (key, table) {
return this.pool.query({
sql: `SELECT value FROM \`${table}\` WHERE \`key\` = ?`,
values: [key],
typeCast: json_typecast
}).then((rows) => {
if (rows.length > 0) {
return rows[0].value;
}
});
}
_set (key, value, table) {
let serialized = JSON.stringify(value);
return this.pool.query(`INSERT INTO \`${table}\` (\`key\`, \`value\`) VALUES (?, ?) ON DUPLICATE KEY UPDATE \`value\` = ?`, [key, serialized, serialized]);
}
}
module.exports = MysqlDataStore