# Installation

Use npm command in order to install bh-oracle module:

```bash
npm install --save bh-oracle
```
# Use oracle with NodeJs

The bh-oracle class allows to manage an oracle connection inside a NodeJs project. It implements the methods required for the BH unified handler compliance:

  - connect(settings) : create a new connection to an oralce server. Settings can be a bh-section object or a path to a configuration file. If settings is not specified a default configuration file will be used (./conf/oracle.conf.json). Connect returns a promise.

  Settings or configuration file format:

  ```json
  {
    "host" : "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)))(CONNECT_DATA=(SID=test)))",
    "user" : "test",
    "pass" : "test"
  }
  ```

  - close()           : close the current connection to oracle. Close returns a promise.
  - query(settings)   : execute a query described by settings in the current connection. Query returns a promise.

  Settings format:

  ```json
  {
    "text" : "SELECT * FROM Test where id >= :id_left AND i =< :id_right",
    "args" : [1, 10],
    "type" : "select"
  }
  ```

  ```json
  {
    "text" : "INSERT INTO Test (comment) VALUES (:comment)",
    "args" : ['test'],
    'type' : 'insert',
    'options' : {
      'autoCommit' : true
    }
  }
  ```

  - transaction()    : open a new transaction session in the current conection. Transaction returns a promise.
  - inTransaction()  : returns true if a transaction session is openend, otherwise false.
  - commit()         : commit the current transaction session in the current connection. Commit returns a promise.
  - rollback()       : rollback the current transaction session in the current connection. Rollback returns a promise.

# Usage example

```js
var OracleClass   = require('bh-oracle');
var SectionClass = require('bh-section');

var oracle = new OracleClass();

oracle.connect() // Read ./conf/oracle.conf.json
  .then(function() {
    oracle.query(new SectionClass({
      'text' : 'SELECT * FROM Test',
      'args' : [],
      'type' : 'select'
    }))
    .then(function(result) {
      console.log(result);
      oracle.close();
    })
    .catch(function(error) {
      console.error(error);
      oracle.close();
    });
  })
  .catch(function(error) {
    console.error(error);
  });
```
