mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-04 06:28:53 +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 { setDebug } from '../config';
|
||||||
import { connectionOptions, mysql_transaction_isolation_level, setDebug } from '../config';
|
import { getPoolConnection } from './connection';
|
||||||
|
|
||||||
let pool: Pool;
|
setTimeout(() => {
|
||||||
let isServerConnected = false;
|
setDebug();
|
||||||
let dbVersion = '';
|
getPoolConnection();
|
||||||
|
});
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
setDebug();
|
setDebug();
|
||||||
}, 1000);
|
}, 1000);
|
||||||
|
|
||||||
setTimeout(async () => {
|
export * from './connection';
|
||||||
setDebug();
|
export * from './rawQuery';
|
||||||
setConnectionPool();
|
export * from './rawExecute';
|
||||||
|
export * from './rawTransaction';
|
||||||
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 };
|
|
||||||
@@ -1,10 +1,8 @@
|
|||||||
import { pool } from '.';
|
|
||||||
import { logError, profileBatchStatements, runProfiler } from '../logger';
|
import { logError, profileBatchStatements, runProfiler } from '../logger';
|
||||||
import { CFXCallback, CFXParameters, QueryType } from '../types';
|
import { CFXCallback, CFXParameters, QueryType } from '../types';
|
||||||
import { parseResponse } from '../utils/parseResponse';
|
import { parseResponse } from '../utils/parseResponse';
|
||||||
import { executeType, parseExecute } from '../utils/parseExecute';
|
import { executeType, parseExecute } from '../utils/parseExecute';
|
||||||
import { scheduleTick } from '../utils/scheduleTick';
|
import { getPoolConnection } from './connection';
|
||||||
import { isServerConnected, waitForConnection } from '../database';
|
|
||||||
import { setCallback } from '../utils/setCallback';
|
import { setCallback } from '../utils/setCallback';
|
||||||
|
|
||||||
export const rawExecute = async (
|
export const rawExecute = async (
|
||||||
@@ -28,11 +26,9 @@ export const rawExecute = async (
|
|||||||
return logError(invokingResource, cb, isPromise, query, err.message);
|
return logError(invokingResource, cb, isPromise, query, err.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isServerConnected) await waitForConnection();
|
const connection = await getPoolConnection();
|
||||||
|
|
||||||
scheduleTick();
|
if (!connection) return;
|
||||||
|
|
||||||
const connection = await pool.getConnection();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const hasProfiler = await runProfiler(connection, invokingResource);
|
const hasProfiler = await runProfiler(connection, invokingResource);
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
import { pool, isServerConnected, waitForConnection } from '.';
|
|
||||||
import { parseArguments } from '../utils/parseArguments';
|
import { parseArguments } from '../utils/parseArguments';
|
||||||
import { setCallback } from '../utils/setCallback';
|
import { setCallback } from '../utils/setCallback';
|
||||||
import { parseResponse } from '../utils/parseResponse';
|
import { parseResponse } from '../utils/parseResponse';
|
||||||
import { logQuery, logError, runProfiler } from '../logger';
|
import { logQuery, logError, runProfiler } from '../logger';
|
||||||
import type { CFXCallback, CFXParameters } from '../types';
|
import type { CFXCallback, CFXParameters } from '../types';
|
||||||
import type { QueryType } from '../types';
|
import type { QueryType } from '../types';
|
||||||
import { scheduleTick } from '../utils/scheduleTick';
|
import { getPoolConnection } from './connection';
|
||||||
import { RowDataPacket } from 'mysql2';
|
import { RowDataPacket } from 'mysql2';
|
||||||
|
|
||||||
export const rawQuery = async (
|
export const rawQuery = async (
|
||||||
@@ -23,11 +22,9 @@ export const rawQuery = async (
|
|||||||
return logError(invokingResource, cb, isPromise, `Query: ${query}`, err.message);
|
return logError(invokingResource, cb, isPromise, `Query: ${query}`, err.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isServerConnected) await waitForConnection();
|
const connection = await getPoolConnection();
|
||||||
|
|
||||||
scheduleTick();
|
if (!connection) return;
|
||||||
|
|
||||||
const connection = await pool.getConnection();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const hasProfiler = await runProfiler(connection, invokingResource);
|
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 { logError, profileBatchStatements, runProfiler } from '../logger';
|
||||||
import { CFXCallback, CFXParameters, TransactionQuery } from '../types';
|
import { CFXCallback, CFXParameters, TransactionQuery } from '../types';
|
||||||
import { parseTransaction } from '../utils/parseTransaction';
|
import { parseTransaction } from '../utils/parseTransaction';
|
||||||
import { scheduleTick } from '../utils/scheduleTick';
|
|
||||||
import { setCallback } from '../utils/setCallback';
|
import { setCallback } from '../utils/setCallback';
|
||||||
|
|
||||||
const transactionError = (queries: { query: string; params?: CFXParameters }[], parameters: CFXParameters) => {
|
const transactionError = (queries: { query: string; params?: CFXParameters }[], parameters: CFXParameters) => {
|
||||||
@@ -27,11 +26,10 @@ export const rawTransaction = async (
|
|||||||
return logError(invokingResource, cb, isPromise, err.message);
|
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;
|
let response = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
+2
-5
@@ -1,8 +1,5 @@
|
|||||||
import { CFXCallback, CFXParameters, TransactionQuery } from './types';
|
import type { CFXCallback, CFXParameters, TransactionQuery } from './types';
|
||||||
import { rawQuery } from './database/rawQuery';
|
import { rawQuery, rawExecute, rawTransaction, isServerConnected, waitForConnection } from './database';
|
||||||
import { rawTransaction } from './database/rawTransaction';
|
|
||||||
import { rawExecute } from './database/rawExecute';
|
|
||||||
import { isServerConnected, waitForConnection } from './database';
|
|
||||||
import('./update');
|
import('./update');
|
||||||
|
|
||||||
const MySQL = {} as Record<string, Function>;
|
const MySQL = {} as Record<string, Function>;
|
||||||
|
|||||||
Reference in new issue
Block a user