mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-02 22:17:39 +00:00
This adds support for the other type of Mysql connection string specified by mysql async. Support for both strings and their respective options are necessary to support backwards compatibility of existing server configurations. This also adds support for jest & unit testing on the server/client and some unit tests for some of the connection string parsing. It runs these test and prettier in a pre-commit hook.
73 lines
2.0 KiB
TypeScript
73 lines
2.0 KiB
TypeScript
export const CONNECTION_STRING = "mysql_connection_string";
|
|
export const DEFAULT_PORT = 3306;
|
|
export const DEFAULT_HOST = "localhost";
|
|
export const DEFAULT_USER = "root";
|
|
|
|
|
|
interface Map {
|
|
[key: string]: string;
|
|
}
|
|
|
|
|
|
/**
|
|
* parse the connection string from the format server=127.0.0.1;database=es_extended;userid=user;password=pass
|
|
* @param connectionString - mysql_connection_string value
|
|
*/
|
|
export function parseSemiColonFormat(connectionString: string): Map {
|
|
const parts = connectionString.split(";");
|
|
if (parts.length === 1) {
|
|
throw new Error(
|
|
`Connection string ${connectionString} is in the incorrect format.`
|
|
);
|
|
}
|
|
|
|
return parts.reduce((connectionInfo: Map, part) => {
|
|
const [key, value] = part.split("=");
|
|
connectionInfo[key] = value;
|
|
return connectionInfo;
|
|
}, {});
|
|
}
|
|
|
|
/**
|
|
* allowed variable names: host | server | data source | datasource | addr | address
|
|
* @param config - database config variables parsed from mysql_connection_string
|
|
*/
|
|
export function getServerHost(config: Map): string {
|
|
return (
|
|
config.host ||
|
|
config.server ||
|
|
config["data source"] ||
|
|
config.datasource ||
|
|
config.addr ||
|
|
config.address ||
|
|
DEFAULT_HOST
|
|
);
|
|
}
|
|
|
|
/**
|
|
* allowed variable names: user | user id | userid | user name | username | uid
|
|
* @param config - database config variables parsed from mysql_connection_string
|
|
*/
|
|
export function getUserId(config: Map): string {
|
|
return (
|
|
config.user ||
|
|
config["user id"] ||
|
|
config.userid ||
|
|
config["user name"] ||
|
|
config.username ||
|
|
config.uid ||
|
|
DEFAULT_USER
|
|
);
|
|
}
|
|
|
|
/**
|
|
* allowed variable names: password | pwd
|
|
* @param config - database config variables parsed from mysql_connection_string
|
|
*/
|
|
export function getPassword(config: Map): string {
|
|
const password = config.password || config.pwd;
|
|
if (!password) {
|
|
throw new Error("Password is a required field");
|
|
}
|
|
return password;
|
|
} |