mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-02 06:28:54 +00:00
refactor(database): move pool and connection variables
Mostly reorganising module. Added getPoolConnection helper.
This commit is contained in:
1 parent
b3f4a7aa6b
commit
0398abc501
6 files changed
+78
-69
No files matched your search
@@ -0,0 +1,57 @@
|
||||
import { createPool, Pool, RowDataPacket } from 'mysql2/promise';
|
||||
import { connectionOptions, mysql_transaction_isolation_level } from '../config';
|
||||
import { scheduleTick } from '../utils/scheduleTick';
|
||||
|
||||
export let pool: Pool;
|
||||
export let isServerConnected = false;
|
||||
export let dbVersion = '';
|
||||
|
||||
export async function waitForConnection() {
|
||||
if (!isServerConnected) {
|
||||
return await new Promise<boolean>((resolve) => {
|
||||
(function wait() {
|
||||
if (isServerConnected) {
|
||||
return resolve(true);
|
||||
}
|
||||
setTimeout(wait);
|
||||
})();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function createConnectionPool() {
|
||||
try {
|
||||
pool = createPool(connectionOptions);
|
||||
|
||||
pool.on('connection', (connection) => {
|
||||
connection.query(mysql_transaction_isolation_level);
|
||||
});
|
||||
|
||||
const connection = await pool.getConnection();
|
||||
const [result] = await (<Promise<RowDataPacket[]>>connection.query('SELECT VERSION() as version'));
|
||||
dbVersion = `^5[${result[0].version}]`;
|
||||
|
||||
connection.release();
|
||||
console.log(`${dbVersion} ^2Database server connection established!^0`);
|
||||
|
||||
isServerConnected = true;
|
||||
} catch (err: any) {
|
||||
isServerConnected = false;
|
||||
|
||||
console.log(
|
||||
`^3Unable to establish a connection to the database (${err.code})!\n^1Error ${err.errno}: ${err.message}^0`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getPoolConnection() {
|
||||
if (!pool) {
|
||||
await createConnectionPool();
|
||||
|
||||
if (!pool) return;
|
||||
}
|
||||
|
||||
scheduleTick();
|
||||
|
||||
return pool.getConnection();
|
||||
}
|
||||
+10
-46
@@ -1,52 +1,16 @@
|
||||
import { createPool, Pool, RowDataPacket } from 'mysql2/promise';
|
||||
import { connectionOptions, mysql_transaction_isolation_level, setDebug } from '../config';
|
||||
import { setDebug } from '../config';
|
||||
import { getPoolConnection } from './connection';
|
||||
|
||||
let pool: Pool;
|
||||
let isServerConnected = false;
|
||||
let dbVersion = '';
|
||||
|
||||
export async function waitForConnection() {
|
||||
if (!isServerConnected) {
|
||||
return await new Promise<boolean>((resolve) => {
|
||||
(function wait() {
|
||||
if (isServerConnected) {
|
||||
return resolve(true);
|
||||
}
|
||||
setTimeout(wait);
|
||||
})();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function setConnectionPool() {
|
||||
pool = createPool(connectionOptions);
|
||||
|
||||
pool.on('connection', (connection) => {
|
||||
connection.query(mysql_transaction_isolation_level);
|
||||
});
|
||||
}
|
||||
setTimeout(() => {
|
||||
setDebug();
|
||||
getPoolConnection();
|
||||
});
|
||||
|
||||
setInterval(() => {
|
||||
setDebug();
|
||||
}, 1000);
|
||||
|
||||
setTimeout(async () => {
|
||||
setDebug();
|
||||
setConnectionPool();
|
||||
|
||||
try {
|
||||
const connection = await pool.getConnection();
|
||||
const [result] = await <Promise<RowDataPacket[]>> connection.query('SELECT VERSION() as version')
|
||||
dbVersion = `^5[${result[0].version}]`
|
||||
connection.release();
|
||||
|
||||
console.log(`${dbVersion} ^2Database server connection established!^0`);
|
||||
isServerConnected = true;
|
||||
} catch (err: any) {
|
||||
console.log(
|
||||
`^3Unable to establish a connection to the database (${err.code})!\n^1Error ${err.errno}: ${err.message}^0`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
export { pool, isServerConnected, dbVersion };
|
||||
export * from './connection';
|
||||
export * from './rawQuery';
|
||||
export * from './rawExecute';
|
||||
export * from './rawTransaction';
|
||||
@@ -1,10 +1,8 @@
|
||||
import { pool } from '.';
|
||||
import { logError, profileBatchStatements, runProfiler } from '../logger';
|
||||
import { CFXCallback, CFXParameters, QueryType } from '../types';
|
||||
import { parseResponse } from '../utils/parseResponse';
|
||||
import { executeType, parseExecute } from '../utils/parseExecute';
|
||||
import { scheduleTick } from '../utils/scheduleTick';
|
||||
import { isServerConnected, waitForConnection } from '../database';
|
||||
import { getPoolConnection } from './connection';
|
||||
import { setCallback } from '../utils/setCallback';
|
||||
|
||||
export const rawExecute = async (
|
||||
@@ -28,11 +26,9 @@ export const rawExecute = async (
|
||||
return logError(invokingResource, cb, isPromise, query, err.message);
|
||||
}
|
||||
|
||||
if (!isServerConnected) await waitForConnection();
|
||||
const connection = await getPoolConnection();
|
||||
|
||||
scheduleTick();
|
||||
|
||||
const connection = await pool.getConnection();
|
||||
if (!connection) return;
|
||||
|
||||
try {
|
||||
const hasProfiler = await runProfiler(connection, invokingResource);
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import { pool, isServerConnected, waitForConnection } from '.';
|
||||
import { parseArguments } from '../utils/parseArguments';
|
||||
import { setCallback } from '../utils/setCallback';
|
||||
import { parseResponse } from '../utils/parseResponse';
|
||||
import { logQuery, logError, runProfiler } from '../logger';
|
||||
import type { CFXCallback, CFXParameters } from '../types';
|
||||
import type { QueryType } from '../types';
|
||||
import { scheduleTick } from '../utils/scheduleTick';
|
||||
import { getPoolConnection } from './connection';
|
||||
import { RowDataPacket } from 'mysql2';
|
||||
|
||||
export const rawQuery = async (
|
||||
@@ -23,11 +22,9 @@ export const rawQuery = async (
|
||||
return logError(invokingResource, cb, isPromise, `Query: ${query}`, err.message);
|
||||
}
|
||||
|
||||
if (!isServerConnected) await waitForConnection();
|
||||
const connection = await getPoolConnection();
|
||||
|
||||
scheduleTick();
|
||||
|
||||
const connection = await pool.getConnection();
|
||||
if (!connection) return;
|
||||
|
||||
try {
|
||||
const hasProfiler = await runProfiler(connection, invokingResource);
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { pool, isServerConnected, waitForConnection } from '.';
|
||||
import { getPoolConnection } from './connection';
|
||||
import { logError, profileBatchStatements, runProfiler } from '../logger';
|
||||
import { CFXCallback, CFXParameters, TransactionQuery } from '../types';
|
||||
import { parseTransaction } from '../utils/parseTransaction';
|
||||
import { scheduleTick } from '../utils/scheduleTick';
|
||||
import { setCallback } from '../utils/setCallback';
|
||||
|
||||
const transactionError = (queries: { query: string; params?: CFXParameters }[], parameters: CFXParameters) => {
|
||||
@@ -27,11 +26,10 @@ export const rawTransaction = async (
|
||||
return logError(invokingResource, cb, isPromise, err.message);
|
||||
}
|
||||
|
||||
if (!isServerConnected) await waitForConnection();
|
||||
const connection = await getPoolConnection();
|
||||
|
||||
scheduleTick();
|
||||
if (!connection) return;
|
||||
|
||||
const connection = await pool.getConnection();
|
||||
let response = false;
|
||||
|
||||
try {
|
||||
|
||||
+2
-5
@@ -1,8 +1,5 @@
|
||||
import { CFXCallback, CFXParameters, TransactionQuery } from './types';
|
||||
import { rawQuery } from './database/rawQuery';
|
||||
import { rawTransaction } from './database/rawTransaction';
|
||||
import { rawExecute } from './database/rawExecute';
|
||||
import { isServerConnected, waitForConnection } from './database';
|
||||
import type { CFXCallback, CFXParameters, TransactionQuery } from './types';
|
||||
import { rawQuery, rawExecute, rawTransaction, isServerConnected, waitForConnection } from './database';
|
||||
import('./update');
|
||||
|
||||
const MySQL = {} as Record<string, Function>;
|
||||
|
||||
Reference in new issue
Block a user