Last active
September 9, 2017 08:59
-
-
Save rts-rob/f5b1df1c7580722806bcd944f317579f to your computer and use it in GitHub Desktop.
Final handler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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