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.
Follow the steps below to walk through the process of creating an ADB instance if you do not already have access to one. Remember the ADMIN password. You will use this to connect Node.js applications to Oracle ADB.
Navigate to the Autonomous Database creation pane and click 'Create Autonomous Database':
Screenshot of Oracle Autonomous Cloud database creation
Choose a database name, for example MYCLOUDDB. Select Transaction Processing, Data Warehouse, or JSON on Shared Infrastructure deployment type:
Screenshot of Oracle Autonomous Cloud database creation
Set the database ADMIN user password:
Screenshot of Oracle Autonomous Cloud database creation button
Allow access from an allowed IP address, and add your IP address:
Screenshot of Oracle Autonomous Cloud database creation button
If your IP address changes in future, you will need to update the database service's access control list.
The 'Secure access from everywhere' option will use mTLS with a wallet which is not covered in this quickstart. Refer to the node-oracledb driver documentation if you need to use it because adding your IP address is not possible.
You may select the 'Always Free' option to use the Always Free Oracle ADB. This will setup your Oracle ADB Shared Infrastructure (ADB-S) database instance.
Once you login to your Oracle Cloud account, navigate to the ADB-S instance details page for the Autonomous Database created in Step 1.
When the database has been created, select the DB Connection button:
Screenshot of Oracle Autonomous Cloud database creation button
Copy any connection string. Important: Make sure to select "TLS" in the authentication drop down before you copy it.
Screenshot of Oracle Autonomous Cloud database connect string page
For example your connection string may be like:
(description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1521)(host=adb.abc.oraclecloud.com))(connect_data=(service_name=myclouddb_low.adb.oraclecloud.com))(security=(ssl_server_dn_match=yes)))
Now paste and store the connection string in any file, say connstring.txt.
Install Node.js (14.6 or later versions) 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"
],
"author": "You",
"license": "MIT",
"dependencies": {
"oracledb": "^6.0"
}
} 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');
async function runApp()
{
let connection;
try {
// Use the connection string copied from the cloud console
// and stored in connstring.txt file from Step 2 of this tutorial
connection = await oracledb.getConnection({ user: "admin", password: "XXXX", connectionString: "(description=...)" });
// 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);
}
}
}
}
runApp(); Run the app:
node example.jsYou 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 connection string to use an HTTP proxy. Learn how to do this in the 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 to connect applications to Oracle Autonomous Database (ADB). Follow the steps below.
Follow the steps below to walk through the process of creating an ADB instance if you do not already have access to one. Remember the ADMIN password. You will use this to connect Node.js applications to Oracle ADB.
Navigate to the Autonomous Database creation pane and click 'Create Autonomous Database':
Screenshot of Oracle Autonomous Cloud database creation button
Choose a database name, for example MYCLOUDDB. Select Transaction Processing, Data Warehouse, or JSON on Shared Infrastructure deployment type:
Screenshot of Oracle Autonomous Cloud database creation button
Set the database ADMIN user password:
Screenshot of Oracle Autonomous Cloud database creation button
Allow access from an allowed IP address, and add your IP address:
Screenshot of Oracle Autonomous Cloud database creation button
If your IP address changes in future, you will need to update the database service's access control list.
The 'Secure access from everywhere' option will use mTLS with a wallet which is not covered in this quickstart. Refer to the node-oracledb driver documentation if you need to use it because adding your IP address is not possible.
You may select the 'Always Free' option to use the Always Free Oracle ADB. This will setup your Oracle ADB Shared Infrastructure (ADB-S) database instance.
Once you login to your Oracle Cloud account, navigate to the ADB-S instance details page for the Autonomous Database created in Step 1.
When the database has been created, select the DB Connection button:
Screenshot of Oracle Autonomous Cloud database creation
Copy any connection string. Important: Make sure to select "TLS" in the authentication drop down before you copy it.
Screenshot of Oracle Autonomous Cloud database connect string page
For example your connection string may be like:
(description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1521)(host=adb.abc.oraclecloud.com))(connect_data=(service_name=myclouddb_low.adb.oraclecloud.com))(security=(ssl_server_dn_match=yes)))
Now paste and store the connection string in any file, say connstring.txt.
{
"name": "Demo",
"version": "1.0.0",
"private": true,
"description": "Demo app",
"keywords": [
"myapp"
],
"author": "You",
"license": "MIT",
"dependencies": {
"oracledb": "^6.0"
}
} 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');
async function runApp()
{
let connection;
try {
// Use the connection string copied from the cloud console
// and stored in connstring.txt file from Step 2 of this tutorial
connection = await oracledb.getConnection({ user: "admin", password: "XXXX", connectionString: "(description=...)" });
// 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);
}
}
}
}
runApp(); Run the app:
node example.jsYou 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 connection string 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.
Follow the steps below to walk through the process of creating an ADB instance if you do not already have access to one. Remember the ADMIN password. You will use this to connect Node.js applications to Oracle ADB.
Navigate to the Autonomous Database creation pane and click 'Create Autonomous Database':
Screenshot of Oracle Autonomous Cloud database creation button
Choose a database name, for example MYCLOUDDB. Select Transaction Processing, Data Warehouse, or JSON on Shared Infrastructure deployment type:
Screenshot of Oracle Autonomous Cloud database creation button
Set the database ADMIN user password:
Screenshot of Oracle Autonomous Cloud database creation button
Allow access from an allowed IP address, and add your IP address:
Screenshot of Oracle Autonomous Cloud database creation button
If your IP address changes in future, you will need to update the database service's access control list.
The 'Secure access from everywhere' option will use mTLS with a wallet which is not covered in this quickstart. Refer to the node-oracledb driver documentation if you need to use it because adding your IP address is not possible.
You may select the 'Always Free' option to use the Always Free Oracle ADB. This will setup your Oracle ADB Shared Infrastructure (ADB-S) database instance.
Once you login to your Oracle Cloud account, navigate to the ADB-S instance details page for the Autonomous Database created in Step 1.
When the database has been created, select the DB Connection button:
Screenshot of Oracle Autonomous Cloud database creation button
Copy any connection string. Important: Make sure to select "TLS" in the authentication drop down before you copy it.
Screenshot of Oracle Autonomous Cloud database connect string page
For example your connection string may be like:
(description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1521)(host=adb.abc.oraclecloud.com))(connect_data=(service_name=myclouddb_low.adb.oraclecloud.com))(security=(ssl_server_dn_match=yes)))
Now paste and store the connection string in any file, say connstring.txt.
Install Node.js version 14.6 or later. 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"
],
"author": "You",
"license": "MIT",
"dependencies": {
"oracledb": "^6.0"
}
} 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 runApp()
{
let connection;
try {
// Use the connection string copied from the cloud console
// and stored in connstring.txt file from Step 2 of this tutorial
connection = await oracledb.getConnection({ user: "admin", password: "XXXX", connectionString: "(description=...)" });
// 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);
}
}
}
}
runApp(); Run the app:
node example.jsYou 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 connection string to use an HTTP proxy. Learn how to do this in the 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.