Node.js provides a JavaScript runtime outside of the browser and uses an asynchronous event driven programming model. For more details, see Introduction to Node.js. On Microsoft Windows, in this tutorial, the shell used is the Command Prompt.
Step 1Install Node.js
โ
Ensure you have Node.js installed and check its version. Enter the following command:
Shell
node -v
If Node.js is installed, the currently installed version is returned, such as v22.16.0.
If Node.js is not installed, download the long-term support (LTS) version of the Node.js installer from Download Node.js.
If an install for Node.js is not provided on Linux, you may choose to install it via a package manager. For more details, please navigate to this link.
During the installation, there is no need to install Chocolatey.
Chocolatey
Step 2Install the data lake Relational Engine client for Node.js
+
The Node.js driver covered in this tutorial is @sap\iq-client which supports the latest Node.js versions and includes a promise library. An alternate driver is the SQL Anywhere driver.
Open a new Shell and create a folder named node and enter the newly created directory.
Shell
mkdir %HOMEPATH%\DataLakeClientsTutorial\node
cd %HOMEPATH%\DataLakeClientsTutorial\node
Initialize the project and install the @sap\iq-client driver from npm.
Shell
npm init -y
npm install @sap/iq-client
The following command lists the Node.js modules that are now installed locally into the DataLakeClientsTutorial\node folder.
Shell
npm list
npm list
Step 3Create a synchronous Node.js application that queries SAP data lake Relational Engine
+
Create a new file named nodeQuery.js in an editor.
Depending on what version of the data lake client was used, execute:
Shell
notepad nodeQuery.js
Substitute pico below for your preferred text editor.
Shell
pico nodeQuery.js
Add the code below to nodeQuery.js and update the host variable.
JavaScript
'use strict';const{PerformanceObserver,performance}=require('perf_hooks');vart0;varutil=require('util');vardatalakeRE=require('@sap/iq-client');varconnOptions={host:'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX.iq.hdl.trial-XXXX.hanacloud.ondemand.com:443',uID:'USER1',pwd:'Password1',enc:'TLS{tls_type=rsa;direct=yes}',};//Synchronous example querying a table
varconnection=datalakeRE.createConnection();connection.connect(connOptions);varsql='select TITLE, FIRSTNAME, NAME from HOTELS.CUSTOMER;';t0=performance.now();varresult=connection.exec(sql);console.log(util.inspect(result,{colors:false}));vart1=performance.now();console.log("time in ms "+(t1-t0));connection.disconnect();
Run the app.
Shell
node nodeQuery.js
Running nodeQuery.js
If an error appears such as Error: libdbcapi_r.so is missing, its location can be specified using an environment variable such as IQ_DBCAPI_DIR.
Step 4Create an asynchronous app that uses callbacks
+
Asynchronous programming enables non-blocking code execution which is demonstrated in the below example.
Open a file named nodeQueryCallback.js in an editor.
Shell
notepad nodeQueryCallback.js
Substitute pico below for your preferred text editor.
Shell
pico nodeQueryCallback.js
Add the code below to nodeQueryCallback.js and update the host variable.
JavaScript
'use strict';const{PerformanceObserver,performance}=require('perf_hooks');vart0;varutil=require('util');vardatalakeRE=require('@sap/iq-client');varconnOptions={host:'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX.iq.hdl.trial-XXXX.hanacloud.ondemand.com:443',uID:'USER1',pwd:'Password1',enc:'TLS{tls_type=rsa;direct=yes}',};//Asynchronous example calling a stored procedure with callbacks
varconnection=datalakeRE.createConnection();connection.connect(connOptions,function(err){if(err){returnconsole.error(err);}//Prepared statement example
conststatement=connection.prepare('CALL HOTELS.SHOW_RESERVATIONS(?,?)');constparameters=[11,'2020-12-24'];varresults=statement.execQuery(parameters,function(err,results){if(err){returnconsole.error(err);}processResults(results,function(err){if(err){returnconsole.error(err);}results.close(function(err){if(err){returnconsole.error(err);}statement.drop(function(err){if(err){returnconsole.error(err);}returnconnection.disconnect(function(err){if(err){returnconsole.error(err);}});});});});});});functionprocessResults(results,cb){results.next(function(err,hasValues){if(err){returnconsole.error(err);}if(hasValues){results.getValues(function(err,row){console.log(util.inspect(row,{colors:false}));processResults(results,cb);});}else{returncb();}});}
Run the app.
Shell
node nodeQueryCallback.js
Running nodeQueryCallback.js
Notice that asynchronous method calls use callback functions.
Step 5Create an asynchronous app that uses promises
+
The Node.js driver for the data lake Relational Engine client provides support for promises. The following example demonstrates this. Notice that there is less nesting of code then the previous example.
Open a file named nodeQueryPromise.js in an editor.
Shell
notepad nodeQueryPromise.js
Substitute pico below for your preferred text editor.
Shell
pico nodeQueryPromise.js
Add the code below to nodeQueryPromise.js.
JavaScript
'use strict';const{PerformanceObserver,performance}=require('perf_hooks');vart0;varutil=require('util');vardatalakeRE=require('@sap/iq-client');varPromiseModule=require('@sap/iq-client/extension/Promise.js');varconnOptions={//Specify the connection parameters
host:'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX.iq.hdl.trial-XXXX.hanacloud.ondemand.com:443',uid:'USER1',pwd:'Password1',enc:'TLS{tls_type=rsa;direct=yes}',};//Asynchronous example calling a stored procedure that uses the promise module
varconnection=datalakeRE.createConnection();varstatement;PromiseModule.connect(connection,connOptions).then(()=>{//Prepared statement example
returnPromiseModule.prepare(connection,'CALL HOTELS.SHOW_RESERVATIONS(?,?)');}).then((stmt)=>{statement=stmt;constparameters=[11,'2020-12-24'];returnPromiseModule.executeQuery(stmt,parameters);}).then((results)=>{returnprocessResults(results);}).then((results)=>{returnPromiseModule.close(results);}).then(()=>{returnPromiseModule.drop(statement);}).then(()=>{PromiseModule.disconnect(connection);}).catch(err=>{console.error(err);});functionprocessResults(results){returnnewPromise((resolve,reject)=>{vardone=false;PromiseModule.next(results).then((hasValues)=>{if(hasValues){returnPromiseModule.getValues(results);}else{done=true;}}).then((values)=>{if(done){resolve(results);}else{console.log(util.inspect(values,{colors:false}));processResults(results).then((results)=>{resolve(results);});}}).catch(err=>{reject(err);});})}
Visual Studio Code can run and debug a Node.js application. It is a lightweight but powerful source code editor which is available on Windows, macOS and Linux. Ensure that Node.js is added to your path in environment variables such as C:\Program Files\nodejs.
Share feedback on this tutorial or join the conversation in SAP Community.
Submit detailed feedbackDiscuss in Community
Steps
Step 1 of 7
1. Install Node.js2. Install the data lake Relational Engine client for Node.js3. Create a synchronous Node.js application that queries SAP data lake Relational Engine4. Create an asynchronous app that uses callbacks5. Create an asynchronous app that uses promises6. Debug the application7. Knowledge check
Joule
AI Notice
Joule is an AI assistant. Generative AI may produce inaccurate, incomplete, or biased information. Always verify important details before acting on them.
Conversations are sent to SAP-hosted large language models for processing. Do not include personal data, credentials, or confidential information in your messages.
Joule's responses are based on the SAP tutorial catalog and may not reflect the latest product changes. For authoritative guidance, consult the linked tutorials and official SAP documentation.