mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-03 14:38:50 +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
-37
@@ -1,37 +0,0 @@
|
|||||||
import { createPool } from 'mysql2/promise';
|
|
||||||
import { parseTypes } from './parser';
|
|
||||||
|
|
||||||
const connectionString = GetConvar('mysql_connection_string', '');
|
|
||||||
|
|
||||||
if (connectionString === '') {
|
|
||||||
throw new Error(`Set up mysql_connection_string in server.cfg`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const dbOptions = (() => {
|
|
||||||
const options = connectionString.includes('mysql://')
|
|
||||||
? { uri: connectionString }
|
|
||||||
: connectionString
|
|
||||||
.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;
|
|
||||||
}, {});
|
|
||||||
|
|
||||||
options.typeCast = parseTypes;
|
|
||||||
options.namedPlaceholders = true;
|
|
||||||
|
|
||||||
// increase the default timeout for slow servers
|
|
||||||
options.connectTimeout = options.connectTimeout || 60000;
|
|
||||||
|
|
||||||
// disabled to prevent SQL injection
|
|
||||||
options.multipleStatements = options.multipleStatements || false;
|
|
||||||
|
|
||||||
return options;
|
|
||||||
})();
|
|
||||||
|
|
||||||
export const pool = createPool(dbOptions);
|
|
||||||
@@ -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