A Guide to Basic Usage of Oracle Database in a JavaScript/Node.js Application

In this guide, we will explore the basics of using Oracle Database in a JavaScript/Node.js application.

Posted on

Oracle Database is one of the most powerful and widely used relational database management systems (RDBMS) in the world. Leveraging Oracle’s capabilities in a JavaScript/Node.js application can provide robust data storage and retrieval. In this guide, we will explore the basics of using Oracle Database in a JavaScript/Node.js application, including installation, setup, and common database operations.

Prerequisites

Before you begin, make sure you have the following prerequisites in place:

  1. Node.js: Ensure that you have Node.js installed on your system. You can download it from nodejs.org.

  2. Oracle Database: Install Oracle Database if you haven’t already. You can download it from the Oracle Technology Network (OTN).

  3. Oracle Instant Client: To connect to Oracle Database from Node.js, you’ll need the Oracle Instant Client. Download and install the appropriate version for your system from the Oracle Instant Client Downloads page.

  4. Node.js OracleDB Driver: You’ll need a Node.js driver to interact with Oracle Database. Oracle provides a Node.js driver called oracledb. Install it using npm:

    npm install oracledb
    

Connecting to Oracle Database

To start using Oracle Database in your JavaScript/Node.js application, you need to establish a connection to the database. Here’s how you can do it:

const oracledb = require("oracledb")

// Configure the connection
const dbConfig = {
  user: "your_username",
  password: "your_password",
  connectString: "your_connection_string" // Typically in the format: hostname:port/service_name
}

// Establish a connection to the database
oracledb.getConnection(dbConfig, (err, connection) => {
  if (err) {
    console.error("Error connecting to Oracle Database:", err)
    return
  }

  console.log("Connected to Oracle Database!")

  // Perform database operations here

  // Release the connection when done
  connection.close()
})

Replace the user, password, and connectString in dbConfig with your specific Oracle Database credentials and connection details.

Performing Basic Database Operations

Oracle Database allows you to perform various database operations, including querying, inserting, updating, and deleting data. Here are some examples:

Querying Data

// Introduction: Fetching data from a 'employees' table.
connection.execute("SELECT * FROM employees", (err, result) => {
  if (err) {
    console.error("Error querying data:", err)
    return
  }

  // 'result.rows' contains the rows retrieved from the database
  console.log("Query results:", result.rows)
})

Running the SELECT query retrieves all records from the ’employees’ table and provides them in the result.rows array.

Inserting Data

// Introduction: Adding a new employee record to the 'employees' table.
const newEmployee = {
  emp_id: 101,
  emp_name: "John Doe",
  emp_salary: 50000
}

// Insert a new record into the 'employees' table
connection.execute(
  "INSERT INTO employees (emp_id, emp_name, emp_salary) VALUES (:1, :2, :3)",
  [newEmployee.emp_id, newEmployee.emp_name, newEmployee.emp_salary],
  (err, result) => {
    if (err) {
      console.error("Error inserting data:", err)
      return
    }

    console.log("Data inserted successfully!")
  }
)

After executing the INSERT INTO query, a new employee record will be added to the ’employees’ table in the database.

Updating Data

// Introduction: Updating an employee's salary in the 'employees' table.
const updatedSalary = 55000
const empIdToUpdate = 101

// Update an employee's salary
connection.execute(
  "UPDATE employees SET emp_salary = :1 WHERE emp_id = :2",
  [updatedSalary, empIdToUpdate],
  (err, result) => {
    if (err) {
      console.error("Error updating data:", err)
      return
    }

    console.log("Data updated successfully!")
  }
)

Using the UPDATE query, you can modify specific fields within an existing record.

Deleting Data

// Introduction: Deleting an employee record from the 'employees' table.
const empIdToDelete = 101

// Delete an employee record
connection.execute(
  "DELETE FROM employees WHERE emp_id = :1",
  [empIdToDelete],
  (err, result) => {
    if (err) {
      console.error("Error deleting data:", err)
      return
    }

    console.log("Data deleted successfully!")
  }
)

The DELETE FROM query removes a specific employee record from the ’employees’ table.

Handling Errors

Proper error handling is essential in any production application, especially when dealing with databases. Be sure to handle errors gracefully to maintain the stability and reliability of your Node.js application.

Conclusion

Oracle Database is a powerful and versatile RDBMS widely used in enterprise applications. In this guide, we covered the basics of using Oracle Database in a JavaScript/Node.js application, from setting up the connection to performing common database operations. As you become more proficient with Oracle, you can explore its advanced features and optimizations to build robust and scalable applications. With Oracle and Node.js, you have a solid foundation for developing enterprise-level database-driven applications.