mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-03 06:28:53 +00:00
refactor: move connectionOptions to config
We'll also ensure namedPlaceholders is boolean and export the object.
This commit is contained in:
1 parent
7796c80f69
commit
521aa8f03d
2 files changed
+49
-47
No files matched your search
@@ -27,6 +27,54 @@ export const mysql_transaction_isolation_level = (() => {
|
|||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
const parseUri = (connectionString: string) => {
|
||||||
|
const splitMatchGroups = connectionString.match(
|
||||||
|
new RegExp(
|
||||||
|
'^(?:([^:/?#.]+):)?(?://(?:([^/?#]*)@)?([\\w\\d\\-\\u0100-\\uffff.%]*)(?::([0-9]+))?)?([^?#]+)?(?:\\?([^#]*))?$'
|
||||||
|
)
|
||||||
|
) as RegExpMatchArray;
|
||||||
|
|
||||||
|
if (!splitMatchGroups) throw new Error(`mysql_connection_string structure was invalid (${connectionString})`);
|
||||||
|
|
||||||
|
const authTarget = splitMatchGroups[2] ? splitMatchGroups[2].split(':') : [];
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
user: authTarget[0] || undefined,
|
||||||
|
password: authTarget[1] || undefined,
|
||||||
|
host: splitMatchGroups[3],
|
||||||
|
port: parseInt(splitMatchGroups[4]),
|
||||||
|
database: splitMatchGroups[5].replace(/^\/+/, ''),
|
||||||
|
...(splitMatchGroups[6] &&
|
||||||
|
splitMatchGroups[6].split('&').reduce<Record<string, string>>((connectionInfo, parameter) => {
|
||||||
|
const [key, value] = parameter.split('=');
|
||||||
|
connectionInfo[key] = value;
|
||||||
|
return connectionInfo;
|
||||||
|
}, {})),
|
||||||
|
};
|
||||||
|
|
||||||
|
return options;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const connectionOptions = (() => {
|
||||||
|
const options: Record<string, any> = mysql_connection_string.includes('mysql://')
|
||||||
|
? parseUri(mysql_connection_string)
|
||||||
|
: 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<Record<string, string>>((connectionInfo, parameter) => {
|
||||||
|
const [key, value] = parameter.split('=');
|
||||||
|
connectionInfo[key] = value;
|
||||||
|
return connectionInfo;
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
options.namedPlaceholders = options.namedPlaceholders === 'false' ? false : true;
|
||||||
|
|
||||||
|
return options;
|
||||||
|
})();
|
||||||
|
|
||||||
RegisterCommand(
|
RegisterCommand(
|
||||||
'oxmysql_debug',
|
'oxmysql_debug',
|
||||||
(source: number, args: string[]) => {
|
(source: number, args: string[]) => {
|
||||||
|
|||||||
+1
-47
@@ -1,53 +1,7 @@
|
|||||||
import { createPool, Pool } from 'mysql2';
|
import { createPool, Pool } from 'mysql2';
|
||||||
import { mysql_connection_string, mysql_transaction_isolation_level } from '../config';
|
import { connectionOptions, mysql_transaction_isolation_level } from '../config';
|
||||||
import { typeCast } from '../utils/typeCast';
|
import { typeCast } from '../utils/typeCast';
|
||||||
|
|
||||||
export const parseUri = (connectionString: string) => {
|
|
||||||
const splitMatchGroups = connectionString.match(
|
|
||||||
new RegExp(
|
|
||||||
'^(?:([^:/?#.]+):)?(?://(?:([^/?#]*)@)?([\\w\\d\\-\\u0100-\\uffff.%]*)(?::([0-9]+))?)?([^?#]+)?(?:\\?([^#]*))?$'
|
|
||||||
)
|
|
||||||
) as RegExpMatchArray;
|
|
||||||
|
|
||||||
if (!splitMatchGroups) throw new Error(`mysql_connection_string structure was invalid (${connectionString})`);
|
|
||||||
|
|
||||||
const authTarget = splitMatchGroups[2] ? splitMatchGroups[2].split(':') : [];
|
|
||||||
|
|
||||||
const options = {
|
|
||||||
user: authTarget[0] || undefined,
|
|
||||||
password: authTarget[1] || undefined,
|
|
||||||
host: splitMatchGroups[3],
|
|
||||||
port: parseInt(splitMatchGroups[4]),
|
|
||||||
database: splitMatchGroups[5].replace(/^\/+/, ''),
|
|
||||||
...(splitMatchGroups[6] &&
|
|
||||||
splitMatchGroups[6].split('&').reduce<Record<string, string>>((connectionInfo, parameter) => {
|
|
||||||
const [key, value] = parameter.split('=');
|
|
||||||
connectionInfo[key] = value;
|
|
||||||
return connectionInfo;
|
|
||||||
}, {})),
|
|
||||||
};
|
|
||||||
|
|
||||||
return options;
|
|
||||||
};
|
|
||||||
|
|
||||||
const connectionOptions = (() => {
|
|
||||||
const options = mysql_connection_string.includes('mysql://')
|
|
||||||
? parseUri(mysql_connection_string)
|
|
||||||
: 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<Record<string, string>>((connectionInfo, parameter) => {
|
|
||||||
const [key, value] = parameter.split('=');
|
|
||||||
connectionInfo[key] = value;
|
|
||||||
return connectionInfo;
|
|
||||||
}, {});
|
|
||||||
|
|
||||||
return options;
|
|
||||||
})();
|
|
||||||
|
|
||||||
let pool: Pool;
|
let pool: Pool;
|
||||||
let serverReady = false;
|
let serverReady = false;
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user