With the standard hdb module you use nested events and callbacks like this:
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;
});
However this module wraps the major features of hdb module in a ES6 class and returns promises. Therefore you could re-write the above block using the easier to read and maintain promise based approach. You just pass in an instance of the HANA Client @sap/hdbext module. In this example its a typical example that gets the HANA client as Express Middelware (req.db):
const dbClass = require("sap-hdb-promisfied")
let db = new dbClass(req.db)
db.preparePromisified(`SELECT SESSION_USER, CURRENT_SCHEMA
FROM "DUMMY"`)
.then(statement => {
db.statementExecPromisified(statement, [])
.then(results => {
let result = JSON.stringify({
Objects: results
})
return res.type("application/json").status(200).send(result)
})
.catch(err => {
return res.type("text/plain").status(500).send(`ERROR: ${err.toString()}`)
})
})
.catch(err => {
return res.type("text/plain").status(500).send(`ERROR: ${err.toString()}`)
})
Or better yet if you are running Node.js 8.x or higher you can use the new AWAIT feature and the code is even more streamlined:
try {
const dbClass = require("sap-hdb-promisfied")
let db = new dbClass(req.db);
const statement = await db.preparePromisified(`SELECT SESSION_USER, CURRENT_SCHEMA
FROM "DUMMY"`)
const results = await db.statementExecPromisified(statement, [])
let result = JSON.stringify({
Objects: results
})
return res.type("application/json").status(200).send(result)
} catch (e) {
return res.type("text/plain").status(500).send(`ERROR: ${e.toString()}`)
}
There are even static helpers to load the HANA connection information from the environment (or local testing file like default-env.json or .env) and create the HANA client for you. We can adjust the above example for such a scenario:
try {
import dbClass from 'sap-hdb-promisfied'
let db = new dbClass(await dbClass.createConnectionFromEnv(dbClass.resolveEnv(null)))
const statement = await db.preparePromisified(`SELECT SESSION_USER, CURRENT_SCHEMA
FROM "DUMMY"`)
const results = await db.statementExecPromisified(statement, [])
console.table(results)
db.destroyClient()
} catch (/** @type {any} */ err) {
console.error(`ERROR: ${err.toString()}`)
}
The following hdb functions are exposed as promise-based methods
prepare = preparePromisified(query)
statement.exec = statementExecPromisified(statement, parameters)
loadProcedure = loadProcedurePromisified(hdbext, schema, procedure)
storedProc = callProcedurePromisified(storedProc, inputParams)
We also have the simplified helper method to both prepare and execute a simple statement via one command:
execSQL(sql)
And finally there are static helpers
createConnectionFromEnv(envFile)
createConnection(options)
resolveEnv(options)
schemaCalc(options, db)
objectName(name)
-
Install any supported version of Node.js on your development machine https://nodejs.org/en/download/
-
Install the code sample as a reusable Node.js module
npm install -g sap-hdb-promisfied
Or you can leverage this module by just listing as requirement in your own project's package.json.
Finally you can clone the repository from https://github.com/SAP-samples/hana-hdbext-promisified-example to study the source content and view the consumption examples (test.js)
None
This project is provided "as-is": there is no guarantee that raised issues will be answered or addressed in future releases.
Copyright (c) 2024 SAP SE or an SAP affiliate company. All rights reserved. This project is licensed under the Apache Software License, version 2.0 except as noted otherwise in the LICENSE file.