npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sap-hdbext-promisfied

v2.202404.2

Published

Promise wrapper for @sap/hdbext

Downloads

6,005

Readme

Promisfied Wrapper around @sap/hdbext

REUSE status

Description

With the standard @sap/hdbext 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 @sap/hdbext 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-hdbext-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-hdbext-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 {
    const dbClass = require("sap-hdbext-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)
} catch (e) {
    console.error(`ERROR: ${err.toString()}`)
}

Methods

The following @sap/hdbext 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)

Requirements / Download and Installation

  • Install Node.js version 12.x or 14.x on your development machine https://nodejs.org/en/download/

  • @sap Node.js packages have moved from https://npm.sap.com to the default registry https://registry.npmjs.org. As future versions of @sap modules are going to be published only there, please make sure to adjust your registry with:

npm config delete @sap:registry
  • Install the code sample as a reusable Node.js module
npm install -g sap-hdbext-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)

Known Issues

None

How to obtain support

This project is provided "as-is": there is no guarantee that raised issues will be answered or addressed in future releases.

License

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.