Why building your own API to support a front-end access to a cloud DB
banner built in canvas
  Hello all, today my post is a little bit different, I am used to post about science topics, but since I am a bioinformatician I also fall into some programming problems. Actually, my current project is more about developing than actually researching for now. Since I started in this career I was more focused in building back-end solutions to parse large amount of data for data analysis only. Now, my project is mostly to build a back-end solution to parse data, feed a database, and also display it in a front-end solution. My supervisor decided to go through a graphQL solution, at first we started with a dgraph database. However, since the only possibility to make it available online is to use the Microsoft solution called CosmoDB from Azure cloud, we had to migrate the back-end, change how it feeds the DB also.
  CosmoDB is interesting because it provides different APIs to access it, like using SQL syntaxes and using a the graphql language Gremlin, which was chosen by my supervisor.
  I hope you guys aren't getting bored, because I am reaching the problem. The thing is that my react app that I build previously to make queries in the previous database can only make Post and Get requisitions using the Rest API from CosmoDB, which uses the SQL syntax.
example of a Rest API query: https://learn.microsoft.com/en-us/rest/api/cosmos-db/querying-cosmosdb-resources-using-the-rest-api
  The tutorials available to use the Gremlin API at CosmoDB only provide back-end solutions, modules that work or for Python, nodejs, C# or java. The first thing that I tried was to install the nodejs module in my react application, it caused lots of conflict with the modules installed and the application had multiple errors when I import the module.
  So, after thinking I decided to make a backend script using nodejs, which plays as a midfielder between the react app and the cloud database.
my own figure
  What this script do? receives a Post/Get request from the react app and uses the gremlin module to perform the query at CosmoDB. So we can built that type script when it is difficult to create a direct connection between our front-end solution and the database. At first I tried using nest JS, however the gremlin module also had problems with some variables that don't work well as type. So I started the script from the scratch using the framework Express JS. Mostly I had to indicate in which port would run the API , since it is a program that in theory will be online all the time to receives Post requests from the web app, It comes as default for the port 3000, so I changed to 4000 since my web app use the 3000.
  Inside package.json I had to include into the scripts the command to start the script, I also installed nodemon to initiate the dev mode, which can allow a refresh during a live alteration of the script:
  I imported the modules used, such as express, gremlin (nodejs client for the database), bodyparser ( to parse the body from the post request), cors ( a solution that I learned after having some errors due to this CORS ) and the config file ( with access parameters for the DB, which you can find at the Microsoft tutorial, such as access key, db name, etc).

  First we initialize the express app and create the client variable using the gremlin module:
  Then some configurations, such as indicating that the Post body will be in a JSON format, also to the program to listen in the port 4000 and also the configuration of the CORS
  And finally we have the main functions, the post function which parse the body of the post request and executes the gremlin query function (which is using the gremlin module). The body has to have a query using the gremlin syntax. The gremlinQuery functions returns the query result to the post function which sends it back to the web app. As you can see it is a very short script, maybe one of my shortest functional scripts that I ever made, but it is my slave now to make things work
  So in conclusion guys, building your own API can help you deal with difficult access to databases that don't have the required access for fetching the data. By the way, one thing that could help as well to test this API is using the postman console. If somebody has something to comment, I would appreciate =)
Comments