Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
hemchand_sharma
Active Participant

Introduction


In this blog post, we will learn how to connect to SAP HANA Cloud database using Node Js application.

SAP HANA Cloud is a cloud-native database and a fully managed in-memory cloud database-as-a-service (DbaaS).

In this blog, we will use Node.js to connect to SAP HANA cloud database, use SQL queries, and obtain the results. We can still extend this example and expose the data as an API service or micro-service.

Let's get started.

Prerequisites


To complete this tutorial you will require -

  1. SAP cloud platform trail account with SAP Business Application studio (BAS) service as cloud application studio. To know more how to setup BAS check these tutorials - Setup Business Application Studio

  2. You will also require HANA Cloud instance up and running in the cloud foundry environment. Check this blog to set up your HANA cloud instance-HANA Cloud Instance set up


Let's proceed step by step



  1. Go to your SAP Business Application studio and click on the terminal button on the top panel and execute below command to create a project folder under the PROJECTS section.
    mkdir hana-nodejs




  2. Navigate to the folder with cd command and initialize the node project with the following command.
    npm init ​


  3. Once the above command executed command line will ask you to provide some information to initialize your packge.json file inside your project folder. An important field is a name which you can provide any name of your app for example - myapp.

  4. Now that we have initialized the project. Let's install the SAP HANA Node.js client which is used to connect to the HANA cloud database. Let’s do this using the following command:
    npm install --save @sap/hana-client


  5. After a couple of minutes, the package will successfully be installed.

  6. Your package.json file should look like as below.

  7. Now create one more folder under your main project folder it can be same as your package.json name field - myapp.

  8. inside your myapp folder create a file name - index.js which is same as the main field name in package.json and paste the below JavaScript code to make the database connection and fetch the record from HANA DB using nodejs.
    "use strict";
    var hana = require("@sap/hana-client");

    var connOptions = {
    serverNode: "<endpoint>",
    encrypt: "true",
    sslValidateCertificate: "false",
    uid: "<user?",
    pwd: "<password>",
    };

    var dbConnection = hana.createConnection();

    dbConnection.connect(connOptions, function (err) {
    if (err) throw err;
    dbConnection.exec(
    "SELECT * FROM <Schema>.<Table>",
    function (err, result) {
    if (err) throw err;
    console.log(result[0]);
    dbConnection.disconnect();
    }
    );
    });​


  9.  In the above code, replace <endpoint> with your HANA cloud database endpoint; <user> and <password> with your database user credentials.

  10. Finally lets run the code, you can do this by using the following command in the project terminal:
    npm start (or) node index.js​


  11. HANA SQL query output will be shown in the console.


   Conclusion


you have completed the first steps on connecting to SAP HANA Cloud database using Node Js application.

Thanks for reading!!

 
7 Comments
Labels in this area