Closing connections and returning results using node-oracledb

If you’re using the npm module node-oracledb to connect to an Oracle database from Node, consider using this Promise-based and cursor-based wrapper/utility to return results from your queries and close connections:

This wrapper provides the following:

  • Only one function to call – executeSQL()
    • Pass in your SQL or stored procedure and any connection parameters
  • Promise-based, so chain off executeSQL() to return your execution results or catch any errors
  • Automatically closes the connection to Oracle and the result set returned from the database so no need to worry about memory leaks

I wrote this for a few reasons, the primary being a separation of concerns.  Instead of the calling code having to worry about getting the database rows from the cursor, checking for empty sets, closing the result set, and closing the connection to the database, this is all wrapped up in one nice function that handles this automatically for you.  Your code won’t be littered with node-oracledb module-specific code when all you want to do is get results back from the database.  Also, it’s very easy and common to get memory leaks when your result sets and connections aren’t closed so this prevents that.