Skip to Content

SAP HANA XS Advanced - Database access from Node.js

test
0 %
SAP HANA XS Advanced - Database access from Node.js
Details

SAP HANA XS Advanced - Database access from Node.js

April 9, 2020
Created by
September 16, 2016
Connecting to a SAP HANA database using Node.js

You will learn

Learn how to connect to the SAP HANA database using Node.js and the HANA database library, @sap/hdbext.


Step 1: Add handler for new example request

In the previous tutorial, you added a handler for a path called /node by modifying the files myNode.js.

Add a new route for example1 in myNode.js to get the database connection/client from the express request object (req.db). Then create a prepared statement for the SELECT of SESSION_USER from dummy (dummy is the synonym created in the initial HDI tutorial). Execute the statement and send the results as JSON in the response object.


//Simple Database Select - In-line Callbacks //Example1 handler app.get("/example1", (req, res) => { let client = req.db; client.prepare( `SELECT SESSION_USER, CURRENT_SCHEMA FROM "DUMMY"`, (err, statement) => { if (err) { return res.type("text/plain").status(500).send(`ERROR: ${err.toString()}`); } statement.exec([], (err, results) => { if (err) { return res.type("text/plain").status(500).send(`ERROR: ${err.toString()}`); } else { var result = JSON.stringify({ Objects: results }); return res.type("application/json").status(200).send(result); } }); return null; }); return null; });

As follows:

Extend for select
Log on to answer question
Step 2: Run the node and web modules

Run the node and web modules. You should see that the build and deploy are successful. Call the example1 script by changing the web tab

add node select

You can see the SESSION_USER returned by the SELECT statement.

Log on to answer question
Step 3: Use the async module to access the database

The default programming approach in Node.js is using callbacks/event handlers. This is because even the different parts of a database request (connection, prepared statement, execution, etc) are all non-blocking operations.

Hint: A callback is an asynchronous operation that gets executed after another one notifies completion.

Add a second route handler, called example2, that uses the async module:

var async = require("async");
//Simple Database Select Via Client Wrapper/Middelware - Async Waterfall
app.get("/example2", (req, res) => {
	let client = req.db;
	async.waterfall([
		function prepare(callback) {
			client.prepare(`SELECT SESSION_USER, CURRENT_SCHEMA
											FROM "DUMMY"`,
				(err, statement) => {
					callback(null, err, statement);
				});
		},

		function execute(err, statement, callback) {
			statement.exec([], (execErr, results) => {
				callback(null, execErr, results);
			});
		},
		function response(err, results, callback) {
			if (err) {
				res.type("text/plain").status(500).send(`ERROR: ${err.toString()}`);
			} else {
				var result = JSON.stringify({
					Objects: results
				});
				res.type("application/json").status(200).send(result);
			}
			return callback();
		}
	]);
});

Take a look at both newly-added examples. You can easily notice that the second one is easier to read, although the runtime does not change. You can check for more information on the async module in the async documentation.

Log on to answer question

Next Steps

Back to top