Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
mistydemeo committed Jan 11, 2019
0 parents commit d1f3ca0
Show file tree
Hide file tree
Showing 5 changed files with 750 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
/node_modules
20 changes: 20 additions & 0 deletions LICENSE.md
@@ -0,0 +1,20 @@
Copyright (c) 2019 GitHub Inc.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42 changes: 42 additions & 0 deletions index.js
@@ -0,0 +1,42 @@
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) {
super();
this.pool = mysql.createPool({
connectionLimit: 2,
host: host,
user: user,
password: password,
database: database,
charset: charset,
});
}

_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]);
}
}

exports = MysqlDataStore

0 comments on commit d1f3ca0

Please sign in to comment.