Using IBM DB2 from node.js
Node.js is an asynchronous, event-driven I/O based server side java script environment for writing scalable internet applications. It is one of the favorite server side environments that is being adopted today.
In this post, I will layout how one can connect to DB2 from node and take you through the setup of a node environment with DB2 that includes running a test program to connect to the sample database and retrieve the rows returned.
The high-level architecture consists of the layers as shown in the figure below.
- ibm_db node module: The asynchronous/synchronous node interface to IBM DB2 and IBM Informix Data servers [https://npmjs.org/package/ibm_db]
- IBM Data Server Driver for ODBC and CLI : IBM implementation of the ODBC interface
- IBM DB2
The node application uses the ibm_db node module to make calls to IBM DB2 database. When the node application needs to make a call to DB2, it invokes the ibm_db node module API. ibm_db node module takes the request and processes it using a separate background thread thereby immediately releasing the node thread to service other requests. When the thread, on which the DB2 call was invoked finishes, an event is emitted that is recognized by node and execution of the code that made the DB2 call is continued.
Setting up DB2 with Node stack
The step-by-step procedure of setting up the stack is below. All the steps below have been tested on a Ubuntu 64-bit platform. If you are on a different platform, you will need to adapt the commands accordingly.
Step 1: Install node.js
a) Download the node.js binaries for your platform from http://nodejs.org/download/ . In this example I will assume you have downloaded it to folder name ‘node_work’ in your $HOME
b) Untar the installed archive
$ cd $HOME/node_work
$ tar -xf node-v0.10.24-linux-x64.tar.gz
c) Set PATH environment variable to the bin directory under the unzipped archive
$ export PATH=$HOME/node_work/ node-v0.10.24-linux -x64/bin:$PATH
Step 2: Install IBM Data Server Driver Package
a) Download IBM Data Server Driver from http://www-01.ibm.com/support/docview.wss?uid=swg27016878 to $HOME/node_work b) Untar the archive
$ tar -xf v10.5fp2_linuxx64_dsdriver.tar.gz
c) Run the command installDSDriver
Step 3: Install ibm_db node module
a) Set the environment variables IBM_DB_HOME to IBM Data Server Driver installation location
$ export IBM_DB_HOME=$HOME/dsdriver
b) Issue the npm command to install the ibm_db node module
$ npm install ibm_db
With this the DB2 with node stack is setup and we are ready to run our sample program.
Running a node program that connects to DB2
Step 1: Source the DSDriver profile before running a node program in the terminal where program is run
$ source $HOME/dsdriver/db2profile
Step 3: Edit the file nodedb2test .js. Replace username, password, hostname and portNumber to the relevant values for your DB2 server.
Step 4: Execute the script, nodedb2test.js, as shown below. The program will output the names of the employees present in the employee table as shown below.
$ node nodedb2test.js
Test program to access DB2 sample database
The comments in the nodedb2test.js explain what each line of code does.
Refer to the node-ibm_db homepage https://npmjs.org/package/ibm_db for more details.