Connect to Data Lake Relational Engine Using the Node.js Driver
- How to install Node.js and the data lake Relational Engine Node.js driver
- How to create and debug a Node.js application
- How to use both the synchronous and asynchronous driver interfaces
Prerequisites
- You have completed the first tutorial in this group.
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 1
Ensure you have Node.js installed and check its version. Enter the following command:
ShellCopynode -vIf 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.

- Step 2
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
nodeand enter the newly created directory.Shell (Microsoft Windows)Copymkdir %HOMEPATH%\DataLakeClientsTutorial\node cd %HOMEPATH%\DataLakeClientsTutorial\nodeShell (Linux)Copymkdir $HOME/DataLakeClientsTutorial/node cd $HOME/DataLakeClientsTutorial/node -
Initialize the project and install the
@sap\iq-clientdriver from npm.ShellCopynpm init -y npm install @sap/iq-client -
The following command lists the Node.js modules that are now installed locally into the
DataLakeClientsTutorial\nodefolder.ShellCopynpm list
-
- Step 3
- Create a new file named
nodeQuery.jsin an editor.
Depending on what version of the data lake client was used, execute:
```Shell (Microsoft Windows) notepad nodeQuery.js ``` Substitute `pico` below for your preferred text editor. ```Shell (Linux or Mac) pico nodeQuery.js ```-
Add the code below to
nodeQuery.jsand update the host variable.JavaScriptCopy'use strict'; const { PerformanceObserver, performance } = require('perf_hooks'); var t0; var util = require('util'); var datalakeRE = require('@sap/iq-client'); var connOptions = { 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 var connection = datalakeRE.createConnection(); connection.connect(connOptions); var sql = 'select TITLE, FIRSTNAME, NAME from HOTELS.CUSTOMER;'; t0 = performance.now(); var result = connection.exec(sql); console.log(util.inspect(result, { colors: false })); var t1 = performance.now(); console.log("time in ms " + (t1 - t0)); connection.disconnect(); -
Run the app.
ShellCopynode nodeQuery.js
If an error appears such as Error:
libdbcapi_r.sois missing, its location can be specified using an environment variable such as IQ_DBCAPI_DIR.Note the above app makes use of some of the data lake Relational Engine client Node.js driver methods, such as connect, exec and disconnect.
Two examples showing the drivers methods being used asynchronously are shown in the next two steps.
To enable debug logging of the Node.js client, enter the following command and then rerun the app.
Shell (Microsoft Windows)Copyset DEBUG=* node nodeQuery.jsLinux or Mac
ShellCopyexport DEBUG=* node nodeQuery.js
The value of the environment variable DEBUG can be seen and removed with the commands below.
Shell (Microsoft Windows)Copyset DEBUG set DEBUG= set DEBUGLinux or Mac
Shell (Linux)Copyprintenv | grep DEBUG unset DEBUG printenv | grep DEBUG
- Create a new file named
- Step 4
Asynchronous programming enables non-blocking code execution which is demonstrated in the below example.
-
Open a file named
nodeQueryCallback.jsin an editor.Shell (Microsoft Windows)Copynotepad nodeQueryCallback.jsSubstitute
picobelow for your preferred text editor.Shell (Linux)Copypico nodeQueryCallback.js -
Add the code below to
nodeQueryCallback.jsand update the host variable.JavaScriptCopy'use strict'; const { PerformanceObserver, performance } = require('perf_hooks'); var t0; var util = require('util'); var datalakeRE = require('@sap/iq-client'); var connOptions = { 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 var connection = datalakeRE.createConnection(); connection.connect(connOptions, function(err) { if (err) { return console.error(err); } //Prepared statement example const statement = connection.prepare('CALL HOTELS.SHOW_RESERVATIONS(?,?)'); const parameters = [11, '2020-12-24']; var results = statement.execQuery(parameters, function(err, results) { if (err) { return console.error(err); } processResults(results, function(err) { if (err) { return console.error(err); } results.close(function(err) { if (err) { return console.error(err); } statement.drop(function(err) { if (err) { return console.error(err); } return connection.disconnect(function(err) { if (err) { return console.error(err); } }); }); }); }); }); }); function processResults(results, cb) { results.next(function (err, hasValues) { if (err) { return console.error(err); } if (hasValues) { results.getValues(function (err, row) { console.log(util.inspect(row, { colors: false })); processResults(results, cb); }); } else { return cb(); } }); } -
Run the app.
ShellCopynode nodeQueryCallback.js
Notice that asynchronous method calls use callback functions.
-
- Step 5
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.jsin an editor.Shell (Microsoft Windows)Copynotepad nodeQueryPromise.jsSubstitute
picobelow for your preferred text editor.Shell (Linux or Mac)Copypico nodeQueryPromise.js -
Add the code below to
nodeQueryPromise.js.JavaScriptCopy'use strict'; const { PerformanceObserver, performance } = require('perf_hooks'); var t0; var util = require('util'); var datalakeRE = require('@sap/iq-client'); var PromiseModule = require('@sap/iq-client/extension/Promise.js'); var connOptions = { //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 var connection = datalakeRE.createConnection(); var statement; PromiseModule.connect(connection, connOptions) .then(() => { //Prepared statement example return PromiseModule.prepare(connection, 'CALL HOTELS.SHOW_RESERVATIONS(?,?)'); }) .then((stmt) => { statement = stmt; const parameters = [11, '2020-12-24']; return PromiseModule.executeQuery(stmt, parameters); }) .then((results) => { return processResults(results); }) .then((results) => { return PromiseModule.close(results); }) .then(() => { return PromiseModule.drop(statement); }) .then(() => { PromiseModule.disconnect(connection); }) .catch(err => { console.error(err); }); function processResults(results) { return new Promise((resolve, reject) => { var done = false; PromiseModule.next(results) .then((hasValues) => { if (hasValues) { return PromiseModule.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); }); }) } -
Run the app.
ShellCopynode nodeQueryPromise.js
The above code makes use of the promise module. Additional details on promises can be found at Using Promises.
-
- Step 6
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.
-
If required, download Visual Studio Code..
-
In Visual Studio Code, choose File | Add Folder to Workspace and then add the
DataLakeClientsTutorialfolder.
-
Open the file
nodeQuery.js. -
Place a breakpoint inside the
connection.execcallback. Select Run | Start Debugging | Node.js.Notice that the debug view becomes active.
Notice that the program stops running at the breakpoint that was set. Observe the variable values in the leftmost pane. Step through code.

If “Can’t find Node.js binary ‘node’: path does not exist” error pops up, open a Shell and run the following command.
ShellCopycode .Then restart VSCode.
-
- Step 7
Congratulations! You have created and debugged a Node.js application that connects to and queries an SAP data lake Relational Engine database.
Which of the following statements are true?