Compare commits

..
2 Commits
Author SHA1 Message Date
Linden cd2b5db342 Update fxmanifest.lua 2021-11-30 04:05:22 +11:00
Linden f3fc016f31 refactor(pool): new createConnection function
Remove the connection string parsing and send the URI directly, if it exists. For semicolon connection strings, check for and replace aliases.
2021-11-25 20:29:02 +11:00
4 changed files with 23 additions and 55 deletions

No files matched your search

+1
View File
@@ -111,6 +111,7 @@ dist
.vscode-test .vscode-test
# yarn v2 # yarn v2
yarn.lock
.yarn/cache .yarn/cache
.yarn/unplugged .yarn/unplugged
.yarn/build-state.yml .yarn/build-state.yml
+1 -1
View File
@@ -3,7 +3,7 @@ game 'common'
name 'oxmysql' name 'oxmysql'
description 'Database wrapper for FiveM utilising node-mysql2 offering improved performance and security.' description 'Database wrapper for FiveM utilising node-mysql2 offering improved performance and security.'
version '1.8.0' version '1.8.1'
url 'https://github.com/overextended/oxmysql' url 'https://github.com/overextended/oxmysql'
author 'overextended' author 'overextended'
use_fxv2_oal 'yes' use_fxv2_oal 'yes'
-1
View File
@@ -13,7 +13,6 @@
}, },
"dependencies": { "dependencies": {
"@citizenfx/server": "^2.0.4793-1", "@citizenfx/server": "^2.0.4793-1",
"connection-string-parser": "^1.0.4",
"mysql2": "https://github.com/overextended/node-mysql2.git", "mysql2": "https://github.com/overextended/node-mysql2.git",
"postinstall-postinstall": "^2.1.0" "postinstall-postinstall": "^2.1.0"
}, },
+21 -53
View File
@@ -1,6 +1,5 @@
import { createPool } from 'mysql2/promise'; import { createPool } from 'mysql2/promise';
import { parseTypes } from './parser'; import { parseTypes } from './parser';
import { ConnectionStringParser } from 'connection-string-parser';
const connectionString = GetConvar('mysql_connection_string', ''); const connectionString = GetConvar('mysql_connection_string', '');
@@ -8,60 +7,29 @@ if (connectionString === '') {
throw new Error(`Set up mysql_connection_string in server.cfg`); throw new Error(`Set up mysql_connection_string in server.cfg`);
} }
const parseSemiColons = () => { const dbOptions = (() => {
const parts = connectionString.split(';'); if (connectionString.includes('mysql://')) return { uri: connectionString };
if (parseTypes.length === 1) { const options = connectionString
throw new Error( .replace(/(?:host(?:name)|ip|server|data\s?source|addr(?:ess)?)=/gi, 'host=')
`Set up mysql_connection_string in correct format - 'username=root;password=password;database=db;host=127.0.0.1'` .replace(/(?:user\s?(?:id|name)?|uid)=/gi, 'user=')
); .replace(/(?:pwd|pass)=/gi, 'password=')
} .replace(/(?:db)=/gi, 'database=')
return parts.reduce((connectionInfo, parameter) => { .split(';')
const [key, value] = parameter.split('='); .reduce((connectionInfo, parameter) => {
connectionInfo[key] = value; const [key, value] = parameter.split('=');
return connectionInfo; connectionInfo[key] = value;
}, {}); return connectionInfo;
}; }, {});
return options;
})();
// @TODO: refactor acceptable options when using semicolon connection; this or stuff is getting old
const createConnection = () => { const createConnection = () => {
if (connectionString.includes('host=')) { return createPool({
const options = parseSemiColons(); ...dbOptions,
namedPlaceholders: true,
return createPool({ typeCast: parseTypes,
host: options.host || 'localhost', });
port: options.port || 3306,
user: options.username || options.user || options.userid || options.uid || 'root',
password: options.password || options.pass || options.pwd || '',
database: options.endpoint || options.database,
charset: 'utf8mb4_unicode_ci',
connectTimeout: 30000,
...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,
charset: 'utf8mb4_unicode_ci',
connectTimeout: 30000,
...options.options,
namedPlaceholders: true,
typeCast: parseTypes,
});
}
}; };
export const pool = createConnection(); export const pool = createConnection();