@@ -22,13 +22,15 @@ class QueryService {
22
22
* @param {object } configMySql - mysql config
23
23
*/
24
24
constructor ( configMySql = { } ) {
25
- configMySql . _user = configMySql ?. user ?? 'gui' ;
26
- configMySql . _password = configMySql ?. password ?? '' ;
27
- configMySql . _host = configMySql ?. host ?? 'localhost' ;
28
- configMySql . _port = configMySql ?. port ?? 3306 ;
29
- configMySql . _database = configMySql ?. database ?? 'info_logger' ;
30
- configMySql . _connectionLimit = configMySql ?. connectionLimit ?? 25 ;
25
+ configMySql . user = configMySql ?. user ?? 'gui' ;
26
+ configMySql . password = configMySql ?. password ?? '' ;
27
+ configMySql . host = configMySql ?. host ?? 'localhost' ;
28
+ configMySql . port = configMySql ?. port ?? 3306 ;
29
+ configMySql . database = configMySql ?. database ?? 'info_logger' ;
30
+ configMySql . connectionLimit = configMySql ?. connectionLimit ?? 25 ;
31
31
this . _timeout = configMySql ?. timeout ?? 10000 ;
32
+ this . _host = configMySql . host ;
33
+ this . _port = configMySql . port ;
32
34
33
35
this . _pool = mariadb . createPool ( configMySql ) ;
34
36
this . _isAvailable = false ;
@@ -38,18 +40,24 @@ class QueryService {
38
40
/**
39
41
* Method to test connection of mysql connector once initialized
40
42
* @param {number } timeout - timeout for the connection test
43
+ * @param {boolean } shouldThrow - whether an error should be thrown on failure
41
44
* @returns {Promise } - a promise that resolves if connection is successful
42
45
*/
43
- async checkConnection ( timeout = this . _timeout ) {
46
+ async checkConnection ( timeout = this . _timeout , shouldThrow = true ) {
44
47
try {
45
48
await this . _pool . query ( {
46
49
sql : 'SELECT 1' ,
47
50
timeout,
48
51
} ) ;
49
52
this . _isAvailable = true ;
53
+ this . _logger . infoMessage ( `Connection to DB successfully established: ${ this . _host } :${ this . _port } ` ) ;
50
54
} catch ( error ) {
51
55
this . _isAvailable = false ;
52
- fromSqlToNativeError ( error ) ;
56
+ if ( shouldThrow ) {
57
+ fromSqlToNativeError ( error ) ;
58
+ } else {
59
+ this . _logger . errorMessage ( error ) ;
60
+ }
53
61
}
54
62
}
55
63
0 commit comments