feat: Add support for semicolon connection strings

This commit is contained in:
Linden committed 2021-10-07 06:26:07 +11:00
1 parent 7e5d4cce06
commit 1ec52903ca
2 files changed
+71 -27

No files matched your search

+7 -18
View File
@@ -1,20 +1,7 @@
import { ConnectionStringParser } from 'connection-string-parser';
import versionCheck from 'github-version-checker'; import versionCheck from 'github-version-checker';
import currentVersion from '../package.json'; import currentVersion from '../package.json';
const connectionString = GetConvar('mysql_connection_string', ''); const slowQueryWarning = GetConvarInt('mysql_slow_query_warning', 150);
if (connectionString === '') throw new Error(`Set up mysql_connection_string in server.cfg`);
const config = new ConnectionStringParser({
scheme: 'mysql',
hosts: [],
}).parse(connectionString);
if (Object.keys(config).length === 0)
throw new Error(`Set up mysql_connection_string in correct format - 'mysql://user:password@host/database'`);
const slowQueryWarning = GetConvarInt('mysql_slow_query_warning', 100);
const debug = GetConvar('mysql_debug', 'false') === 'true'; const debug = GetConvar('mysql_debug', 'false') === 'true';
const isolationLevel = (() => { const isolationLevel = (() => {
@@ -29,25 +16,27 @@ const isolationLevel = (() => {
return 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE'; return 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE';
default: default:
return 'SET TRANSACTION ISOLATION LEVEL READ COMMITTED'; return 'SET TRANSACTION ISOLATION LEVEL READ COMMITTED';
}; }
})(); })();
const resourceName = GetCurrentResourceName() || 'oxmysql'; const resourceName = GetCurrentResourceName() || 'oxmysql';
export { config, slowQueryWarning, debug, isolationLevel, resourceName }; export { slowQueryWarning, debug, isolationLevel, resourceName };
setImmediate(async () => { setImmediate(async () => {
const versionCheckConfig = { const versionCheckConfig = {
repo: 'oxmysql', repo: 'oxmysql',
owner: 'overextended', owner: 'overextended',
currentVersion: currentVersion.version, currentVersion: currentVersion.version,
latest: true latest: true,
}; };
try { try {
const update = await versionCheck(versionCheckConfig); const update = await versionCheck(versionCheckConfig);
if (update) { if (update) {
console.log(`^3Your version of oxmysql is outdated (v${versionCheckConfig.currentVersion})! Please update to the latest version (${update.name}) here: \nhttps://github.com/overextended/oxmysql/releases/latest`); console.log(
`^3Your version of oxmysql is outdated (v${versionCheckConfig.currentVersion})! Please update to the latest version (${update.name}) here: \nhttps://github.com/overextended/oxmysql/releases/latest`
);
} }
} catch (e) { } catch (e) {
console.log(`^3Could not fetch updates for oxmysql!^0\n${e}`); console.log(`^3Could not fetch updates for oxmysql!^0\n${e}`);
+64 -9
View File
@@ -1,16 +1,71 @@
import { createPool } from 'mysql2/promise'; import { createPool } from 'mysql2/promise';
import { config } from './config'; import { debug } from './config';
import { parseTypes } from './parser'; import { parseTypes } from './parser';
import { ConnectionStringParser } from 'connection-string-parser';
export const pool = createPool({ const connectionString = GetConvar('mysql_connection_string', '');
host: config.hosts[0].host || 'localhost',
port: config.hosts[0].port || 3306, if (connectionString === '') {
user: config.username || 'root', throw new Error(`Set up mysql_connection_string in server.cfg`);
password: config.password || '', }
database: config.endpoint || 'es_extended',
const parseSemiColons = () => {
const parts = connectionString.split(';');
if (parseTypes.length === 1) {
throw new Error(
`Set up mysql_connection_string in correct format - 'username=root;password=password;database=db;host=127.0.0.1'`
);
}
return parts.reduce((connectionInfo, parameter) => {
const [key, value] = parameter.split('=');
connectionInfo[key] = value;
return connectionInfo;
}, {});
};
const createConnection = () => {
if (connectionString.includes('database=')) {
const options = parseSemiColons();
return createPool({
host: options.host || 'localhost',
port: options.port || 3306,
user: options.username || options.user || options.userid || 'root',
password: options.password || options.pass || '',
database: options.endpoint || options.database || 'es_extended',
charset: 'utf8mb4_unicode_ci', charset: 'utf8mb4_unicode_ci',
connectTimeout: 30000, connectTimeout: 30000,
...config.options, trace: debug || false,
debug: debug || false,
...options.options,
namedPlaceholders: true, namedPlaceholders: true,
typeCast: parseTypes, typeCast: parseTypes,
}); });
} else {
const options = new ConnectionStringParser({
scheme: 'mysql',
hosts: [],
}).parse(connectionString);
if (Object.keys(options).length === 0) {
throw new Error(`Set up mysql_connection_string in correct format - 'mysql://user:password@host/database'`);
}
return createPool({
host: options.hosts[0].host || 'localhost',
port: options.hosts[0].port || 3306,
user: options.username || 'root',
password: options.password || '',
database: options.endpoint || 'es_extended',
charset: 'utf8mb4_unicode_ci',
connectTimeout: 30000,
trace: debug || false,
debug: debug || false,
...options.options,
namedPlaceholders: true,
typeCast: parseTypes,
});
}
};
export const pool = createConnection();