# Installation

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

```bash
npm install --save bh-mysql
```

# Use mysql with NodeJs

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

  - connect(settings) : create a new connection to a mysql 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/mysql.conf.json). Connect returns a promise.

  Settings or configuration file format:

  ```json
  {
    "host" : "127.0.0.1",
    "user" : "test",
    "pass" : "test",
    "base" : "test"
  }
  ```

  - close()          : close the current connection to mysql. 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 >= ? AND i =< ?",
    "args" : [1, 10]
  }
  ```

  - 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 MysqlClass   = require('bh-mysql');
var SectionClass = require('bh-section');

var mysql = new MysqlClass();

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