# MYSQL DATABASE CONNECTION

Simple Mysql Database Server (MDS) providing comprehensive error message for both development and production as you mention in the .env file

## Environment configuration

```javascript
    IS_PRODUCTION = 1  # 0 - Development server | 1 - Production server
    DB_HOST = localhost
    DB_HOST_USER = root
    DB_HOST_PWD = 
    DB_NAME = test
    DB_POOL_MAX_CONNECTIONS = 10
    DB_QUEUE_LIMIT = 0
    DB_CAN_DELAY_QUERY_ON_QUEUE_LIMIT = 1
```

## Usage

@uwineza/mysql.connect is a promise version of connection, therefore to be working with this MySQL connector you should follow the promise rule of javascript.
Here is the functional sample code

```javascript
    const express = require('express');
    const { executeQuery, mysqlMessage } = require('@uwineza/mysql.connect');

    const router = express.Router();

    router.get('/users', async (req, res) => {
        try {
            const [users] = await executeQuery("SELECT * FROM users");
            return res.json({ users });
        } catch (err) {
            return mysqlMessage(err, res)
        }
    });
```

## Configuration explains

#### 1. IS_PRODUCTION
This is a boolean variable received tiny-int 0 (false) and 1 (true), it tells the package whether the project on which required in is under development or in production mode.

#### 2. DB_HOST
This variable holds the address of your database host.

#### 3. DB_HOST_USER
This variable holds the username of your host.

#### 4. DB_HOST_PWD
This variable holds the password of your host.

#### 5. DB_NAME
This variable holds the name of your database.

#### 6. DB_POOL_MAX_CONNECTIONS
This variable refers how much connection you want to you database

🚨 Caution, Please avoid to much connection because this may affect the performance! consider the amount of connection wisely regarding you use case and try to minimize as much you can.

#### 7. DB_QUEUE_LIMIT
This variable define the amount of query to be waiting for connection in queue.
The queue limit is a configuration option that controls the number of queries that can be pending (i.e., in the queue) at any given time.

🚨 Caution, Please avoid to much connection because this may affect the performance! consider the amount of connection wisely regarding you use case and try to minimize as much you can.

🚀 Advice, When you find yourself in situation where you required to set a very large size for query queue think about keep the same size and try this feature below DB_CAN_DELAY_QUERY_ON_QUEUE_LIMIT compare the performance to check what works better for you situation.

#### 8. DB_CAN_DELAY_QUERY_ON_QUEUE_LIMIT
Normally, when there is no connection available for incoming query and queue is full the query execution stopped by default but with this variable you can set DB_CAN_DELAY_QUERY_ON_QUEUE_LIMIT to 1 (true) to ensure the delay of your queries instead of stoping.

so, if turn to delaying to 1 (true) all your query will be delayed and waits in queue when all you connection and queue in pool are busy and full waiting for any spot after some query release connection they enter in sequence.

🚨 Caution, This is more advanced feature as an advice you may consider it when you app receive too many DB query request and you do not need to cancel any user request.

🚀 To work with query queueing service you should import predefined function called "executeQuery" instead of pool as before.

```javascript
    // .env variable to enforce query execution delaying

    DB_QUEUE_LIMIT = 10 // For better performance you're advised not to use less than 10 queries but feel free to adjust, do not set to 0 when set delay to 1
    DB_CAN_DELAY_QUERY_ON_QUEUE_LIMIT = 0 // set delay to 1 to enable delaying

```

```javascript
    // DB_CAN_DELAY_QUERY_ON_QUEUE_LIMIT example

    const express = require('express');
    const { executeQuery, mysqlMessage } = require('@uwineza/mysql.connect');

    const router = express.Router();

    router.get('/users', async (req, res) => {
        try {
            const [users] = await executeQuery("SELECT * FROM users WHERE role = ?", ['admin']);
            return res.json({ users });
        } catch (err) {
            return mysqlMessage(err, res)
        }
    });
```
