To simplify your querying from ORACLE-DB, you may want to call your query like this:
const oracle = require('./oracle.js');
const sql = "select 'test' as c1, 'oracle' as c2 from dual";
oracle.queryObject(sql, {}, {})
.then(function(result) {
console.log(result.rows[0]['C2']);
})
.catch(function(err) {
next(err);
});
Building up the connection and executing is included in this oracle.js file with content as follows:
'use strict';
const oracledb = require('oracledb');
const oracleDbRelease = function(conn) {
conn.release(function (err) {
if (err)
console.log(err.message);
});
};
function queryArray(sql, bindParams, options) {
options.isAutoCommit = false; // we only do SELECTs
return new Promise(function(resolve, reject) {
oracledb.getConnection(
{
user : "oli",
password : "password",
connectString : "ORACLE_DEV_DB_TNA_NAME"
})
.then(function(connection){
//console.log("sql log: " + sql + " params " + bindParams);
connection.execute(sql, bindParams, options)
.then(function(results) {
resolve(results);
process.nextTick(function() {
oracleDbRelease(connection);
});
})
.catch(function(err) {
reject(err);
process.nextTick(function() {
oracleDbRelease(connection);
});
});
})
.catch(function(err) {
reject(err);
});
});
}
function queryObject(sql, bindParams, options) {
options['outFormat'] = oracledb.OBJECT; // default is oracledb.ARRAY
return queryArray(sql, bindParams, options);
}
module.exports = queryArray;
module.exports.queryArray = queryArray;
module.exports.queryObject = queryObject;
Note that you have both methods queryArray and queryObject to call on your oracle object.