2

I'm trying to connect with npm Oracledb using typescript and in the format of class and module using typescript. For example,

Import {Oracledb} from 'oracledb'
class ConnectDAO{

     public ConnectionDB(): connection{
         let connection = Oracledb.getConnection(// connection string);
         Return connection;
     }

     public fetchData(connection): recordset{
          connection.execute('query')
          Return rs;
     }

}

can anyone please help to achieve this issue

2 Answers 2

4

Here is a full example. Just fill in your connection string...

import * as oracledb from 'oracledb';

let testConfig: oracledb.IConnectionAttributes = {
    user: `scott`,
    password: `tiger`,
    connectString: "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=..."
}

class ConnectDAO {
    public async ConnectionDB(): Promise<oracledb.IConnection> {
        return await oracledb.getConnection(testConfig);
    }
    public fetchData(connection: oracledb.IConnection):
        oracledb.IPromise<oracledb.IExecuteReturn> {
        return connection.execute('SELECT CURRENT_TIMESTAMP FROM dual');
    }
}

async function connectAndExecute() {
    let connectDao = new ConnectDAO();
    try {
        let connection = await connectDao.ConnectionDB();
        let results = await connectDao.fetchData(connection);
        console.log(`results : ${JSON.stringify(results, null, 4)}`)
    } catch (err) {
        console.log(`error caught ${err}`);
    }
}

connectAndExecute();
Sign up to request clarification or add additional context in comments.

3 Comments

for those who still get errors on the import, you might have to use npm install --save-dev @types/oracledb
This keeps on throwing an error: Error: ./node_modules/oracledb/lib/oracledb.js Module not found: Error: Can't resolve 'path' in 'C:\Users\ac61817\ACS\acs_dashboard\node_modules\oracledb\lib'
And another one Error from chokidar (C:\): Error: EBUSY: resource busy or locked, lstat 'C:\DumpStack.log.tmp' And there is no DumpStack file in C:\
1

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);
    });
}

enter image description here

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.