Could Any one please tell me, Is it possible to connect Apigee Edge with any external Backend databases like MYSQL/MongoDB?

Not applicable

Could Any one please tell me, Is it possible to connect Apigee Edge with any external Backend

databases like MYSQL/MongoDB?

3 9 1,473
9 REPLIES 9

Yes, absolutely!

MongoDB has an HTTP interface, and you could build API proxies in Apigee Edge to connect directly to mongo in that regard. There's a similar model, proxying to UserGrid.

I don't know about MySQL, I don't know if it has a similar HTTP interface. But I do know that it is possible to write nodejs targets for Apigee Edge, which would allow you to bridge into MySQL from the nodejs driver.

Could you please tell me the process of Connecting apigee with MongoDB

Check out Diego's answer - he includes sample code too!

@Dino The link is returning answer was deleted

i don't know what happened to that link...

but try this one.

https://community.apigee.com/questions/62378/java-call-out-mongodb-connection-sql-queries.html

Not applicable

Agreed. It's possible.

1. MySQL

Take a look at the documentation of NPM MySQL Node.js https://github.com/mysqljs/mysql. Taken from the documentation, you just need to do this:

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'me',
  password : 'secret',
  database : 'my_db'
});

connection.connect();

connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
  if (error) throw error;
  console.log('The solution is: ', results[0].solution);
});

connection.end();

2. MongoDB

Takes a look at MongoDB native driver https://github.com/mongodb/node-mongodb-native.

var MongoClient = require('mongodb').MongoClient
  , assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log("Connected correctly to server");

  // call findDocuments here.
  db.close();
});


var findDocuments = function(db, callback) {
  // Get the documents collection
  var collection = db.collection('documents');
  // Find some documents
  collection.find({}).toArray(function(err, docs) {
    assert.equal(err, null);
    assert.equal(2, docs.length);
    console.log("Found the following records");
    console.dir(docs);
    callback(docs);
  });
}

I've use Postgres DB and it works like charm. Give it a try and let us know how it goes!

Nice answer! sample code too! w00t!

@Dino @Diego Zuluaga Hi, unfortunately this is not working for me. Any ideas? How would one even get mongodb running on Edge in the first place? I guess the first question is that, how do we start a Mongo instance on Edge?

Getting this error:

{status: 500,
errors: 
[
{name: "MongoError",
message: "connect EINVAL"
}
]
}<br>

database.js file:

exports.getGPSData = function () {
  return new Promise(function (resolve, reject) {
    mongoClient.connect(envConfig.database, function (err, db) {
      if (err) {
        return reject(err);
      } else {
        db.collection('landmarks').find({}).toArray() // Find all objects
          .then(function (result) {
            return resolve(result);
          })
          .catch(function (err) {
            return reject(err);
          })
      }
    })
  })
};

<br>

Mongo url coming from my config file:

database: 'mongodb://localhost:27017/gpsdataloaderapi'<br>

Never mind, I see there is no connector for MongoDB yet:(