Skip to content

Instantly share code, notes, and snippets.

@rts-rob
Last active September 9, 2017 08:59
Show Gist options
  • Save rts-rob/f5b1df1c7580722806bcd944f317579f to your computer and use it in GitHub Desktop.
Save rts-rob/f5b1df1c7580722806bcd944f317579f to your computer and use it in GitHub Desktop.
Final handler.js
'use strict';
let fs = require('fs');
let mysql = require('mysql');
let querystring = require('querystring');
module.exports.reset = (event, context, callback) => {
// Extract the Slack username
const requestingUser = querystring.parse(event.body).user_name;
// Setup the MySQL connection
const connection = mysql.createConnection({
host: "somedatabasehost.com",
user: "someuser",
password: "somepassword",
multipleStatements: true
});
// Load in the reset script
const resetScript = fs.readFileSync("resetdata.sql");
// Execute the reset script
const query = `USE ${requestingUser}; ${resetScript}`;
connection.connect();
connection.query(query, function (error, results, fields) {
connection.end();
const response = {
statusCode: 200,
body: JSON.stringify({
text: `Hi, ${requestingUser}.\nYour database has been reset!`
})
};
callback(null, response);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment