This tutorial shows you how to connect Node.js applications to Oracle Autonomous Database (ADB) using the node-oracledb module. This module lets you quickly develop applications that execute SQL or PL/SQL statements. Your applications can also use Oracle's document storage SODA calls. Node-oracledb can be used with TypeScript or directly with Node.js.
If you would like to use a local database instead, then see the Developing Node.js Applications for Oracle Database tutorial.
This tutorial shows you how use the Node.js node-oracledb interface on Windows to connect applications to Oracle Autonomous Database (ADB). Follow the steps below.
Click on the links below to walk through these steps if you have not yet provisioned an ADB instance. Remember the ADMIN password. You will use this to connect Node.js to ADB.
You may select the Always Free option to use the Always Free Oracle ADB. Choose the Shared Infrastructure deployment type.
Below are the instructions to download the client credentials from the Oracle Cloud Console. The credentials give mutual TLS, providing enhanced security for authentication and encryption.
A new window will appear. Click the Download Wallet button.
Enter a wallet password in the Password field and confirm the password in the Confirm password field. Then, click the Download button. The password must be at least 8 characters long and include at least 1 letter and either 1 numeric character or 1 special character. Although not required for Node.js, this password protects the downloaded client credentials wallet.
Download the free Oracle Instant Client Basic zip file from Oracle Instant Client for Microsoft Windows (x64) 64-bit. (If your Node.js is 32-bit, then you will need to download the 32-bit Basic package from here instead). Remember to install the matching VS Redistributable, as shown on the download page.
Extract the libraries to an accessible directory, for example the libraries could be in C:\oracle\instantclient_19_11
Make a network\admin sub-directory in your Instant Client directory, for example C:\oracle\instantclient_19_11\network\admin.
Unzip the previously obtained credentials zip file and move the extracted files to the new network\admin sub-directory. Wallet files including network\admin\tnsnames.ora should exist.
Install Node.js by downloading the MSI package, clicking it, and following the prompts.
Restart terminal windows, if necessary, so that the new Node.js binary is found.
Using your favorite editor, create a new file package.json in a directory of your choice. It should contain:
{
"name": "Demo",
"version": "1.0.0",
"private": true,
"description": "Demo app",
"keywords": [
"myapp"
],
"dependencies": {
"oracledb": "^5.1.0"
},
"author": "You",
"license": "MIT"
}
Install node-oracledb:
npm install
For further assistance and options, such as for installing behind an HTTP proxy, see Node-oracledb Installation on Microsoft Windows.
Use your editor to create a new Node.js file example.js in the same directory as package.json:
const oracledb = require('oracledb');
oracledb.initOracleClient({ libDir: 'C:\\oracle\\instantclient_19_11' });
async function run() {
let connection;
try {
connection = await oracledb.getConnection({ user: "admin", password: "XXXX", connectionString: "XXX_high" });
// Create a table
await connection.execute(`begin
execute immediate 'drop table nodetab';
exception when others then if sqlcode <> -942 then raise; end if;
end;`);
await connection.execute(`create table nodetab (id number, data varchar2(20))`);
// Insert some rows
const sql = `INSERT INTO nodetab VALUES (:1, :2)`;
const binds =
[ [1, "First" ],
[2, "Second" ],
[3, "Third" ],
[4, "Fourth" ],
[5, "Fifth" ],
[6, "Sixth" ],
[7, "Seventh" ] ];
await connection.executeMany(sql, binds);
// connection.commit(); // uncomment to make data persistent
// Now query the rows back
const result = await connection.execute(`SELECT * FROM nodetab`);
console.dir(result.rows, { depth: null });
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
run();
Run the app:
node example.js
You will see the queried rows returned from the database. Congratulations! You have successfully used Oracle Autonomous Database.
Note: If you connect to ADB from behind a firewall, you will likely encounter a connection timeout error. Update the tnsnames.ora file to use an HTTP proxy. Learn how to do this in this ADB documentation section. Scroll down to the "Connections with an HTTP Proxy" section on the doc page.
More information and resources on using node-oracledb are available here.
This tutorial shows you how use the Node.js node-oracledb interface on macOS (Intel x86) to connect applications to Oracle Autonomous Database (ADB). Follow the steps below.
Click on the links below to walk through these steps if you have not yet provisioned an ADB instance. Remember the ADMIN password. You will use this to connect Node.js to ADB.
You may select the Always Free option to use the Always Free Oracle ADB. Choose the Shared Infrastructure deployment type.
Below are the instructions to download the client credentials from the Oracle Cloud Console. The credentials give mutual TLS, providing enhanced security for authentication and encryption.
A new window will appear. Click the Download Wallet button.
Enter a wallet password in the Password field and confirm the password in the Confirm password field. Then, click the Download button. The password must be at least 8 characters long and include at least 1 letter and either 1 numeric character or 1 special character. Although not required for Node.js, this password protects the downloaded client credentials wallet.
Download the free Oracle Instant Client Basic DMG file from Instant Client Downloads for macOS (Intel x86).
Mount the DMG and run its install_ic.sh script. More details are in the Instant Client installation instructions.
Move the credentials zip file to the network/admin sub-directory of your Instant Client directory and unzip it. For example:
mv Wallet_*.zip $HOME/Downloads/instantclient_19_8/network/admin
cd $HOME/Downloads/instantclient_19_8/network/admin
unzip Wallet_*.zip
Wallet files including network/admin/tnsnames.ora should exist.
{
"name": "Demo",
"version": "1.0.0",
"private": true,
"description": "Demo app",
"keywords": [
"myapp"
],
"dependencies": {
"oracledb": "^5.1.0"
},
"author": "You",
"license": "MIT"
}
Install node-oracledb:
npm install
For further assistance and options, such as for installing behind an HTTP proxy, see Node-oracledb Installation on Apple macOS.
Use your editor to create a new Node.js file example.js in the same directory as package.json:
const oracledb = require('oracledb');
oracledb.initOracleClient({ libDir: process.env.HOME + '/Downloads/instantclient_19_8' });
async function run() {
let connection;
try {
connection = await oracledb.getConnection({ user: "admin", password: "XXXX", connectionString: "XXX_high" });
// Create a table
await connection.execute(`begin
execute immediate 'drop table nodetab';
exception when others then if sqlcode <> -942 then raise; end if;
end;`);
await connection.execute(`create table nodetab (id number, data varchar2(20))`);
// Insert some rows
const sql = `INSERT INTO nodetab VALUES (:1, :2)`;
const binds =
[ [1, "First" ],
[2, "Second" ],
[3, "Third" ],
[4, "Fourth" ],
[5, "Fifth" ],
[6, "Sixth" ],
[7, "Seventh" ] ];
await connection.executeMany(sql, binds);
// connection.commit(); // uncomment to make data persistent
// Now query the rows back
const result = await connection.execute(`SELECT * FROM nodetab`);
console.dir(result.rows, { depth: null });
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
run();
Run the app:
node example.js
You will see the queried rows returned from the database. Congratulations! You have successfully used Oracle Autonomous Database.
Note: If you connect to ADB from behind a firewall, you will likely encounter a connection timeout error. Update the tnsnames.ora file to use an HTTP proxy. Learn how to do this in this ADB documentation section. Scroll down to the "Connections with an HTTP Proxy" section on the doc page.
More information and resources on using node-oracledb are available here.
This tutorial shows you how use the Node.js node-oracledb interface on Linux to connect applications to Oracle Autonomous Database (ADB). Follow the steps below.
Click on the links below to walk through these steps if you have not yet provisioned an ADB instance. Remember the ADMIN password. You will use this to connect Node.js to ADB.
You may select the Always Free option to use the Always Free Oracle ADB. Choose the Shared Infrastructure deployment type.
Below are the instructions to download the client credentials from the Oracle Cloud Console. The credentials give mutual TLS, providing enhanced security for authentication and encryption.
A new window will appear. Click the Download Wallet button.
Enter a wallet password in the Password field and confirm the password in the Confirm password field. Then, click the Download button. The password must be at least 8 characters long and include at least 1 letter and either 1 numeric character or 1 special character. Although not required for Node.js, this password protects the downloaded client credentials wallet.
Install Instant Client, for example on Oracle Linux 7:
sudo yum install oracle-instantclient-release-el7
sudo yum install oracle-instantclient-basic
For other Linux flavors, install the Instant Client ZIP files and follow the instructions from the download page: Instant Client for Linux x86-64 (64-bit). If you use ZIP files, make sure to run ldconfig or set LD_LIBRARY_PATH as shown in the instructions.
Move the credentials zip file to the network/admin sub-directory of your Instant Client directory and unzip it. For example:
sudo cp Wallet_*.zip /usr/lib/oracle/21/client64/lib/network/admin/
sudo sh -c 'cd /usr/lib/oracle/21/client64/lib/network/admin/ && unzip -B Wallet_*.zip'
Wallet files including network/admin/tnsnames.ora should exist.
Install Node.js. For example, on Oracle Linux 7:
sudo yum install oracle-nodejs-release-el7
sudo yum install nodejs
For generic installation steps, see Node.js Downloads.
{
"name": "Demo",
"version": "1.0.0",
"private": true,
"description": "Demo app",
"keywords": [
"myapp"
],
"dependencies": {
"oracledb": "^5.1.0"
},
"author": "You",
"license": "MIT"
}
Install node-oracledb:
npm install
For further assistance and options, such as for installing behind an HTTP proxy, see Node-oracledb Installation on Linux.
Use your editor to create a new Node.js file example.js in the same directory as package.json:
const oracledb = require('oracledb');
async function run() {
let connection;
try {
connection = await oracledb.getConnection({ user: "admin", password: "XXXX", connectionString: "XXX_high" });
// Create a table
await connection.execute(`begin
execute immediate 'drop table nodetab';
exception when others then if sqlcode <> -942 then raise; end if;
end;`);
await connection.execute(`create table nodetab (id number, data varchar2(20))`);
// Insert some rows
const sql = `INSERT INTO nodetab VALUES (:1, :2)`;
const binds =
[ [1, "First" ],
[2, "Second" ],
[3, "Third" ],
[4, "Fourth" ],
[5, "Fifth" ],
[6, "Sixth" ],
[7, "Seventh" ] ];
await connection.executeMany(sql, binds);
// connection.commit(); // uncomment to make data persistent
// Now query the rows back
const result = await connection.execute(`SELECT * FROM nodetab`);
console.dir(result.rows, { depth: null });
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
run();
Run the app:
node example.js
You will see the queried rows returned from the database. Congratulations! You have successfully used Oracle Autonomous Database.
Note: If you connect to ADB from behind a firewall, you will likely encounter a connection timeout error. Update the tnsnames.ora file to use an HTTP proxy. Learn how to do this in this ADB documentation section. Scroll down to the "Connections with an HTTP Proxy" section on the doc page.
More information and resources on using node-oracledb are available here.