Connect to Oracle, Salesforce and others using ODBC drivers from Node.js

Introduction

If you are looking to connect to your databases like SQL Server, Oracle, DB2, Postgres, MongoDB etc., or SaaS apps like Salesforce, Eloqua, Oracle Sales Cloud, Oracle Service Cloud etc., using a ODBC driver from your Node.js application, this is the place to get started. In this tutorial, we will walk you through on how you can connect to Oracle database from Node.js using Progress DataDirect ODBC drivers.

Pre-requisites

Have Node.js installed on your machine

Download and Install DataDirect Oracle ODBC driver

  1. Go to our website and download the 64-bit ODBC driver for the Oracle database.
  2. Extract the package and install the ODBC driver by running the installer.
  3. Now Open ODBC Administrator (64-bit) and Click on Add, to add a new datasource.

    Add new ODBC DSN

  4. Choose “DataDirect 8.0 Oracle Wire Protocol” as your driver

    Choose Oracle Driver

  5. Now fill in the connection information such as hostname, port, SID/Service Name and click on Test Connect to verify connection details.

Install Node.js ODBC package

  1. In your Node.js, you need to install node-odbc package to be able to use ODBC drivers.
  2. To install the package, run
    npm i odbc

Connect to Oracle from Node.js

  1. Following is a sample code on how you can connect to Oracle using Progress DataDirect ODBC driver and query a table. This is just a starter code, you can modify this as per your needs.

    var db = require('odbc')()
        , cn = "Dsn=Oracle;UID=saikrishnabobba;PWD=<password>;"
    //open the connection
    db.open(cn, function (err) {
        if (err) {
          return console.log(err);
        }
    // Run a sample query
         db.query("select * from COUNTRIES", function (err, rows, moreResultSets) {
           
          if (err) {
            return console.log(err);
          }
           
          console.log(rows);
         });
      });


  2. Change DSN, Username, password and query based on your requirements. When you run the above code, it should print the data in your table to console.

You can use similar steps with any of DataDirect ODBC suite of drivers available for Relational, Big Data, SaaS and NoSQL Data sources. Feel free to try any of our drivers with your Node.js apps for connecting to your datasource of your choice.

ODBC TUTORIAL

Connect to Oracle, Salesforce and others using ODBC drivers from Node.js

DataDirect
connectors

Connect any data source to any application

DataDirect
Tutorials

Discussions, tips and tricks for
DataDirect Connect drivers