mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-03 14:38:50 +00:00
refactor(execute): execute queries after first tick
Force queries to wait until the server has ticked before sending anything.
This commit is contained in:
1 parent
cd2e73953f
commit
193da3e6c7
2 files changed
+24
-12
No files matched your search
+23
-1
@@ -1,9 +1,30 @@
|
|||||||
import { pool } from './pool';
|
import { pool } from './pool';
|
||||||
import { parseParameters } from './parser';
|
import { parseParameters } from './parser';
|
||||||
import { slowQueryWarning, debug, resourceName } from './config';
|
import { slowQueryWarning, debug, resourceName, isolationLevel } from './config';
|
||||||
import { FormatError } from './errors';
|
import { FormatError } from './errors';
|
||||||
|
|
||||||
|
let isReady = false;
|
||||||
|
|
||||||
|
const serverReady = () => {
|
||||||
|
return new Promise(() => {
|
||||||
|
setTimeout(() => {
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
setImmediate(async () => {
|
||||||
|
try {
|
||||||
|
await pool.query(isolationLevel);
|
||||||
|
isReady = true;
|
||||||
|
console.log(`^2Database server connection established!^0`);
|
||||||
|
} catch (error) {
|
||||||
|
console.log(`^3Unable to establish a connection to the database! [${error.code}]\n${error.message}^0`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const execute = async (query, parameters, resource) => {
|
const execute = async (query, parameters, resource) => {
|
||||||
|
if (!isReady) await serverReady();
|
||||||
try {
|
try {
|
||||||
[query, parameters] = parseParameters(query, parameters);
|
[query, parameters] = parseParameters(query, parameters);
|
||||||
|
|
||||||
@@ -45,6 +66,7 @@ const queryType = (query) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const preparedStatement = async (query, parameters, resource) => {
|
const preparedStatement = async (query, parameters, resource) => {
|
||||||
|
if (!isReady) await serverReady();
|
||||||
ScheduleResourceTick(resourceName);
|
ScheduleResourceTick(resourceName);
|
||||||
const connection = await pool.getConnection();
|
const connection = await pool.getConnection();
|
||||||
try {
|
try {
|
||||||
|
|||||||
+1
-11
@@ -1,16 +1,6 @@
|
|||||||
import { pool } from './pool';
|
|
||||||
import { execute, preparedStatement } from './execute';
|
import { execute, preparedStatement } from './execute';
|
||||||
import { transaction } from './transaction';
|
import { transaction } from './transaction';
|
||||||
import { debug, isolationLevel } from './config';
|
import { debug } from './config';
|
||||||
|
|
||||||
setImmediate(async () => {
|
|
||||||
try {
|
|
||||||
await pool.query(isolationLevel);
|
|
||||||
console.log(`^2Database server connection established!^0`);
|
|
||||||
} catch (error) {
|
|
||||||
console.log(`^3Unable to establish a connection to the database! [${error.code}]\n${error.message}^0`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const safeCallback = (callback, result) => {
|
const safeCallback = (callback, result) => {
|
||||||
if (typeof callback === 'function') callback(result);
|
if (typeof callback === 'function') callback(result);
|
||||||
|
|||||||
Reference in new issue
Block a user