mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-02 06:28:54 +00:00
refactor(pool): cleanup connection options and createPool defaults
Additionally perform initial database connection as part of this module, rather than index.
This commit is contained in:
1 parent
17c4916010
commit
35f198f2b5
2 files changed
+37
-37
No files matched your search
@@ -0,0 +1,37 @@
|
||||
import { createPool } from 'mysql2/promise';
|
||||
import { mysql_connection_string, mysql_transaction_isolation_level, typeCast } from '../config.js';
|
||||
|
||||
const connectionString = (() => {
|
||||
if (mysql_connection_string.includes('mysql://')) return { uri: mysql_connection_string };
|
||||
|
||||
const options = mysql_connection_string
|
||||
.replace(/(?:host(?:name)|ip|server|data\s?source|addr(?:ess)?)=/gi, 'host=')
|
||||
.replace(/(?:user\s?(?:id|name)?|uid)=/gi, 'user=')
|
||||
.replace(/(?:pwd|pass)=/gi, 'password=')
|
||||
.replace(/(?:db)=/gi, 'database=')
|
||||
.split(';')
|
||||
.reduce((connectionInfo, parameter) => {
|
||||
const [key, value] = parameter.split('=');
|
||||
connectionInfo[key] = value;
|
||||
return connectionInfo;
|
||||
}, {});
|
||||
|
||||
return options;
|
||||
})();
|
||||
|
||||
const pool = createPool({
|
||||
typeCast: typeCast,
|
||||
connectTimeout: 60000,
|
||||
...connectionString,
|
||||
});
|
||||
|
||||
pool
|
||||
.query(mysql_transaction_isolation_level)
|
||||
.then(() => {
|
||||
console.log(`^2Database server connection established!^0`);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(`^3Unable to establish a connection to the database! [${err.code}]\n${err.message}^0`);
|
||||
});
|
||||
|
||||
export default pool;
|
||||
Reference in new issue
Block a user