Install below packages
npm i oracledb @types/oracledb --save
database.ts
import OracleDB from 'oracledb';
export default class DatabaseConnection {
private oracleDB = OracleDB;
private dbConfig = {
user: "<<USER_NAME>>",
password: "<<USER_PASSWORD>>",
connectString: "(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST=<<YOUR_IP>>)(PORT = <<YOUR_PORT>>))(CONNECT_DATA =(SERVICE_NAME='<<YOUR_SERVICE_NAME>>')))"
}
public async init(): Promise<void> {
}
public async connectWithDB() {
return new Promise((resolve, reject) => {
this.oracleDB.getConnection(this.dbConfig, (err, connection) => {
if (err) {
reject(err.message);
}
console.log('Connected with Database...');
resolve(connection);
});
});
}
public doRelease(connection) {
connection.release((err) => {
if (err)
console.error(err.message);
console.log('connection released');
});
}
}
App.js
Import database file in your App.js/Service.js File
import DatabaseConnection from './database';
Create a class variable for the same
public dbConnect = new DatabaseConnection;
Add below method in same file
private async connectToDB() {
await this.dbConnect.connectWithDB().then(async (connection: any) => {
await connection.execute("SELECT * FROM User", [], (err, result) => {
if (err) {
console.error(err.message);
}
console.log(result.metaData);
});
this.dbConnect.doRelease(connection);
}).catch(error => {
console.log(error);
});
}
