Node.js Series: Connecting to Oracle DB

Hi!

[Versão em Português]

This is part 4 of my Node.js series, do checkout the previous posts and let me know what you think!

So we knew it would come to this, connection to Oracle DB. First of all, it does work, so what I am going to do here is the first part of the post is telling you how you do it, second part is my overall experience with this and what you should expect being your next moves. Let’s get to it then:

Acquiring the tools

Oracle provides you a mostly open-source module for your connection right here. And it works for Node 6, 8 and 10, so make sure you check your installation first. I used it with Node.JS version 10.7.0 and it worked fine.

Even though the installation process is a bit tricky, it is well documented at their github page. Keep in mind it demands the installation of Oracle client libraries into your running machine, so be warned that a simple npm i oracledb will not do!

So here is my example code:


var oracledb = require('oracledb');

oracledb.getConnection(
  {
    user          : "usr",
    password      : "pwd",
    connectString : "host:port/orcl"
  },
  function(err, connection) {
    if (err) {
      console.error(err.message);
      return;
    }
    connection.execute(
      `SELECT *
       FROM dual`,
      [],  
      function(err, result) {
        if (err) {
          console.error(err.message);
          doRelease(connection);
          return;
        }
        console.log(result.rows);
        doRelease(connection);
      });
  });

function doRelease(connection) {
  connection.close(
    function(err) {
      if (err)
        console.error(err.message);
    });
}


This code is basically a copy of the code the can be found on their github page, as you can see, there is a lot of room for improvement braking this code into components, so keep that in mind when building your production ready version.

The code above did not work the first time though, I got thrown an error:

ORA-24454: client host name is not set

All you have to do is run this on you Linux console:

sudo /bin/bash -c "echo '127.0.1.1 ${HOSTNAME}' >> /etc/hosts"

So yeah, this all I had to do in order to get this Oracle DB thing going.

It does work, BUT…

I think this solution is incomplete. As promised that is the second part with my personal opinion on the solution.

We are right now living in an environment where the high tendency is not even knowing the machine your code is running at, so offering a solution that needs local machine config in order to connect is questionable in the very least. I thought we had this figured out by the time JDBC thin clients were born. Most of Node.js customers out there are planning if not already deployed their first versions on serverless cloud providers, so it is my belief it should be reviewed.

My personal recommendation here is you should by now really start planning the usage of intermediary NoSQL databases databases like MongoDB instead of going straight to the SQL database. There are a LOT of advantages on the performance and even security realms to this strategy which we may even talk about in future post.

There are still lots to talk about on Node.js and new things are coming everyday, please follow me on twitter @vrsbrazil to follow this series and more.

Thank you!

Advertisements

One thought on “Node.js Series: Connecting to Oracle DB”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s