Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
@sap/hana-client
Advanced tools
@sap/hana-client is an npm package that provides a client interface for connecting to and interacting with SAP HANA databases. It allows developers to execute SQL queries, manage database connections, and handle transactions programmatically.
Connecting to SAP HANA
This feature allows you to establish a connection to an SAP HANA database using the provided connection parameters.
const hana = require('@sap/hana-client');
const conn = hana.createConnection();
const connParams = {
serverNode: 'myServer:30015',
uid: 'myUser',
pwd: 'myPassword'
};
conn.connect(connParams, (err) => {
if (err) throw err;
console.log('Connected to SAP HANA');
});
Executing SQL Queries
This feature allows you to execute SQL queries against the connected SAP HANA database and retrieve the results.
const sql = 'SELECT * FROM MY_TABLE';
conn.exec(sql, (err, result) => {
if (err) throw err;
console.log(result);
});
Handling Transactions
This feature allows you to manage transactions by explicitly committing or rolling back changes to the database.
conn.setAutoCommit(false);
conn.exec('INSERT INTO MY_TABLE (ID, NAME) VALUES (1, 'John Doe')', (err) => {
if (err) {
conn.rollback();
throw err;
}
conn.commit((err) => {
if (err) throw err;
console.log('Transaction committed');
});
});
The 'mysql' package provides a client interface for connecting to MySQL databases. It offers similar functionalities such as executing SQL queries, managing connections, and handling transactions. However, it is specific to MySQL databases, whereas @sap/hana-client is specific to SAP HANA.
The 'pg' package is a PostgreSQL client for Node.js. It allows developers to connect to PostgreSQL databases, execute SQL queries, and manage transactions. Like @sap/hana-client, it provides a robust set of features for database interaction, but it is tailored for PostgreSQL.
The 'mssql' package is a Microsoft SQL Server client for Node.js. It provides functionalities for connecting to SQL Server databases, executing queries, and handling transactions. It is similar to @sap/hana-client in terms of features but is designed for use with SQL Server.
Fully-featured Node.js driver for SAP HANA. It is used to connect, issue SQL queries, and obtain result sets.
npm install @sap/hana-client
This driver communicates with the native HANA libraries, and thus requires platform-specific native binaries. The official hosted version includes precompiled libraries for Linux, Windows and Mac OS X.
The @sap/hana-client driver supports versions of Node.js 16 and higher.
SAP Community provides a forum where you can ask and answer questions, and comment and vote on the questions of others and their answers.
See SAP HANA Community Questions for details.
The SAP HANA Node.js Driver help guide and API reference can be found on help.sap.com.
var hana = require('@sap/hana-client');
var conn = hana.createConnection();
var conn_params = {
serverNode : 'myserver:30015',
uid : 'system',
pwd : 'Password123'
};
conn.connect(conn_params, function(err) {
if (err) throw err;
conn.exec('SELECT Name, Description FROM Products WHERE id = ?', [301], function (err, result) {
if (err) throw err;
console.log('Name: ', result[0].Name, ', Description: ', result[0].Description);
// output --> Name: Tee Shirt, Description: V-neck
conn.disconnect();
})
});
A database connection object is created by calling createConnection
. The
connection is established by calling the connection object's connect
method,
and passing in an object representing connection parameters.
conn.connect({
host : 'myserver',
port : '30015',
uid : 'system',
pwd : 'Password123'
});
conn.disconnect(function(err) {
if (err) throw err;
console.log('Disconnected');
});
Direct statement execution is the simplest way to execute SQL statements. The inputs are the SQL command to be executed, and an optional array of positional arguments. The result is returned using callbacks. The type of returned result depends on the kind of statement.
In the case of a successful DDL Statement, nothing is returned.
conn.exec('CREATE TABLE Test (id INTEGER PRIMARY KEY, msg VARCHAR(128))', function (err, result) {
if (err) throw err;
console.log('Table Test created!');
});
In the case of a DML Statement the number of affectedRows
is returned.
conn.exec("INSERT INTO Test VALUES(1, 'Hello')", function (err, affectedRows) {
if (err) throw err;
console.log('Number of affected rows:', affectedRows);
});
The exec
function is a convenient way to completely retrieve the result of a
query. In this case all selected rows are fetched and returned in the callback.
conn.exec("SELECT * FROM Test WHERE id < 5", function (err, rows) {
if (err) throw err;
console.log('Rows:', rows);
});
Values in the query can be substitued with JavaScript variables by using ?
placeholders in the query, and passing an array of positional arguments.
conn.exec("SELECT * FROM Test WHERE id BETWEEN ? AND ?", [5, 8], function (err, rows) {
if (err) throw err;
console.log('Rows:', rows);
});
The connection returns a statement
object which can be executed multiple times.
conn.prepare('SELECT * FROM Test WHERE id = ?', function (err, stmt){
if (err) throw err;
// do something with the statement
});
The execution of a prepared statement is similar to the direct statement execution.
The first parameter of exec
function is an array with positional parameters.
stmt.exec([16], function(err, rows) {
if (err) throw err;
console.log("Rows: ", rows);
});
The execution of a prepared batch statement is similar to the direct statement execution.
The first parameter of execBatch
function is an array with positional parameters.
var stmt=conn.prepare("INSERT INTO Customers(ID, NAME) VALUES(?, ?)");
stmt.execBatch([[1, 'Company 1'], [2, 'Company 2']], function(err, rows) {
if (err) throw err;
console.log("Rows: ", rows);
});
The execution of a prepared query is similar to the direct statement execution.
The first parameter of execQuery
function is an array with positional parameters.
var stmt=conn.prepare("SELECT * FROM Customers WHERE ID >= ? AND ID < ?");
stmt.execQuery([100, 200], function(err, rs) {
if (err) throw err;
var rows = [];
while (rs.next()) {
rows.push(rs.getValues());
}
console.log("Rows: ", rows);
});
stmt.drop(function(err) {
if (err) throw err;
});
Transactions are automatically commited. Setting autocommit to false implicitly starts a new transaction that must be explicitly committed, or rolled back.
conn.setAutoCommit(false);
// Execute some statements
conn.commit(function(err) {
if (err) throw err;
console.log('Transaction commited.');
});
conn.setAutoCommit(false);
// Execute some statements
conn.rollback(function(err) {
if (err) throw err;
console.log('Transaction rolled back.');
});
The HANA Node.js Driver is provided via the SAP Developer License Agreement.
By using this software, you agree that the following text is incorporated into the terms of the Developer Agreement:
If you are an existing SAP customer for On Premise software, your use of this current software is also covered by the terms of your software license agreement with SAP, including the Use Rights, the current version of which can be found at: https://www.sap.com/about/agreements/product-use-and-support-terms.html?tag=agreements:product-use-support-terms/on-premise-software/software-use-rights
FAQs
Official SAP HANA Node.js Driver
The npm package @sap/hana-client receives a total of 117,249 weekly downloads. As such, @sap/hana-client popularity was classified as popular.
We found that @sap/hana-client demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.