SAP HANA Database Client for Node.js (hdb)
The `hdb` package provides a pure JavaScript client for connecting to SAP HANA Cloud and SAP HANA Platform servers from Node.js applications. Currently at stable version 2.27.1, the library typically sees patch releases addressing bug fixes, dependency updates, and minor feature enhancements. While `hdb` is fully supported by SAP for existing projects, for new development, SAP strongly encourages the use of the `@sap/hana-client` driver, which offers a more comprehensive feature set including automatic reconnect, connection pooling, and client-side data encryption, features explicitly marked as absent in `hdb`. This differentiation positions `hdb` as a viable client for established applications but a less feature-rich option compared to its C++ based counterpart for new projects.
Common errors
-
Connect error Error: unable to get local issuer certificate
cause The SAP HANA server's TLS/SSL certificate is not trusted by the Node.js client, often due to missing or outdated root CA certificates in the client's trust store.fixEnsure your Node.js environment has the necessary root certificates. You can specify trusted CAs in the `createConnection` options using the `ca` property, or set `sslValidateCertificate: false` (not recommended for production). Upgrading `hdb` to `2.26.3` or higher can help as it includes additional fallback certificates. -
TypeError: hdb.createConnection is not a function
cause Incorrect import of the `hdb` module, leading to `hdb` being `undefined` or not the expected object with the `createConnection` method. This often happens with incorrect named imports in ESM or destructuring in CJS.fixFor ESM, use `import hdb from 'hdb';`. For CommonJS, use `const hdb = require('hdb');`. Ensure you are not attempting to destructure `createConnection` directly if `hdb` only exports it as a property of its default export. -
Error: SQL statement is too large
cause The executed SQL statement or large object (LOB) parameters exceed the default maximum packet size configured for the `hdb` driver.fixIncrease the `packetSize` and `packetSizeLimit` options in the `hdb.createConnection()` configuration. For example: `packetSize: Math.pow(2, 17), packetSizeLimit: Math.pow(2, 20)` to allow larger packets.
Warnings
- breaking The package underwent a major version jump from `0.x` to `2.x`. While specific breaking changes from this transition are not detailed in recent changelogs, such a leap typically indicates significant API overhauls, requiring careful migration for older projects.
- gotcha SAP officially encourages new projects to use the `@sap/hana-client` driver due to its more extensive feature set (e.g., connection pooling, automatic reconnect, client-side data encryption) and full support for SAP HANA server features. `hdb` is maintained but less feature-rich.
- gotcha Users may encounter TLS/SSL connection issues due to outdated or missing root certificates. Recent versions have added fallback certificates (e.g., DigiCert TLS RSA4096 Root G5, DigiCert Global Root G2).
- gotcha An uncaught exception could occur when attempting to close an already closed `ResultSet` object, leading to application crashes.
- gotcha The `util.os_user` function could lead to an uncaught exception in secured Docker container environments due to restrictions on accessing OS user information.
Install
-
npm install hdb -
yarn add hdb -
pnpm add hdb
Imports
- hdb
import { hdb } from 'hdb';import hdb from 'hdb';
- hdb (CommonJS)
const { hdb } = require('hdb');const hdb = require('hdb'); - ConnectionOptions (TypeScript Type)
import { ConnectionOptions } from 'hdb';import type { ConnectionOptions } from 'hdb';
Quickstart
import hdb from 'hdb';
const client = hdb.createConnection({
host: process.env.HANA_HOST ?? 'localhost',
port: parseInt(process.env.HANA_PORT ?? '30015', 10),
user: process.env.HANA_USER ?? 'DBADMIN',
password: process.env.HANA_PASSWORD ?? 'password',
encrypt: true, // Use TLS encryption
sslValidateCertificate: true // Validate server certificate
});
client.on('error', (err) => {
console.error('SAP HANA Client Network Error:', err);
client.end(); // Ensure connection is closed on error
});
client.connect((err) => {
if (err) {
return console.error('SAP HANA Connection Error:', err);
}
console.log('Connected to SAP HANA.');
const sql = 'SELECT CURRENT_USER, CURRENT_SCHEMA FROM DUMMY;';
client.exec(sql, (execErr, rows) => {
if (execErr) {
console.error('SAP HANA Query Execution Error:', execErr);
} else {
console.log('Query result:', rows);
console.log('Current User:', rows[0].CURRENT_USER);
console.log('Current Schema:', rows[0].CURRENT_SCHEMA);
}
client.end(); // Always close the connection
console.log('Connection to SAP HANA closed.');
});
});