# 02. __Usage__

[![+Professional Support](https://www.totaljs.com/img/badge-support.svg)](https://www.totaljs.com/support/) [![+Chat with contributors](https://www.totaljs.com/img/badge-chat.svg)](https://messenger.totaljs.com)

This module works with existing database NPM modules.

## Initialization

- this module can work with multiple database engines together

__Create a definition file `db.js`__:

```javascript
const dbms = require('dbms');

// dbms.init([alias], connection_string);
// @alias {String} Optional, alias for connection string (default: 'default')
// @connection_string {String} A connection string to DB

dbms.init('postgresql://user:pass@localhost:5432/dbname1');
dbms.init('mypg', 'postgresql://user:pass@localhost:5432/dbname2'); // with a name
dbms.init('mynosql', 'nosql'); // It uses Total.js NOSQL
dbms.init('mytable', 'table'); // It uses Total.js TABLE
```

__Simple usage__:

```javascript
// DBMS is a global variable for the entire project
var db = DBMS();

db.find('table_name').take(100).callback(console.log);
db.find('mypg/table_name').take(100).callback(console.log);
db.find('mynosql/users').take(100).callback(console.log);
db.find('mytable/users').take(100).callback(console.log);
```

## Connection string params

Connection string can contain additional params for some databases.

```html
postgresql://user:pass@localhost:5432/dbname1?param=1&amp;param=2
```

### Param: `max`

Max pools for pooling. This param supports PostgreSQL only. Default: `4`.

```html
?max=4
```

### Param: `min`

Min pools for pooling. This param supports PostgreSQL only. Default: `2`.

```html
?min=2
```

### Param: `native`

Enables a native driver for DB engine. Currently only `PostgreSQL` supports native driver. Just follow steps on [Node PostgreSQL C/C++ Binding](https://node-postgres.com/features/native). By default is `native` binding __disabled__.

Possible values for connection string:

```html
?native=1

// or

?native=true
```

### Param: `pooling`

Enables pooling. This param supports PostgreSQL only. Default: `true`.

```html
// Disabling via:
?pooling=false

// or

?pooling=0
```

### Param: `ssl`

Enables __SSL__ secured connection. By default is `ssl` __disabled__.

```html
?ssl=1
// or
?ssl=true
```

### Param: `timeout`

Idle timeout in milliseconds. This param supports PostgreSQL only. Default: `1000`.

```html
?timeout=1000
```