Skip to content

Instantly share code, notes, and snippets.

@DonSchenck
Created August 31, 2018 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DonSchenck/64c6474b85c03b95d7b41169157e7925 to your computer and use it in GitHub Desktop.
Save DonSchenck/64c6474b85c03b95d7b41169157e7925 to your computer and use it in GitHub Desktop.
first attempt reading mysql inside openwhisk using node.js (note: mysql is running in openshift; so is openwhisk)
function helloworld(params) {
return new Promise(function (resolve, reject ) {
// Default values set here
let name = params.name || 'stranger';
getGreeting(name, value => {
resolve({message: value});
})
})
};
function getGreeting(name, callback) {
const format = require('string-format');
const mysql = require('mysql');
// Get special greeting from database here
const dbconn = mysql.createConnection({
host: 'mysql',
user: 'myuser',
password: 'myuser',
database: 'mydb'
});
// Default greeting
var greeting = "Hello";
try {
dbconn.connect((err) => {
if (err) {
}
});
} catch (error) {
}
try {
var qry = format('SELECT custom_greeting FROM personal_greeting WHERE first_name = "{}";', name);
dbconn.query(qry, (err,rows) => {
if ( rows.length > 0 ) {
greeting = rows[0].custom_greeting;
}
return callback(format(greeting + ", {}", name));
});
} catch (error) {
};
dbconn.end;
}
exports.main = helloworld;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment