@condor-labs/knex-oracle

2.2.0 • Public • Published

This module provide and usefull helper to connect ORACLE using KNEX library.

See official documentation here.

Compatibility

  • The minimum supported version of Node.js is v8.
  • Since the version 2.0.1, we are supporting node v.12 with oracledb5.1.0

How to use it

To use the library you just need to follow the following steps Install the library with npm

npm install @condor-labs/knex-oracle

Import the library:

const knexOracle = require('@condor-labs/knex-oracle')(settings);

1. settings object properties

Property Default Description
user (String) The username for authentication. This option is mongoose-specific, they are equivalent to the MongoDB driver's auth.username options.
password (String) The password for authentication. This option is mongoose-specific, they are equivalent to the MongoDB driver's auth.password options.
connectionString (String) Specifies the connection string to connect to the Oracle database
options (Object) Additional settings or properties used to initialize the knex connection, more information.

2. settings object properties (when used the semaphore api)

Property Default Description
debugMode (Boolean) false Define if print in console to connect a some datacenter
semaphore (Boolean) Define if the connection to oracle is through of semaphore api, should be true value
semaphoreApi (Object) Setting up the gateway api so that the library queries the available datacenter node. The fields for this object are url (Url of the api gateway of the traffic light), typeKey ("Authorization") and apiKey (Api key that allows access to the api).
default (String) Default node to be connected in case of any error when accessing the gateway api (Only values: NODE_1 and NODE_2 can be passed).
NODE_1 (Object) Corresponds to the first datacenter available to connect, the object it has must be with the object used in the settings explained in the previous point.
NODE_2 (Object) Corresponds to the second datacenter available to connect, the object it has must be with the object used in the settings explained in the previous point.

Error Codes

Code Description
INVALID_SETTINGS When the settings object is wrongly setup
SETTINGS_NOT_DEFINED When no settings are passed
ORACLE_SEMAPHORE_NETWORK_ERROR When there is a failure trying to get the available datacenter through the semaphore api gateway. (In this case the library connects to the datacenter by default.)
UNSUPPORTED_SETTINGS When the semaphore is enabled and the function getClient is called

Examples

Create testing package in ORACLE

constants.js

module.exports = {
    settings: {
        user: 'my_user',
        password: 'my_pass',
        connectionString: '10.111.222.333:1521/MY_ORACLE_DATABASE',
        options: {
            acquireConnectionTimeout: 10000,
            pool: { min: 0, max: 10 }
        }
    },
    settingsSemaphore: {
        debugMode: true,
        semaphore: true,
        semaphoreApi: {
            url : 'https://apps-inventory.test.cebroker.com/semaphore',
            typeKey: 'Authorization',
            apiKey: '123'
        },
        default: 'NODE_1',
        NODE_1:
        {
            user: 'my_user_test',
            password: 'my_pass_test',
            connectionString: '10.111.222.333:1521/MY_ORACLE_DATABASE_TEST',
            options: {
                acquireConnectionTimeout: 10000,
                pool: { min: 0, max: 10 }
            },
        },
        NODE_2: {
            user: 'my_user_dev',
            password: 'my_pass_dev',
            connectionString: '10.111.222.334:1521/MY_ORACLE_DATABASE_DEV',
            options: {
                acquireConnectionTimeout: 10000,
                pool: { min: 0, max: 10 }
            },
        }
    },
    executeStatement: {
        statement: `
            declare
                pparam1 varchar2(10) := :param1;
                pparam2 varchar2(10) := :param2;
            begin
                open :creturn_1 for select 'TEST OK 1!!!' as "result1", pparam1 as "param 11", pparam2 as "param 21" from dual;
                open :creturn_2 for select 'TEST OK 2!!!' as "result2", pparam1 as "param 12", pparam2 as "param 22" from dual;
                open :creturn_3 for select 'TEST OK 3!!!' as "result3", pparam1 as "param 13", pparam2 as "param 23" from dual;
            end;
        `,
        params: { param1: "abc", param2: '123' },
        cursorTypeParams: ['creturn_1', 'creturn_2', 'creturn_3']
    },
    executeFunction: {
        name: 'pkg_test_knex_oracle.fnc_calculator',
        params: { pcd_operator: '+', pam_value1: 10, pam_value2: 5 }
    },
    executeProcedure: {
        name: 'pkg_test_knex_oracle.proc_count_until_value_plus_5',
        params: { pam_start_value: 1000 },
        cursorTypeParams: ['pcursor_return'],
        outputParams: ['pam_value_out_1', 'pam_value_out_2']
    }
};

