What is oracledb?
The oracledb npm package is a Node.js driver for Oracle Database, providing a way to connect to Oracle databases, execute SQL queries, and manage database transactions. It supports various features such as connection pooling, data types, and advanced Oracle Database features.
What are oracledb's main functionalities?
Connecting to Oracle Database
This code demonstrates how to establish a connection to an Oracle Database using the oracledb package. It includes error handling and ensures the connection is closed after use.
const oracledb = require('oracledb');
async function run() {
let connection;
try {
connection = await oracledb.getConnection({
user: 'your_username',
password: 'your_password',
connectString: 'localhost/XEPDB1'
});
console.log('Connection was successful!');
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
run();
Executing SQL Queries
This code sample shows how to execute a SQL query using the oracledb package. It retrieves rows from the 'employees' table where the department_id is 101.
const oracledb = require('oracledb');
async function run() {
let connection;
try {
connection = await oracledb.getConnection({
user: 'your_username',
password: 'your_password',
connectString: 'localhost/XEPDB1'
});
const result = await connection.execute(
`SELECT * FROM employees WHERE department_id = :id`,
[101] // bind value for :id
);
console.log(result.rows);
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
run();
Using Connection Pooling
This code demonstrates how to use connection pooling with the oracledb package. It creates a pool of connections, retrieves a connection from the pool, executes a query, and then closes the connection and the pool.
const oracledb = require('oracledb');
async function run() {
let pool;
try {
pool = await oracledb.createPool({
user: 'your_username',
password: 'your_password',
connectString: 'localhost/XEPDB1',
poolMin: 2,
poolMax: 10,
poolIncrement: 1
});
const connection = await pool.getConnection();
const result = await connection.execute(`SELECT * FROM employees`);
console.log(result.rows);
await connection.close();
} catch (err) {
console.error(err);
} finally {
if (pool) {
try {
await pool.close(0);
} catch (err) {
console.error(err);
}
}
}
}
run();
Other packages similar to oracledb
mysql
The mysql package is a Node.js driver for MySQL databases. It provides similar functionalities to oracledb, such as connecting to the database, executing queries, and managing transactions. However, it is specifically designed for MySQL databases and does not support Oracle-specific features.
pg
The pg package is a Node.js driver for PostgreSQL databases. Like oracledb, it allows you to connect to the database, execute SQL queries, and manage transactions. It is tailored for PostgreSQL and includes features specific to that database system, but it does not support Oracle Database features.
mssql
The mssql package is a Node.js driver for Microsoft SQL Server. It offers functionalities similar to oracledb, such as database connections, query execution, and transaction management. It is designed for use with SQL Server and includes features specific to that database, but it does not support Oracle Database features.
node-oracledb version 6.7.0
The node-oracledb add-on for Node.js powers high performance Oracle Database
applications. Applications can be written in TypeScript, or directly in
JavaScript.
Use node-oracledb 6.7.0 to connect Node.js 14.6, or later, to Oracle
Database. Older versions of node-oracledb may work with older versions of
Node.js.
Node-oracledb supports basic and advanced features of Oracle Database
and Oracle Client. See the homepage for a list.
The node-oracledb module is open source and maintained by Oracle Corp.
It is stable, well documented, and has a comprehensive test suite.
Installation
See Getting Started with Node-oracledb and Quick Start Node-oracledb Installation.
Documentation
See Documentation for the Oracle Database Node.js Add-on and the release
notes.
Examples
See the examples directory. Start with examples/example.js.
Help
Questions about node-oracledb can be posted on GitHub or Slack (link
to join Slack).
Tests
To run the test suite see test/README.
Contributing
This project welcomes contributions from the community. Before submitting a
pull request, please review our contribution guide.
Security
Please consult the security guide for our responsible security
vulnerability disclosure process.
License
Copyright (c) 2015, 2024, Oracle and/or its affiliates.
This software is dual-licensed to you under the Universal Permissive License
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
either license.
If you elect to accept the software under the Apache License, Version 2.0,
the following applies:
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.