How to connect SAP HANA Cloud database using Node Js


Dear SAPLearners, in this blog post we will learn on how to connect to SAP HANA Cloud database using Node Js application.

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

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

Lets get started.

✋🏻 Prerequisites

To complete this tutorial, you will need:

📚 Step-by-Step Procedure

1. The First thing we want to do is create a folder “hanaclouddb-nodejs” in any of your location choice on your local machine. Let’s do this using the following windows command:

mkdir hanaclouddb-nodejs

2. Navigate to the folder and initialize the a node project by using the following command:

npm init 

3. Provide the following information to initialize a Node.js project. You can just enter for every question and leave all values to default.

4. Now that we have initialize the project. Lets install the SAP HANA Node.js client which is used to connect to HANA cloud database. Let’s do this using the following command:

npm install --save @sap/hana-client

@sap/hana-client is the official driver for SAP HANA. You can find more information about the package here.

5. After couple of minutes, the package is successfully installed.

6. Finally its time to write JavaScript code to connect to cloud database. Here is code snippet i used and you can use the same

"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;
    function (err, result) {
      if (err) throw err;

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

8. Finally lets run the code, you can do this by using following command in the project terminal:

npm start (or) node index.js

9. Awesome! HANA SQL query output is show in the console.


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

Please feel free to comment and let us know your feedback. Subscribe for more updates

If you liked it ❤️, please share it! Thanks! 🙏