Share Oracle database connection in NodeJS for MVC architecture

Viewed 25

I'm quite new with the implementation of Oracle database with Node.js, currently I'm using oracledb library to connect my Node.js app to my Oracle database as follows

connection = await oracledb.getConnection({
  user: "SYS",
  password: password,
  connectString: "localhost:1522/userdb",
  privilege: oracledb.SYSDBA
});

I set everything up as a monolithic app(It's a simple user review app) to start, but I want to split the code in User and Review Controller, the same for the service, model for both objects.

The only problem I have now (That I know of), is that I am not able to make each model (User and Review) to take that connection and simply send a query depending on what the controller needs. I tried to create a separate db.js file and set the connection there and then use

class DBConnection {
  constructor() {
    this._connect();
  }
 async _connect() {
   try {
    connection = await oracledb.getConnection({
      user: "SYS",
      password: password,
      connectString:process.env.DATABASE,
      privilege: oracledb.SYSDBA
    });
   } catch (err) {
    console.log(err.message);
   }
  
  }
}

module.exports = new DBConnection();

Then I would try to import it and use something like:

connection= DBConnection();
connection.execute(query);

But it has not worked, is there any way to do this?

0 Answers
Related