Created
August 31, 2018 18:07
-
-
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)
This file contains hidden or 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
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