Search code examples
javascriptoracle-databasecloudnode-oracledb

Connect to oracle cloud database by wallet in JavaScript?


I have scripts that i need to run, to precise it is CREATE TABLE script. I have such functions, see below:

oracleLoader.ts

const oracledb = require('oracledb');
import dbConfig from "./oracleDBConfig";
import log from "../utils/winston";

function oracleLoader(sql: string) {

  const connection = oracledb.getConnection(dbConfig);

  try{
    connection.execute(sql);
    log.info("Table created.");
  } catch (e) {
    log.error("SQL script do not executed!" + e);
  }

}

export default oracleLoader;

oracleDBConfig.ts

function oracleDBConfig() {
  return {
    user: mail,
    password: pass,
    connectString: str
  };
}

export default oracleDBConfig;

Where to get that username or do i need email?

Where to get password, is it password from database, or from oracle account?

Where to get connection string?

What if i have my .zip file with oracle-wallet.

Remember that i need to connect to the database that existing in the cloud.

Note: in line const connection = oracledb.getConnection(dbConfig); i have a warning that:

Argument type () => {password: string, user: string, connectString: string} is not assignable to parameter type GetConnectionOptions


Solution

  • The username and password you need are the Database username and password. Assuming you're using Oracle Autonomous DB, then the administration username is 'ADMIN' (and you can use this to create other users). The connection string will be the alias in the tnsnames.ora file that you download in the zip file with the wallet.

    Since this seems new to you, Oracle's 'quick start' is a handy place to begin https://www.oracle.com/database/technologies/appdev/quickstartnodejs.html

    The 'not assignable' error is a different issue, not related to Oracle. First you need to call dbConfig() to get the credentials. Then you need somehow to work with the async oracledb functions. Then you should add some error handling.

    I'm not a typescript user, but this runs:

    const oracledb = require('oracledb');
    import dbConfig from "./oracleDBConfig";
    
    async function oracleLoader(sql: string) {
    
        let connection;
        try {
            connection = await oracledb.getConnection(dbConfig());
            const r = await connection.execute(sql);
            console.log(r);
            console.log("SQL statement was executed!");
        } catch (e) {
            console.error("SQL statement was not executed!" + e);
        } finally {
            if (connection) {
                try {
                    await connection.close();
                } catch (err) {
                    console.error(err);
                }
            }
        }
    }
    
    export default oracleLoader;
    

    Note that execute() executes a single statement, not a script.

    In real life, you probably want to use a connection pool, see the node-oracledb connection pooling documentation.