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
+76 -32

No files matched your search

+8 -19
View File
@@ -1,20 +1,7 @@
import { ConnectionStringParser } from 'connection-string-parser';
import versionCheck from 'github-version-checker';
import currentVersion from '../package.json';
const connectionString = GetConvar('mysql_connection_string', '');
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 slowQueryWarning = GetConvarInt('mysql_slow_query_warning', 150);
const debug = GetConvar('mysql_debug', 'false') === 'true';
const isolationLevel = (() => {
@@ -29,27 +16,29 @@ const isolationLevel = (() => {
return 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE';
default:
return 'SET TRANSACTION ISOLATION LEVEL READ COMMITTED';
};
}
})();
const resourceName = GetCurrentResourceName() || 'oxmysql';
export { config, slowQueryWarning, debug, isolationLevel, resourceName };
export { slowQueryWarning, debug, isolationLevel, resourceName };
setImmediate(async () => {
const versionCheckConfig = {
repo: 'oxmysql',
owner: 'overextended',
currentVersion: currentVersion.version,
latest: true
latest: true,
};
try {
const update = await versionCheck(versionCheckConfig);
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) {
console.log(`^3Could not fetch updates for oxmysql!^0\n${e}`);
}
});
});
+68 -13
View File
@@ -1,16 +1,71 @@
import { createPool } from 'mysql2/promise';
import { config } from './config';
import { debug } from './config';
import { parseTypes } from './parser';
import { ConnectionStringParser } from 'connection-string-parser';
export const pool = createPool({
host: config.hosts[0].host || 'localhost',
port: config.hosts[0].port || 3306,
user: config.username || 'root',
password: config.password || '',
database: config.endpoint || 'es_extended',
charset: 'utf8mb4_unicode_ci',
connectTimeout: 30000,
...config.options,
namedPlaceholders: true,
typeCast: parseTypes,
});
const connectionString = GetConvar('mysql_connection_string', '');
if (connectionString === '') {
throw new Error(`Set up mysql_connection_string in server.cfg`);
}
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',
connectTimeout: 30000,
trace: debug || false,
debug: debug || false,
...options.options,
namedPlaceholders: true,
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();