Skip to content

Commit 593a912

Browse files
JuanVillegas95LeonSilva15
authored andcommitted
refactor(db): internalize pool initialization within getConnection
getConnection now handles pool creation internally, eliminating the need for external init calls.
1 parent 148089a commit 593a912

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

templates/app/utils/db/index.cjs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
** All rights reserved
55
** Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
66
*/
7+
8+
// app/server/utils/db/config.cjs
79
const oracledb = require('oracledb');
810
const dbConfig = require('./config.cjs');
911

@@ -15,24 +17,24 @@ class DBConnector {
1517
this.pool = null;
1618
}
1719

18-
async init() {
19-
try {
20-
this.pool = await oracledb.createPool({
21-
...dbConfig,
22-
poolMax: 10,
23-
poolMin: 10
24-
});
25-
console.log('Connection pool created successfully.');
26-
} catch (error) {
27-
console.error('Error creating connection pool:', error);
28-
throw error;
20+
async createPool() {
21+
if (!this.pool) {
22+
try {
23+
this.pool = await oracledb.createPool({
24+
...dbConfig,
25+
poolMax: 10,
26+
poolMin: 10
27+
});
28+
console.log('Connection pool created successfully.');
29+
} catch (error) {
30+
console.error('Error creating connection pool:', error);
31+
throw error;
32+
}
2933
}
3034
}
3135

3236
async getConnection(options = {}) {
33-
if (!this.pool) {
34-
throw new Error('Connection pool not initialized.');
35-
}
37+
await this.createPool();
3638
try {
3739
const connection = await this.pool.getConnection({
3840
...dbConfig,
@@ -46,4 +48,4 @@ class DBConnector {
4648
}
4749
}
4850

49-
module.exports = new DBConnector();
51+
module.exports = new DBConnector();

templates/app/utils/rest-services/connection.cjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
** All rights reserved
55
** Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
66
*/
7-
const db = require( '../db/index.cjs' );
7+
8+
const db = require('../db/index.cjs');
89

910
exports.getStatus = async function () {
10-
await db.init();
1111
const connection = await db.getConnection();
12-
const result = await connection.execute( 'select 1 from dual' );
12+
const result = await connection.execute('select 1 from dual');
1313
await connection.close();
1414

1515
return {
1616
status: 'ok',
17-
}
18-
};
17+
};
18+
};

0 commit comments

Comments
 (0)