Skip to content

Commit

Permalink
fix(packages/database): hopefully shit shit works
Browse files Browse the repository at this point in the history
  • Loading branch information
itschip committed Jan 13, 2024
1 parent b32eb08 commit a3798e7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
4 changes: 2 additions & 2 deletions apps/game/server/boot/boot.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class _BootDb {
* @returns Boolean - If the player table exists.
**/
async doesPlayerTableExist(): Promise<boolean> {
console.log("CONFIG DB PLAYERTABLE", config.database.playerTable)
console.log('CONFIG DB PLAYERTABLE', config.database.playerTable);

const tableSchema = parseUri(mysqlConnectionString).database;

Expand All @@ -30,7 +30,7 @@ export class _BootDb {
* @returns Array<string> - String array of column names.
**/
async getPlayerTableColumns(): Promise<string[]> {
console.log("CONFIG DB PLAYERTABLE 2", config.database.playerTable)
console.log('CONFIG DB PLAYERTABLE 2', config.database.playerTable);
const query = `SHOW COLUMNS IN ${config.database.playerTable}`;
const [results] = await DbInterface._rawExec(query, []);

Expand Down
56 changes: 45 additions & 11 deletions packages/database/src/db/parseUri.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@

import { ConnectionOptions } from 'mysql2';
interface NPWDConnectionOptions extends ConnectionOptions {
driver: string;
username?: string
username?: string;
}

export const parseUri = (connectionUri: string) => {
// oxmysql stuff below - i dont want to deal with this right now
export const parseUri2 = (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 parseUri = (connectionUri: string) => {
if (connectionUri.includes('mysql://')) {
const parsedUrl = new URL(connectionUri);
const options: NPWDConnectionOptions = {
Expand All @@ -23,30 +52,35 @@ export const parseUri = (connectionUri: string) => {
} catch (err) {
// Otherwise assume it is a plain string
// convert 'username' to user to match driver type
if(key === 'username') options['user'] = value;
if (key === 'username') options['user'] = value;
else (options as Record<typeof key, any>)[key] = value;
}
});
// we only want this if we miss both password & user or database
if (!options.password && !options.user || !options.database) {
const regex = new RegExp('^(?:([^:/?#.]+):)?(?://(?:([^/?]*):([^/?]*)@)?([[A-Za-z0-9_.]+]*)(?::([0-9]+))?)?(?:\\\\?([^#]*))?$', '')
if ((!options.password && !options.user) || !options.database) {
const regex = new RegExp(
'^(?:([^:/?#.]+):)?(?://(?:([^/?]*):([^/?]*)@)?([[A-Za-z0-9_.]+]*)(?::([0-9]+))?)?(?:\\\\?([^#]*))?$',
'',
);
const specialCharactersRegex = regex.exec(connectionUri);
if (specialCharactersRegex) {
options.user = specialCharactersRegex[2] || void 0;
options.password = specialCharactersRegex[3] || void 0;
options.host = specialCharactersRegex[4];
options.port = parseInt(specialCharactersRegex[5]);
options.database = specialCharactersRegex[6].replace(/^\/+/, "");
options.database = specialCharactersRegex[6].replace(/^\/+/, '');
}
}
// we need to delete any empty keys, the driver really hates empty strings in the options.
for(let key in options) !(options as Record<typeof key, any>)[key] && delete (options as Record<typeof key, any>)[key]
for (let key in options)
!(options as Record<typeof key, any>)[key] &&
delete (options as Record<typeof key, any>)[key];
return options;
}
}
return connectionUri
.replace(/(?:host(?:name)|ip|server|data\s?source|addr(?:ess)?)=/gi, 'host=')
.replace(/(?:user\s?(?:id|name)?|uid)=/gi, 'user=')
Expand All @@ -58,4 +92,4 @@ export const parseUri = (connectionUri: string) => {
connectionInfo[key] = value;
return connectionInfo;
}, {});
};
}; */
15 changes: 13 additions & 2 deletions packages/database/src/db/pool.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import mysql from 'mysql2/promise';
import { CONNECTION_STRING, parseSemiColonFormat } from './db_utils';
import { mainLogger } from '@npwd/logger/server';
import { parseUri2 } from './parseUri';

// we require set mysql_connection_string to be set in the config
const mysqlConnectionString = GetConvar(CONNECTION_STRING, 'none');
Expand All @@ -23,8 +24,18 @@ if (mysqlConnectionString === 'none') {
export function generateConnectionPool() {
try {
const config = mysqlConnectionString.includes('mysql://')
? { uri: mysqlConnectionString }
: parseSemiColonFormat(mysqlConnectionString);
? parseUri2(mysqlConnectionString)
: mysqlConnectionString
.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('=');
if (key) connectionInfo[key] = value;
return connectionInfo;
}, {});

return mysql.createPool({
connectTimeout: 60000,
Expand Down

0 comments on commit a3798e7

Please sign in to comment.