1
- import { Connection , createPool , Pool } from 'mysql2/promise' ;
1
+ import { createPool , Pool } from 'mysql2/promise' ;
2
2
import { connectionOptions , mysql_transaction_isolation_level , setDebug } from '../config' ;
3
- import { typeCast } from '../utils/typeCast' ;
4
3
5
4
let pool : Pool ;
6
5
let isServerConnected = false ;
@@ -18,25 +17,8 @@ export async function waitForConnection() {
18
17
}
19
18
}
20
19
21
- setDebug ( ) ;
22
-
23
- setInterval ( ( ) => {
24
- setDebug ( ) ;
25
- } , 1000 ) ;
26
-
27
- setTimeout ( async ( ) => {
28
- const flags : string [ ] = [ ] ;
29
- flags . push ( connectionOptions . database ? 'CONNECT_WITH_DB' : '-CONNECT_WITH_DB' ) ;
30
-
31
- pool = createPool ( {
32
- connectTimeout : 60000 ,
33
- trace : false ,
34
- supportBigNumbers : true ,
35
- ...connectionOptions ,
36
- typeCast,
37
- namedPlaceholders : false , // we use our own named-placeholders patch, disable mysql2s
38
- flags : flags ,
39
- } ) ;
20
+ function setConnectionPool ( ) {
21
+ pool = createPool ( connectionOptions ) ;
40
22
41
23
pool . on ( 'connection' , ( connection ) => {
42
24
connection . query ( mysql_transaction_isolation_level ) ;
@@ -45,16 +27,27 @@ setTimeout(async () => {
45
27
pool . on ( 'acquire' , ( connection ) => {
46
28
connection . query ( 'SET profiling_history_size = 0' ) ;
47
29
connection . query ( 'SET profiling = 0' ) ;
48
- connection . query ( 'SET profiling = 1' ) ;
49
30
connection . query ( 'SET profiling_history_size = 1000' ) ;
31
+ connection . query ( 'SET profiling = 1' ) ;
50
32
} ) ;
33
+ }
34
+
35
+ setInterval ( ( ) => {
36
+ setDebug ( ) ;
37
+ } , 1000 ) ;
38
+
39
+ setTimeout ( async ( ) => {
40
+ setDebug ( ) ;
41
+ setConnectionPool ( ) ;
51
42
52
43
try {
53
- await pool . query ( 'SELECT DATABASE()' ) ;
44
+ ( await pool . getConnection ( ) ) . release ( ) ;
54
45
console . log ( `^2Database server connection established!^0` ) ;
55
46
isServerConnected = true ;
56
- } catch ( err ) {
57
- console . error ( `^3Unable to establish a connection to the database!\n^3[${ err } ]^0` ) ;
47
+ } catch ( err : any ) {
48
+ console . log (
49
+ `^3Unable to establish a connection to the database (${ err . code } )!\n^1Error ${ err . errno } : ${ err . message } ^0`
50
+ ) ;
58
51
}
59
52
} ) ;
60
53
0 commit comments