How to build an API for SAP HANA using strongloop/loopback

One of the aspects I like the most about SAP HANA is the cloud app development environment that allows you to quickly put together a data-entry app using Fiori.

Recently, I wanted to build a small JavaScript app for data querying and entry using the awesome ag-grid. The data was in SAP HANA but the prospect of building and testing a secure API was quite daunting (is it worth it? How long is it going to take? Who’s going to maintain it?) It was actually easier to switch to MongoDB, use Express or Parse and add an ETL process to sync the databases. Wouldn’t it be great if there was a way to create some sort of automatic API through configuration?

There is.

The Loopback component of Strongloop offers the possibility to quickly create secure APIs for CRUD operations against MySQL, Postgres, Oracle and other databases. In many cases, it allows you to completely bypass the development of boring and —commoditized— backend stuff. Using a convention over configuration approach, you can create endppoints for each of your tables in a matter of minutes.

But, can it connect to SAP HANA?

I googled and found a connector for HANA

The best way to set this up is to containerize the solution: create a docker container with an installation of strongloop and link a directory in the host machine to the working directory of the container, that way you keep the configuration outside of the container to quickly modify it, and you can quickly switch or upgrade the container.

I started from node official image, but you can start from the strongloop official image, and created my own, which you can use. Here is a link to my image.

FROM node

MAINTAINER Daniel Pradilla <info@danielpradilla.info>

RUN npm -g config set user root

RUN npm install -g --unsafe-perm strongloop 

RUN npm install loopback-datasource-juggler

RUN npm install loopback-connector-saphana

WORKDIR /app
EXPOSE 3000

Once you start the container, you need to create a connection to SAP HANA

docker run --name loopback -p 3000:3000 -v `pwd`:/app/ -t -i danielpradilla/loopback slc loopback:datasource

Then, you have to edit your server/datasources.json file and manually specify the schema name (This is something that you don’t need to do with other databases, and you may get stuck on a table not found error if you don’t do it)

{
  "db": {
    "name": "db",
    "connector": "memory"
  },
  "hana": {
    "host": "my_server_address",
    "port": my_server_port,
    "database": "MY_DATABASE_NAME",
    "name": "hana",
    "user": "my_HANA_user",
    "password": "my_hana_password",
    "schema": "MY_SCHEMA_NAME",
    "connector": "saphana"
  }
}

Then you create an endpoint to the table using the wizard

docker run --name loopback -p 3000:3000 -v `pwd`:/app/ -t -i danielpradilla/loopback slc loopback:model

Or using arc by running the API interface

docker run --name loopback -p 3000:3000 -v `pwd`:/app/ -t -i danielpradilla/loopback slc arc

And after that, you are ready to experience the awesomeness of having all the API endpoints created for you.

docker run --name loopback -p 3000:3000 -v `pwd`:/app/ -t -i danielpradilla/loopback slc run .

The next step would be to secure the API using microgateway for API Key validation, OAuth 2.0 and rate limiting,

I'm a software architect and I help people solve their problems with technology. In this site, I write about how to seize the opportunities that a hyperconnected world offers us. How to live simpler and more productive lives. I invite you to check the "Best of" section. If you want to contact me, or work with me, you can use the social links below.