2

I am currently working on a web site using Angular, Node.Js, express and an Oracle database.

I'm still not familiar with all this technologies and I'm doing my best! My problem is the Oracle database connection can't be exported. I searched about this and I think it's a promise thing. The only solution I found is to connect to the database every time I use a new SQL query which is impossible for me because the project is big.

Is there any method I can use to make the database connection in a separate file and export it when I need it?

2
  • if you are saying that you want to store the connection string or authentication details of your database yes you can do that easily. Use environment_variable. Commented Jun 2, 2021 at 23:14
  • i want to put all the connection config in one file .js then i just import the connection made and use it with execute function in other files. Commented Jun 3, 2021 at 15:38

1 Answer 1

3

With a multi-user app, you should use a connection pool. You open it at app start, and then access it as needed - you don't have to pass anything around.

If you open the connection pool in your initialization routine:

await oracledb.createPool({
  user: dbConfig.user,
  password: dbConfig.password,
  connectString: dbConfig.connectString
});
console.log('Connection pool started');

then any other module that needs a connection can simply do:

conn = await oracledb.getConnection();
result = await conn.execute(statement, binds, opts);
await conn.close();

Read up on connection pools and sizing and threads in the node-oracledb documentation on Connection Pooling.

Also see the series Build REST APIs for Node.js and its code https://github.com/oracle/oracle-db-examples/tree/master/javascript/rest-api. In particular, look at database.js.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.