@uwineza/mysql.connect
Version:
Simple Mysql Database Server (MDS) connection providing comprehensive error message for both development and production as you mention in the .env file variable called IS_PRODUCTION which is either 1 for production or 0 for development.
103 lines (72 loc) ⢠4.26 kB
Markdown
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
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
```
@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)
}
});
```
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.
This variable holds the address of your database host.
This variable holds the username of your host.
This variable holds the password of your host.
This variable holds the name of your database.
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.
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)
}
});
```