How to use NodeJS with KNEX.js to Access Oracle Database
Below is a cheat sheet install guide:
npm init npm install knex --save npm install oracledb |
Follow the instructions to downloading the appropriate oracle basic client. You’ll need to basically download a basic client zip file, unzip the folder, and set the folder in either your Windows Path (PATH) or your Linux Path (LD_LIBRARY_PATH) More info can be found here: https://oracle.github.io/odpi/doc/installation.html
You’ll probably end up unzipping the basic client to /opt/oracle/instaclient_### or c:\oracle\instaclient##_#
There shouldn’t be any need to install a heavy ODT installer based version.
This guide should support Oracle 10 through Oracle 12 for connecting to Oracle database. This method assumes you only have an SID as well which was my own case and proved to be difficult to determine what to do to resolve it.
Below is a code example of a node js application using knex to connect to an oracle database.
Remember, table names are case sensitive, so you may need to capitalize the table name. (This isn’t necessarily true in older Oracle versions)
console.log( 'Biri.me Oracle Test Code' ); var knex = require( 'knex' )({ client: 'oracledb' , connection: { host: 'host.address.com:1234' , user: 'username' , password: 'password' , database: 'databasename' , instanceName: 'INSTANCE1' , requestTimeout: 100, connectString: '(DESCRIPTION= (ADDRESS_LIST= (ADDRESS=(PROTOCOL=TCP) (HOST=host.address.com)(PORT=1234) ) ) (CONNECT_DATA=(SERVICE_NAME=INSTANCE1) ) )' //expirationChecker?(): boolean; }, fetchAsString: [ 'number' , 'clob' ] }); knex.select( '*' ).from( 'TABLENAME' ).then(result=>console.log(result)). catch (error => console.log(error)); |
Comments