mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-01 22:21:51 +00:00
refactor(database): new URI parser
This commit is contained in:
1 parent
c07fa4a451
commit
5d69db51da
2 files changed
+47
-19
No files matched your search
@@ -19,21 +19,3 @@ export const mysql_transaction_isolation_level = (() => {
|
||||
return `${query} READ COMMITTED`;
|
||||
}
|
||||
})();
|
||||
|
||||
export const connectionOptions = (() => {
|
||||
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<Record<string, string>>((connectionInfo, parameter) => {
|
||||
const [key, value] = parameter.split('=');
|
||||
connectionInfo[key] = value;
|
||||
return connectionInfo;
|
||||
}, {});
|
||||
|
||||
return options;
|
||||
})();
|
||||
+47
-1
@@ -1,7 +1,53 @@
|
||||
import { createPool, Pool } from 'mysql2';
|
||||
import { connectionOptions, mysql_transaction_isolation_level } from '../config';
|
||||
import { mysql_connection_string, mysql_transaction_isolation_level } from '../config';
|
||||
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 serverReady = false;
|
||||
|
||||
|
||||
Reference in new issue
Block a user