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 =)



0
0
0.000
8 comments
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support. 
 

0
0
0.000
avatar
Thank you for sharing this post on HIVE!

Your content got selected by our fellow curator tibfox & you received a little thank you upvote from our non-profit curation initiative. Your post will be featured in one of our recurring curation compilations which is aiming to offer you a stage to widen your audience within the DIY scene of Hive.

Next time make sure to post / cross-post your creation within the DIYHub community on HIVE and you will receive a higher upvote!

Stay creative & hive on!
0
0
0.000
avatar
(Edited)

Thanks for sharing this. I never tried anything with microsoft azure, having that on my bookmark now. I only have experience with google cloud so far and it was seamless. Also API is a topic I have yet to master, I am still learning since at some point I want to build a usable app. So no comment much on your code but appreciate that you're sharing it 😃

0
0
0.000
avatar

Yeah for me the most important thing is to add in our experience the possibility of building an API as a tool to face some challenges!Thanks for the comment!!

0
0
0.000
avatar

Congratulations @gwajnberg! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

You distributed more than 7000 upvotes.
Your next target is to reach 8000 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

0
0
0.000