Upload
truongdang
View
220
Download
0
Embed Size (px)
Citation preview
RHMAP MBaaS Services and RESTful APIs
Integrating with MongoDB and Amazon RDS
Hong Hua, Chad DarbySolution Architects @ GPTE2 May 2017
Agenda
● Create an RHMAP MBaaS Service
● Expose RESTful API for CRUDL operations
● First version: MongoDB for persistence
● Second version: Amazon RDS for persistence
Create a New User - Part 1
var $fh = require('fh-mbaas-api');
users.post('/', function(req, res) { console.log(new Date(), 'In user route POST / req.body=', req.body);
// // TODO: Do your work here //
res.json({'msg': 'POST Success'}); });
Create a New User - Part 2
// Create a single entry/row var options = { "act": "create", "type": "myusers", "fields": req.body };
$fh.db(options, function (err, data) { if (err) { res.json({msg : err}); } else { res.json(data); } });
Get a List of Users - Part 1
users.get('/', function(req, res) { console.log(new Date(), 'In user route GET / req.body=', req.body);
// // TODO: Do your work here //
res.json({'msg': 'GET Success'}); });
Get a List of Users - Part 2
// get a list of users var options = { "act": "list", "type": "myusers" };
$fh.db(options, function (err, data) { if (err) { res.json({msg : err}); } else { res.json(data); } });
RHMAP and Amazon RDS
● Amazon Relational Data Services (RDS)
● Example of connecting to an external data service … could be ANY data service
Create a New User - Set up Connection Pool
var pool = mysql.createPool({ connectionLimit : 10, host : process.env.MYSQL_HOST, database : process.env.MYSQL_DATABASE, user : process.env.MYSQL_USERNAME, password : process.env.MYSQL_PASSWORD});
Create a New User - Perform SQL Insert
users.post('/', function(req, res) {
var values = [req.body.firstname, req.body.lastname]; var theSql = "insert into demo_users set firstname=?, lastname=?";
pool.query(theSql, values, function(err, results) { if (err) { var info = {"status" : "fail", "error" : err}; res.json(info); }
var info = {"status" : "ok", "data" : results}; res.json(info); });
});
Get a List of Users - Perform Query
users.get('/', function(req, res) { var theSql = "select * from demo_users";
pool.query(theSql, function(err, rows) { if (err) { var result = {"status" : "fail","error" : err}; res.json(result); }
var result = {"status" : "ok", "data" : rows};
res.json(result); });
THANK YOUplus.google.com/+RedHat
linkedin.com/company/red-hat
youtube.com/user/RedHatVideos
facebook.com/redhatinc
twitter.com/RedHatNews
Chad Darby
Hong Hua
We love your feedback!