Connect with Oracle DB in Node JS

Let's see how can we connect with oracle db in node js project or lambda

Oracle DB - Education Funda
Oracle DB - Education Funda


First step is to install oracle db package with below command:
~ npm i oracledb

Let's see the code for connecting with oracle db for lambda handler:

const oracledb = require("oracledb");
oracledb.outFormat = oracledb.OUT_FORMAT_OBJECT;

exports.handler = async (event, context, callback) => {
let connection;

const uatConnObj = {
user: "db_user_name",
password: "db_pass",
connectString: "ip:port/db",
poolMax: 1,
poolMin: 0,
poolIncrement: 10,
};
const prodConnObj = {
user: "db_user_name",
password: "db_pass",
connectString: "ip:port/db",
poolMax: 1,
poolMin: 0,
poolIncrement: 10,
};

try {
await oracledb.createPool(prodConnObj);
let dbPool = oracledb.getPool();
console.log("Pool created");
connection = await dbPool.getConnection();
let db;
let query = event.query; // here query which we will pass in lambda paramertes
if (!!query) {
db = await connection.execute(query);
}
return db
} catch (err) {
console.log(err.message);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error('finally error: ',err.message);
}
}
}
};


Same above code we can use in our simple node js project as well, the only different we can remove the Handler function and we can write our custom function and for event.query either we can take in function params or we can pass it statically.

Happy Coding </>

Follow me here in LinkedIn.

Comments

Popular posts from this blog

JavaScript Logical Output Based Interview Questions

Create and Deploy AWS SAM Application

Deploy Angular Build With Express JS Project