mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-02 22:17:39 +00:00
91 lines
3.0 KiB
TypeScript
91 lines
3.0 KiB
TypeScript
import mysql from 'mysql2/promise';
|
|
import { ConnectionStringParser } from 'connection-string-parser';
|
|
import {
|
|
CONNECTION_STRING,
|
|
DEFAULT_PORT,
|
|
parseSemiColonFormat,
|
|
getServerHost,
|
|
getUserId,
|
|
getPassword,
|
|
} from './db_utils';
|
|
import { mainLogger } from './sv_logger';
|
|
|
|
// we require set mysql_connection_string to be set in the config
|
|
const mysqlConnectionString = GetConvar(CONNECTION_STRING, 'none');
|
|
if (mysqlConnectionString === 'none') {
|
|
const error = new Error(
|
|
`No connection string provided. make sure "${CONNECTION_STRING}" is set in your config.`,
|
|
);
|
|
mainLogger.error(error.message);
|
|
throw error;
|
|
}
|
|
|
|
/**
|
|
* Most fivem servers utilize fivem-mysql-async (https://brouznouf.github.io/fivem-mysql-async/) and
|
|
* define a environment variable "mysql_connection_string" in their configurations. We will try to
|
|
* maintain backwards compatibility with this.
|
|
*
|
|
* fivem-mysql-async allows for two different connection string formats defined here:
|
|
* https://brouznouf.github.io/fivem-mysql-async/config/ and we need to handle both of them.
|
|
*/
|
|
export function generateConnectionPool() {
|
|
try {
|
|
if (mysqlConnectionString.includes('database=')) {
|
|
// This is checking for this format:
|
|
// set mysql_connection_string "server=127.0.0.1;database=es_extended;userid=user;password=pass"
|
|
const config = parseSemiColonFormat(mysqlConnectionString);
|
|
return mysql.createPool({
|
|
host: getServerHost(config),
|
|
user: getUserId(config),
|
|
port: config.port ? parseInt(config.port) : DEFAULT_PORT,
|
|
password: getPassword(config),
|
|
database: config.database,
|
|
waitForConnections: true,
|
|
connectionLimit: 10,
|
|
queueLimit: 0,
|
|
});
|
|
} else {
|
|
// This is checking for this format:
|
|
// set mysql_connection_string "mysql://root:[email protected]/es_extended?charset=utf8mb4"
|
|
const connectionStringParser = new ConnectionStringParser({
|
|
scheme: 'mysql',
|
|
hosts: [],
|
|
});
|
|
const connectionOjbect = connectionStringParser.parse(mysqlConnectionString);
|
|
|
|
return mysql.createPool({
|
|
host: connectionOjbect.hosts[0].host,
|
|
user: connectionOjbect.username,
|
|
password: connectionOjbect.password,
|
|
database: connectionOjbect.endpoint,
|
|
waitForConnections: true,
|
|
connectionLimit: 10,
|
|
queueLimit: 0,
|
|
});
|
|
}
|
|
} catch (e) {
|
|
mainLogger.error(`SQL Connection Pool Error: ${e.message}`, {
|
|
connection: mysqlConnectionString,
|
|
});
|
|
}
|
|
}
|
|
|
|
export const pool = generateConnectionPool();
|
|
|
|
export async function withTransaction(queries: Promise<any>[]): Promise<any[]> {
|
|
const connection = await pool.getConnection();
|
|
connection.beginTransaction();
|
|
|
|
try {
|
|
const results = await Promise.all(queries);
|
|
await connection.commit();
|
|
await connection.release();
|
|
return results;
|
|
} catch (err) {
|
|
mainLogger.warn(`Error when submitting queries in transaction, ${err.message}`);
|
|
await connection.rollback();
|
|
await connection.release();
|
|
return Promise.reject(err);
|
|
}
|
|
}
|