index.js

let {
    settings,
    settingsSemaphore,
    executeStatement,
    executeFunction,
    executeProcedure
} = require('./constants');

const runExample = async (knexOracle,isSemaphore = false) => {
    // init knex-oracle connection

    //---------------------------------------------------
    // prepare query statement (EXECUTE STATEMENT)
    //---------------------------------------------------

    let ret = {};
    let client;
    try {
        client = (!isSemaphore?knexOracle.getClient():await knexOracle.getClientAsync());
        ret = await client.executeStatement(
            executeStatement.statement,
            executeStatement.params,
            executeStatement.cursorTypeParams
        );
    }
    catch (error) {
        ret = {
            code: error.code,
            message: error.message,
            stack: error.stack
        }
    }
    // print response
    console.log('EXECUTE STATEMENT Result:', ret);

    //---------------------------------------------------
    // prepare query statement (EXECUTE FUNCTION)
    //---------------------------------------------------

    try {
        client = (!isSemaphore?knexOracle.getClient():await knexOracle.getClientAsync());
        ret = await client.executeFunction(
            executeFunction.name,
            executeFunction.params
        );
    }
    catch (error) {
        ret = {
            code: error.code,
            message: error.message,
            stack: error.stack
        }
    }
    // print response
    console.log('EXECUTE FUNCTION Result:', ret);

    //---------------------------------------------------
    // prepare query statement (EXECUTE PROCEDURE)
    //---------------------------------------------------

    try {
        client = (!isSemaphore?knexOracle.getClient():await knexOracle.getClientAsync());
        ret = await client.executeProcedure(
            executeProcedure.name,
            executeProcedure.params,
            executeProcedure.cursorTypeParams,
            executeProcedure.outputParams
        );
    }
    catch (error) {
        ret = {
            code: error.code,
            message: error.message,
            stack: error.stack
        }
    }
    // print response
    console.log('EXECUTE PROCEDURE Result:', ret);
}

(async () => {
  try {
    let isSemaphore = false;
    const semaphore = process.argv.filter((arg) => arg.startsWith("semaphore"));
    if (semaphore && semaphore.length > 0) {
      settings = settingsSemaphore;
      isSemaphore = true;
    }
    const knexOracle = require("./../library")(settings);
    await runExample(knexOracle,isSemaphore);

    const retry = process.argv.filter((arg) => arg.startsWith("retry"));
    if (retry && retry.length > 0) {
      const ms = isSemaphore ? 70000 : 10000;
      console.log(`Waiting ${ms} miliseconds.... `);
      await new Promise((res) => setTimeout(res, ms));
      console.log("Try again");
      await runExample(knexOracle,isSemaphore);
    }

    process.exit(1);
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
})();

In case of using the connection to the cloud semaphore, you must run index.js and passed the argument semaphore and you can pass the argument retry in orden to retry the testing connections again after 2 minutes.

node ../example/index.js semaphore retry

How to Publish

Increasing package version

You will need to update the package.json file placed in the root folder.

identify the property version and increase the right number in plus one.

Login in NPM by console.

 npm login
 [Enter username]
 [Enter password]
 [Enter email]

If all is ok the console will show you something like this : Logged in as USERNAME on https://registry.npmjs.org/.

Uploading a new version

 npm publish --access public

Ref: https://docs.npmjs.com/getting-started/publishing-npm-packages

Note: you will need to have a NPM account, if you don't have one create one here: https://www.npmjs.com/signup

Contributors

The original author and current lead maintainer of this module is the @condor-labs development team.

More about Condorlabs Here.

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i @condor-labs/knex-oracle

Weekly Downloads

10

Version

2.2.0

License

MIT

Unpacked Size

50.3 kB

Total Files

7

Last publish

Collaborators

  • daniel.castillo
  • kevin.pedroza_condorlabs.io
  • federico-garcia
  • jorgesanes
  • hjimenez-condorlabs
  • awilches
  • jorgelozano95