Updates
This commit is contained in:
parent
b38ddc9f21
commit
6acf0f2cd6
8
dist/index.d.ts
vendored
8
dist/index.d.ts
vendored
@ -1,8 +1,8 @@
|
||||
import type { Pool } from "mariadb";
|
||||
import type { Connection } from "mariadb";
|
||||
declare global {
|
||||
var DSQL_DB_CONN: Pool | undefined;
|
||||
var DSQL_READ_ONLY_DB_CONN: Pool | undefined;
|
||||
var DSQL_FULL_ACCESS_DB_CONN: Pool | undefined;
|
||||
var DSQL_DB_CONN: Connection | undefined;
|
||||
var DSQL_READ_ONLY_DB_CONN: Connection | undefined;
|
||||
var DSQL_FULL_ACCESS_DB_CONN: Connection | undefined;
|
||||
var DSQL_USE_LOCAL: boolean | undefined;
|
||||
var ERROR_CALLBACK: ErrorCallback | undefined;
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ const grab_dsql_connection_1 = __importDefault(require("../../utils/grab-dsql-co
|
||||
function dbHandler(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ query, values, noErrorLogs, }) {
|
||||
var _b;
|
||||
const CONNECTION = (0, grab_dsql_connection_1.default)();
|
||||
const CONNECTION = yield (0, grab_dsql_connection_1.default)();
|
||||
let results;
|
||||
try {
|
||||
if (query && values) {
|
||||
@ -54,7 +54,12 @@ function dbHandler(_a) {
|
||||
yield (CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end());
|
||||
}
|
||||
if (results) {
|
||||
return JSON.parse(JSON.stringify(results));
|
||||
if (Array.isArray(results)) {
|
||||
return Array.from(results);
|
||||
}
|
||||
else {
|
||||
return results;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
|
@ -53,7 +53,6 @@ function fullAccessDbHandler(_a) {
|
||||
return error.message;
|
||||
}
|
||||
finally {
|
||||
DB_CONN === null || DB_CONN === void 0 ? void 0 : DB_CONN.end();
|
||||
}
|
||||
/**
|
||||
* Return results
|
||||
|
@ -1,4 +0,0 @@
|
||||
/**
|
||||
* # No Database DB Handler
|
||||
*/
|
||||
export default function noDatabaseDbHandler(queryString: string): Promise<any>;
|
@ -1,64 +0,0 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = noDatabaseDbHandler;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const serverError_1 = __importDefault(require("./serverError"));
|
||||
const NO_DB_HANDLER_1 = __importDefault(require("../../utils/backend/global-db/NO_DB_HANDLER"));
|
||||
/**
|
||||
* # No Database DB Handler
|
||||
*/
|
||||
function noDatabaseDbHandler(queryString) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
var _a;
|
||||
((_a = process.env.NODE_ENV) === null || _a === void 0 ? void 0 : _a.match(/dev/)) &&
|
||||
fs_1.default.appendFileSync("./.tmp/sqlQuery.sql", queryString + "\n" + Date() + "\n\n\n", "utf8");
|
||||
/**
|
||||
* Declare variables
|
||||
*
|
||||
* @description Declare "results" variable
|
||||
*/
|
||||
let results;
|
||||
/**
|
||||
* Fetch from db
|
||||
*
|
||||
* @description Fetch data from db if no cache
|
||||
*/
|
||||
try {
|
||||
/** ********************* Run Query */
|
||||
results = yield (0, NO_DB_HANDLER_1.default)(queryString);
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
}
|
||||
catch ( /** @type {any} */error) {
|
||||
(0, serverError_1.default)({
|
||||
component: "noDatabaseDbHandler",
|
||||
message: error.message,
|
||||
});
|
||||
console.log("ERROR in noDatabaseDbHandler =>", error.message);
|
||||
}
|
||||
/**
|
||||
* Return results
|
||||
*
|
||||
* @description Return results add to cache if "req" param is passed
|
||||
*/
|
||||
if (results) {
|
||||
return results;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
@ -18,14 +18,28 @@ const conn_db_handler_1 = __importDefault(require("../../utils/db/conn-db-handle
|
||||
const mariadb_1 = __importDefault(require("mariadb"));
|
||||
function suDbHandler(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ query, database, user, values, }) {
|
||||
const connection = mariadb_1.default.createPool({
|
||||
const connection = yield mariadb_1.default.createConnection({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: database,
|
||||
charset: "utf8mb4",
|
||||
ssl: (0, grabDbSSL_1.default)(),
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
});
|
||||
// const connection = mariadb.createPool({
|
||||
// host: process.env.DSQL_DB_HOST,
|
||||
// user: process.env.DSQL_DB_USERNAME,
|
||||
// password: process.env.DSQL_DB_PASSWORD,
|
||||
// database: database,
|
||||
// charset: "utf8mb4",
|
||||
// ssl: grabDbSSL(),
|
||||
// supportBigNumbers: true,
|
||||
// bigNumberStrings: false,
|
||||
// dateStrings: true,
|
||||
// });
|
||||
const results = yield (0, conn_db_handler_1.default)(connection, query);
|
||||
return results;
|
||||
});
|
||||
|
@ -20,13 +20,16 @@ const grabDbSSL_1 = __importDefault(require("../../utils/backend/grabDbSSL"));
|
||||
function userDbHandler(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ query, user, database, debug, tableSchema, values, }) {
|
||||
const { fullName, host, username, password } = yield (0, grab_mariadb_main_user_for_user_1.default)({ user });
|
||||
const connection = mariadb_1.default.createPool({
|
||||
const connection = yield mariadb_1.default.createConnection({
|
||||
host,
|
||||
user: username,
|
||||
password: password,
|
||||
database: database,
|
||||
charset: "utf8mb4",
|
||||
ssl: (0, grabDbSSL_1.default)(),
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
});
|
||||
const results = yield (0, conn_db_handler_1.default)(connection, query);
|
||||
return results;
|
||||
|
@ -23,11 +23,11 @@ const conn_db_handler_1 = __importDefault(require("../../utils/db/conn-db-handle
|
||||
function varDatabaseDbHandler(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ queryString, queryValuesArray, database, tableSchema, debug, }) {
|
||||
var _b;
|
||||
let CONNECTION = (0, grab_dsql_connection_1.default)({ fa: true });
|
||||
let CONNECTION = yield (0, grab_dsql_connection_1.default)({ fa: true });
|
||||
if (global.DSQL_USE_LOCAL)
|
||||
CONNECTION = (0, grab_dsql_connection_1.default)({ local: true });
|
||||
CONNECTION = yield (0, grab_dsql_connection_1.default)({ local: true });
|
||||
if (database === null || database === void 0 ? void 0 : database.match(/^datasquirel$/))
|
||||
CONNECTION = (0, grab_dsql_connection_1.default)();
|
||||
CONNECTION = yield (0, grab_dsql_connection_1.default)();
|
||||
if (debug) {
|
||||
console.log(`varDatabaseDbHandler:query:`, queryString);
|
||||
console.log(`varDatabaseDbHandler:values:`, queryValuesArray);
|
||||
|
@ -40,7 +40,6 @@ function varReadOnlyDatabaseDbHandler(_a) {
|
||||
return error.message;
|
||||
}
|
||||
finally {
|
||||
DB_CONN === null || DB_CONN === void 0 ? void 0 : DB_CONN.end();
|
||||
}
|
||||
if (results) {
|
||||
const unparsedResults = results;
|
||||
|
@ -1 +0,0 @@
|
||||
export {};
|
@ -1,102 +0,0 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const mysql_1 = __importDefault(require("mysql"));
|
||||
const child_process_1 = require("child_process");
|
||||
const util_1 = require("util");
|
||||
function getConnection(config) {
|
||||
return mysql_1.default.createConnection(config);
|
||||
}
|
||||
function getMasterStatus(config) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const connection = getConnection(config);
|
||||
connection.query("SHOW MASTER STATUS", (error, results) => {
|
||||
connection.end();
|
||||
if (error)
|
||||
reject(error);
|
||||
else
|
||||
resolve(results[0]);
|
||||
});
|
||||
});
|
||||
}
|
||||
function syncDatabases() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const config = {
|
||||
host: "localhost",
|
||||
user: "root",
|
||||
password: "your_password",
|
||||
};
|
||||
let lastPosition = null; // Track last synced position
|
||||
while (true) {
|
||||
try {
|
||||
// Get current master status
|
||||
const { File, Position } = yield getMasterStatus(config);
|
||||
// Determine start position (use lastPosition or 4 if first run)
|
||||
const startPosition = lastPosition !== null ? lastPosition + 1 : 4;
|
||||
if (startPosition >= Position) {
|
||||
yield new Promise((resolve) => setTimeout(resolve, 5000)); // Wait 5 seconds if no new changes
|
||||
continue;
|
||||
}
|
||||
// Execute mysqlbinlog to get changes
|
||||
const execPromise = (0, util_1.promisify)(child_process_1.exec);
|
||||
const { stdout } = yield execPromise(`mysqlbinlog --database=db_master ${File} --start-position=${startPosition} --stop-position=${Position}`);
|
||||
if (stdout) {
|
||||
const connection = getConnection(Object.assign(Object.assign({}, config), { database: "db_slave" }));
|
||||
return new Promise((resolve, reject) => {
|
||||
connection.query(stdout, (error) => {
|
||||
connection.end();
|
||||
if (error)
|
||||
reject(error);
|
||||
else {
|
||||
lastPosition = Position;
|
||||
console.log(`Synced up to position ${Position} at ${new Date().toISOString()}`);
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error("Sync error:", error);
|
||||
}
|
||||
yield new Promise((resolve) => setTimeout(resolve, 5000)); // Check every 5 seconds
|
||||
}
|
||||
});
|
||||
}
|
||||
// Initialize db_slave with db_master data
|
||||
function initializeSlave() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const config = {
|
||||
host: "localhost",
|
||||
user: "root",
|
||||
password: "your_password",
|
||||
};
|
||||
try {
|
||||
yield (0, util_1.promisify)(child_process_1.exec)(`mysqldump -u ${config.user} -p${config.password} db_master > db_master_backup.sql`);
|
||||
yield (0, util_1.promisify)(child_process_1.exec)(`mysql -u ${config.user} -p${config.password} db_slave < db_master_backup.sql`);
|
||||
console.log("Slave initialized with master data");
|
||||
}
|
||||
catch (error) {
|
||||
console.error("Initialization error:", error);
|
||||
}
|
||||
});
|
||||
}
|
||||
// Run the sync process
|
||||
function main() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
yield initializeSlave();
|
||||
yield syncDatabases();
|
||||
});
|
||||
}
|
||||
main().catch(console.error);
|
4
dist/package-shared/shell/checkDb.js
vendored
4
dist/package-shared/shell/checkDb.js
vendored
@ -27,7 +27,7 @@ const grab_dsql_connection_1 = __importDefault(require("../utils/grab-dsql-conne
|
||||
*/
|
||||
(() => __awaiter(void 0, void 0, void 0, function* () {
|
||||
var _a;
|
||||
const CONNECTION = (0, grab_dsql_connection_1.default)();
|
||||
const CONNECTION = yield (0, grab_dsql_connection_1.default)();
|
||||
try {
|
||||
const result = yield CONNECTION.query("SELECT id,first_name,last_name FROM users LIMIT 3");
|
||||
console.log("Connection Query Success =>", result);
|
||||
@ -37,7 +37,7 @@ const grab_dsql_connection_1 = __importDefault(require("../utils/grab-dsql-conne
|
||||
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `Error Checking DB`, error);
|
||||
}
|
||||
finally {
|
||||
CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end();
|
||||
yield (CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end());
|
||||
process.exit();
|
||||
}
|
||||
}))();
|
||||
|
@ -27,7 +27,7 @@ const grab_dsql_connection_1 = __importDefault(require("../utils/grab-dsql-conne
|
||||
*/
|
||||
(() => __awaiter(void 0, void 0, void 0, function* () {
|
||||
var _a;
|
||||
const CONNECTION = (0, grab_dsql_connection_1.default)({ noDb: true });
|
||||
const CONNECTION = yield (0, grab_dsql_connection_1.default)({ noDb: true });
|
||||
/**
|
||||
* Switch Database
|
||||
*
|
||||
@ -42,7 +42,7 @@ const grab_dsql_connection_1 = __importDefault(require("../utils/grab-dsql-conne
|
||||
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `Error Testing External Server`, error);
|
||||
}
|
||||
finally {
|
||||
CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end();
|
||||
yield (CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end());
|
||||
process.exit();
|
||||
}
|
||||
}))();
|
||||
|
4
dist/package-shared/shell/updateSSLUsers.js
vendored
4
dist/package-shared/shell/updateSSLUsers.js
vendored
@ -28,7 +28,7 @@ const grab_sql_key_name_1 = __importDefault(require("../utils/grab-sql-key-name"
|
||||
*/
|
||||
(() => __awaiter(void 0, void 0, void 0, function* () {
|
||||
var _a, _b;
|
||||
const CONNECTION = (0, grab_dsql_connection_1.default)();
|
||||
const CONNECTION = yield (0, grab_dsql_connection_1.default)();
|
||||
try {
|
||||
const result = yield CONNECTION.query("SELECT user,host,ssl_type FROM mysql.user");
|
||||
const parsedResults = JSON.parse(JSON.stringify(result));
|
||||
@ -51,7 +51,7 @@ const grab_sql_key_name_1 = __importDefault(require("../utils/grab-sql-key-name"
|
||||
(_b = global.ERROR_CALLBACK) === null || _b === void 0 ? void 0 : _b.call(global, `Error Updating SSL Users`, error);
|
||||
}
|
||||
finally {
|
||||
CONNECTION.end();
|
||||
yield (CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end());
|
||||
process.exit();
|
||||
}
|
||||
}))();
|
||||
|
@ -21,12 +21,17 @@ const grab_dsql_connection_1 = __importDefault(require("../../grab-dsql-connecti
|
||||
function DB_HANDLER(query, values) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
var _a;
|
||||
const CONNECTION = (0, grab_dsql_connection_1.default)();
|
||||
const CONNECTION = yield (0, grab_dsql_connection_1.default)();
|
||||
try {
|
||||
if (!CONNECTION)
|
||||
throw new Error("No Connection provided to DB_HANDLER function!");
|
||||
const results = yield CONNECTION.query(query, values);
|
||||
return JSON.parse(JSON.stringify(results));
|
||||
if (Array.isArray(results)) {
|
||||
return Array.from(results);
|
||||
}
|
||||
else {
|
||||
return results;
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `DB_HANDLER Error`, error);
|
||||
|
@ -22,8 +22,8 @@ function DSQL_USER_DB_HANDLER(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ paradigm, queryString, queryValues, }) {
|
||||
var _b;
|
||||
const CONNECTION = paradigm == "Read Only"
|
||||
? (0, grab_dsql_connection_1.default)({ ro: true })
|
||||
: (0, grab_dsql_connection_1.default)({ fa: true });
|
||||
? yield (0, grab_dsql_connection_1.default)({ ro: true })
|
||||
: yield (0, grab_dsql_connection_1.default)({ fa: true });
|
||||
try {
|
||||
return yield (0, conn_db_handler_1.default)(CONNECTION, queryString, queryValues);
|
||||
}
|
||||
@ -32,7 +32,7 @@ function DSQL_USER_DB_HANDLER(_a) {
|
||||
return null;
|
||||
}
|
||||
finally {
|
||||
CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end();
|
||||
yield (CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -20,10 +20,15 @@ const grab_dsql_connection_1 = __importDefault(require("../../grab-dsql-connecti
|
||||
function LOCAL_DB_HANDLER(query, values) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
var _a;
|
||||
const MASTER = (0, grab_dsql_connection_1.default)();
|
||||
const CONNECTION = yield (0, grab_dsql_connection_1.default)();
|
||||
try {
|
||||
const results = yield MASTER.query(query, values);
|
||||
return JSON.parse(JSON.stringify(results));
|
||||
const results = yield CONNECTION.query(query, values);
|
||||
if (Array.isArray(results)) {
|
||||
return Array.from(results);
|
||||
}
|
||||
else {
|
||||
return results;
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `LOCAL_DB_HANDLER Error`, error);
|
||||
@ -33,7 +38,7 @@ function LOCAL_DB_HANDLER(query, values) {
|
||||
};
|
||||
}
|
||||
finally {
|
||||
yield (MASTER === null || MASTER === void 0 ? void 0 : MASTER.end());
|
||||
yield (CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -1,7 +1,4 @@
|
||||
/**
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
export default function NO_DB_HANDLER(query: string, values?: any[]): Promise<unknown> | {
|
||||
success: boolean;
|
||||
error: any;
|
||||
};
|
||||
export default function NO_DB_HANDLER(query: string, values?: any[]): Promise<unknown>;
|
||||
|
@ -1,4 +1,13 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
@ -9,32 +18,37 @@ const grab_dsql_connection_1 = __importDefault(require("../../grab-dsql-connecti
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
function NO_DB_HANDLER(query, values) {
|
||||
var _a;
|
||||
const CONNECTION = (0, grab_dsql_connection_1.default)();
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
CONNECTION.query(query, values)
|
||||
.then((results) => {
|
||||
CONNECTION.end();
|
||||
resolve(JSON.parse(JSON.stringify(results)));
|
||||
})
|
||||
.catch((err) => {
|
||||
CONNECTION.end();
|
||||
resolve({
|
||||
error: err.message,
|
||||
sql: err.sql,
|
||||
});
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
var _a;
|
||||
const CONNECTION = yield (0, grab_dsql_connection_1.default)();
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
CONNECTION.query(query, values)
|
||||
.then((results) => __awaiter(this, void 0, void 0, function* () {
|
||||
if (Array.isArray(results)) {
|
||||
resolve(Array.from(results));
|
||||
}
|
||||
else {
|
||||
resolve(results);
|
||||
}
|
||||
}))
|
||||
.catch((err) => __awaiter(this, void 0, void 0, function* () {
|
||||
resolve({
|
||||
error: err.message,
|
||||
sql: err.sql,
|
||||
});
|
||||
}))
|
||||
.finally(() => __awaiter(this, void 0, void 0, function* () {
|
||||
yield (CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end());
|
||||
}));
|
||||
});
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `NO_DB_HANDLER Error`, error);
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
finally {
|
||||
CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end();
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `NO_DB_HANDLER Error`, error);
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -1,7 +0,0 @@
|
||||
/**
|
||||
* # Root DB handler
|
||||
*/
|
||||
export default function ROOT_DB_HANDLER(query: string, values?: any[]): Promise<unknown> | {
|
||||
success: boolean;
|
||||
error: any;
|
||||
};
|
@ -1,40 +0,0 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = ROOT_DB_HANDLER;
|
||||
const grab_dsql_connection_1 = __importDefault(require("../../grab-dsql-connection"));
|
||||
/**
|
||||
* # Root DB handler
|
||||
*/
|
||||
function ROOT_DB_HANDLER(query, values) {
|
||||
var _a;
|
||||
const CONNECTION = (0, grab_dsql_connection_1.default)();
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
CONNECTION.query(query, values)
|
||||
.then((results) => {
|
||||
CONNECTION.end();
|
||||
resolve(JSON.parse(JSON.stringify(results)));
|
||||
})
|
||||
.catch((err) => {
|
||||
CONNECTION.end();
|
||||
resolve({
|
||||
error: err.message,
|
||||
sql: err.sql,
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `ROOT_DB_HANDLER Error`, error);
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
finally {
|
||||
CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end();
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
import { DSQLErrorObject } from "../../types";
|
||||
import type { ConnectionConfig, Pool } from "mariadb";
|
||||
import mariadb, { ConnectionConfig } from "mariadb";
|
||||
export type ConnDBHandlerQueryObject = {
|
||||
query: string;
|
||||
values?: (string | number | undefined)[];
|
||||
@ -16,9 +16,9 @@ type Return<ReturnType = any> = ReturnType | ReturnType[] | null | {
|
||||
*/
|
||||
export default function connDbHandler<ReturnType = any>(
|
||||
/**
|
||||
* MariaDB Connection Pool Object
|
||||
* MariaDB Connection
|
||||
*/
|
||||
connPool?: Pool,
|
||||
conn?: mariadb.Connection,
|
||||
/**
|
||||
* String Or `ConnDBHandlerQueryObject` Array
|
||||
*/
|
||||
|
13
dist/package-shared/utils/db/conn-db-handler.js
vendored
13
dist/package-shared/utils/db/conn-db-handler.js
vendored
@ -21,9 +21,9 @@ const debug_log_1 = __importDefault(require("../logging/debug-log"));
|
||||
*/
|
||||
function connDbHandler(
|
||||
/**
|
||||
* MariaDB Connection Pool Object
|
||||
* MariaDB Connection
|
||||
*/
|
||||
connPool,
|
||||
conn,
|
||||
/**
|
||||
* String Or `ConnDBHandlerQueryObject` Array
|
||||
*/
|
||||
@ -35,13 +35,13 @@ values, debug) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
var _a, _b;
|
||||
try {
|
||||
if (!connPool)
|
||||
if (!conn)
|
||||
throw new Error("No Connection Found!");
|
||||
if (!query)
|
||||
throw new Error("Query String Required!");
|
||||
let queryErrorArray = [];
|
||||
if (typeof query == "string") {
|
||||
const res = yield connPool.query(trimQuery(query), values);
|
||||
const res = yield conn.query(trimQuery(query), values);
|
||||
if (debug) {
|
||||
(0, debug_log_1.default)({
|
||||
log: res,
|
||||
@ -62,7 +62,7 @@ values, debug) {
|
||||
const queryObj = query[i];
|
||||
currentQueryError.sql = queryObj.query;
|
||||
currentQueryError.sqlValues = queryObj.values;
|
||||
const queryObjRes = yield connPool.query(trimQuery(queryObj.query), queryObj.values);
|
||||
const queryObjRes = yield conn.query(trimQuery(queryObj.query), queryObj.values);
|
||||
if (debug) {
|
||||
(0, debug_log_1.default)({
|
||||
log: queryObjRes,
|
||||
@ -79,6 +79,7 @@ values, debug) {
|
||||
}
|
||||
catch (error) {
|
||||
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `Connection DB Handler Query Error`, error);
|
||||
console.log("query", query);
|
||||
resArray.push(null);
|
||||
currentQueryError["error"] = error.message;
|
||||
queryErrorArray.push(currentQueryError);
|
||||
@ -117,7 +118,7 @@ values, debug) {
|
||||
};
|
||||
}
|
||||
finally {
|
||||
connPool === null || connPool === void 0 ? void 0 : connPool.end();
|
||||
yield (conn === null || conn === void 0 ? void 0 : conn.end());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Pool } from "mariadb";
|
||||
import { Connection } from "mariadb";
|
||||
type Param = {
|
||||
/**
|
||||
* Read Only?
|
||||
@ -20,5 +20,5 @@ type Param = {
|
||||
/**
|
||||
* # Grab General CONNECTION for DSQL
|
||||
*/
|
||||
export default function grabDSQLConnection(param?: Param): Pool;
|
||||
export default function grabDSQLConnection(param?: Param): Promise<Connection>;
|
||||
export {};
|
||||
|
107
dist/package-shared/utils/grab-dsql-connection.js
vendored
107
dist/package-shared/utils/grab-dsql-connection.js
vendored
@ -1,4 +1,13 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
@ -10,9 +19,58 @@ const grabDbSSL_1 = __importDefault(require("./backend/grabDbSSL"));
|
||||
* # Grab General CONNECTION for DSQL
|
||||
*/
|
||||
function grabDSQLConnection(param) {
|
||||
if (global.DSQL_USE_LOCAL || (param === null || param === void 0 ? void 0 : param.local)) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
if (global.DSQL_USE_LOCAL || (param === null || param === void 0 ? void 0 : param.local)) {
|
||||
return (global.DSQL_DB_CONN ||
|
||||
(yield mariadb_1.default.createConnection({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: (param === null || param === void 0 ? void 0 : param.noDb) ? undefined : process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: (0, grabDbSSL_1.default)(),
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
})));
|
||||
}
|
||||
if (param === null || param === void 0 ? void 0 : param.ro) {
|
||||
return (global.DSQL_READ_ONLY_DB_CONN ||
|
||||
(yield mariadb_1.default.createConnection({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_READ_ONLY_USERNAME,
|
||||
password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: (0, grabDbSSL_1.default)(),
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
})));
|
||||
}
|
||||
if (param === null || param === void 0 ? void 0 : param.fa) {
|
||||
return (global.DSQL_FULL_ACCESS_DB_CONN ||
|
||||
(yield mariadb_1.default.createConnection({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_FULL_ACCESS_USERNAME,
|
||||
password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: (0, grabDbSSL_1.default)(),
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
})));
|
||||
}
|
||||
return (global.DSQL_DB_CONN ||
|
||||
mariadb_1.default.createPool({
|
||||
(yield mariadb_1.default.createConnection({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
@ -22,44 +80,9 @@ function grabDSQLConnection(param) {
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: (0, grabDbSSL_1.default)(),
|
||||
}));
|
||||
}
|
||||
if (param === null || param === void 0 ? void 0 : param.ro) {
|
||||
return (global.DSQL_READ_ONLY_DB_CONN ||
|
||||
mariadb_1.default.createPool({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_READ_ONLY_USERNAME,
|
||||
password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: (0, grabDbSSL_1.default)(),
|
||||
}));
|
||||
}
|
||||
if (param === null || param === void 0 ? void 0 : param.fa) {
|
||||
return (global.DSQL_FULL_ACCESS_DB_CONN ||
|
||||
mariadb_1.default.createPool({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_FULL_ACCESS_USERNAME,
|
||||
password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: (0, grabDbSSL_1.default)(),
|
||||
}));
|
||||
}
|
||||
return (global.DSQL_DB_CONN ||
|
||||
mariadb_1.default.createPool({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: (param === null || param === void 0 ? void 0 : param.noDb) ? undefined : process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: (0, grabDbSSL_1.default)(),
|
||||
}));
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
})));
|
||||
});
|
||||
}
|
||||
|
6
dist/package-shared/utils/setup-db.d.ts
vendored
6
dist/package-shared/utils/setup-db.d.ts
vendored
@ -5,7 +5,7 @@ type Params = {
|
||||
ssl?: boolean;
|
||||
connectionLimit?: number;
|
||||
};
|
||||
export default function setupDSQLDb({ useLocal, dbConfig, ssl, connectionLimit, }: Params): {
|
||||
pool: mariadb.Pool;
|
||||
};
|
||||
export default function setupDSQLDb({ useLocal, dbConfig, ssl, connectionLimit, }: Params): Promise<{
|
||||
conn: mariadb.Connection;
|
||||
}>;
|
||||
export {};
|
||||
|
68
dist/package-shared/utils/setup-db.js
vendored
68
dist/package-shared/utils/setup-db.js
vendored
@ -1,4 +1,13 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
@ -6,26 +15,41 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = setupDSQLDb;
|
||||
const grabDbSSL_1 = __importDefault(require("./backend/grabDbSSL"));
|
||||
const mariadb_1 = __importDefault(require("mariadb"));
|
||||
function setupDSQLDb({ useLocal, dbConfig, ssl, connectionLimit = 100, }) {
|
||||
global.DSQL_USE_LOCAL = useLocal || true;
|
||||
const pool = mariadb_1.default.createPool(Object.assign(Object.assign({ host: process.env.DSQL_DB_HOST, user: process.env.DSQL_DB_USERNAME, password: process.env.DSQL_DB_PASSWORD, database: process.env.DSQL_DB_NAME, charset: "utf8mb4" }, dbConfig), { ssl: ssl ? (0, grabDbSSL_1.default)() : undefined, connectionLimit }));
|
||||
global.DSQL_DB_CONN = pool;
|
||||
// let readOnlyConnection;
|
||||
// if (addReadOnlyConn) {
|
||||
// readOnlyConnection = mariadb.createPool({
|
||||
// host: process.env.DSQL_DB_HOST,
|
||||
// user: process.env.DSQL_DB_READ_ONLY_USERNAME,
|
||||
// password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
|
||||
// database: process.env.DSQL_DB_NAME,
|
||||
// charset: "utf8mb4",
|
||||
// ...readOnlyDbConfig,
|
||||
// ssl: ssl ? grabDbSSL() : undefined,
|
||||
// connectionLimit,
|
||||
// });
|
||||
// global.DSQL_READ_ONLY_DB_CONN = readOnlyConnection;
|
||||
// }
|
||||
return {
|
||||
pool,
|
||||
// readOnlyConnection,
|
||||
};
|
||||
function setupDSQLDb(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ useLocal, dbConfig, ssl, connectionLimit = 20, }) {
|
||||
global.DSQL_USE_LOCAL = useLocal || true;
|
||||
const conn = yield mariadb_1.default.createConnection(Object.assign(Object.assign({ host: process.env.DSQL_DB_HOST, user: process.env.DSQL_DB_USERNAME, password: process.env.DSQL_DB_PASSWORD, database: process.env.DSQL_DB_NAME, charset: "utf8mb4" }, dbConfig), { ssl: ssl ? (0, grabDbSSL_1.default)() : undefined, supportBigNumbers: true, bigNumberStrings: false, dateStrings: true }));
|
||||
// const conn = mariadb.createPool({
|
||||
// host: process.env.DSQL_DB_HOST,
|
||||
// user: process.env.DSQL_DB_USERNAME,
|
||||
// password: process.env.DSQL_DB_PASSWORD,
|
||||
// database: process.env.DSQL_DB_NAME,
|
||||
// charset: "utf8mb4",
|
||||
// ...dbConfig,
|
||||
// ssl: ssl ? grabDbSSL() : undefined,
|
||||
// connectionLimit,
|
||||
// supportBigNumbers: true,
|
||||
// bigNumberStrings: false,
|
||||
// dateStrings: true,
|
||||
// });
|
||||
global.DSQL_DB_CONN = conn;
|
||||
// let readOnlyConnection;
|
||||
// if (addReadOnlyConn) {
|
||||
// readOnlyConnection = mariadb.createPool({
|
||||
// host: process.env.DSQL_DB_HOST,
|
||||
// user: process.env.DSQL_DB_READ_ONLY_USERNAME,
|
||||
// password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
|
||||
// database: process.env.DSQL_DB_NAME,
|
||||
// charset: "utf8mb4",
|
||||
// ...readOnlyDbConfig,
|
||||
// ssl: ssl ? grabDbSSL() : undefined,
|
||||
// connectionLimit,
|
||||
// });
|
||||
// global.DSQL_READ_ONLY_DB_CONN = readOnlyConnection;
|
||||
// }
|
||||
return {
|
||||
conn,
|
||||
// readOnlyConnection,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
8
index.ts
8
index.ts
@ -1,9 +1,9 @@
|
||||
import type { Pool } from "mariadb";
|
||||
import type { Connection } from "mariadb";
|
||||
|
||||
declare global {
|
||||
var DSQL_DB_CONN: Pool | undefined;
|
||||
var DSQL_READ_ONLY_DB_CONN: Pool | undefined;
|
||||
var DSQL_FULL_ACCESS_DB_CONN: Pool | undefined;
|
||||
var DSQL_DB_CONN: Connection | undefined;
|
||||
var DSQL_READ_ONLY_DB_CONN: Connection | undefined;
|
||||
var DSQL_FULL_ACCESS_DB_CONN: Connection | undefined;
|
||||
var DSQL_USE_LOCAL: boolean | undefined;
|
||||
var ERROR_CALLBACK: ErrorCallback | undefined;
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ export default async function dbHandler({
|
||||
values,
|
||||
noErrorLogs,
|
||||
}: Param): Promise<any[] | object | null> {
|
||||
const CONNECTION = grabDSQLConnection();
|
||||
const CONNECTION = await grabDSQLConnection();
|
||||
|
||||
let results;
|
||||
|
||||
@ -58,7 +58,11 @@ export default async function dbHandler({
|
||||
}
|
||||
|
||||
if (results) {
|
||||
return JSON.parse(JSON.stringify(results));
|
||||
if (Array.isArray(results)) {
|
||||
return Array.from(results);
|
||||
} else {
|
||||
return results;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
@ -53,7 +53,6 @@ export default async function fullAccessDbHandler({
|
||||
*/
|
||||
return error.message;
|
||||
} finally {
|
||||
DB_CONN?.end();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,56 +0,0 @@
|
||||
import fs from "fs";
|
||||
import serverError from "./serverError";
|
||||
import NO_DB_HANDLER from "../../utils/backend/global-db/NO_DB_HANDLER";
|
||||
|
||||
/**
|
||||
* # No Database DB Handler
|
||||
*/
|
||||
export default async function noDatabaseDbHandler(
|
||||
queryString: string
|
||||
): Promise<any> {
|
||||
process.env.NODE_ENV?.match(/dev/) &&
|
||||
fs.appendFileSync(
|
||||
"./.tmp/sqlQuery.sql",
|
||||
queryString + "\n" + Date() + "\n\n\n",
|
||||
"utf8"
|
||||
);
|
||||
|
||||
/**
|
||||
* Declare variables
|
||||
*
|
||||
* @description Declare "results" variable
|
||||
*/
|
||||
let results;
|
||||
|
||||
/**
|
||||
* Fetch from db
|
||||
*
|
||||
* @description Fetch data from db if no cache
|
||||
*/
|
||||
try {
|
||||
/** ********************* Run Query */
|
||||
results = await NO_DB_HANDLER(queryString);
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
serverError({
|
||||
component: "noDatabaseDbHandler",
|
||||
message: error.message,
|
||||
});
|
||||
|
||||
console.log("ERROR in noDatabaseDbHandler =>", error.message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return results
|
||||
*
|
||||
* @description Return results add to cache if "req" param is passed
|
||||
*/
|
||||
if (results) {
|
||||
return results;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
@ -16,15 +16,30 @@ export default async function suDbHandler({
|
||||
user,
|
||||
values,
|
||||
}: Params) {
|
||||
const connection = mariadb.createPool({
|
||||
const connection = await mariadb.createConnection({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: database,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
});
|
||||
|
||||
// const connection = mariadb.createPool({
|
||||
// host: process.env.DSQL_DB_HOST,
|
||||
// user: process.env.DSQL_DB_USERNAME,
|
||||
// password: process.env.DSQL_DB_PASSWORD,
|
||||
// database: database,
|
||||
// charset: "utf8mb4",
|
||||
// ssl: grabDbSSL(),
|
||||
// supportBigNumbers: true,
|
||||
// bigNumberStrings: false,
|
||||
// dateStrings: true,
|
||||
// });
|
||||
|
||||
const results = await connDbHandler(connection, query);
|
||||
|
||||
return results;
|
||||
|
@ -24,13 +24,16 @@ export default async function userDbHandler({
|
||||
const { fullName, host, username, password } =
|
||||
await grabMariadbMainUserForUser({ user });
|
||||
|
||||
const connection = mariadb.createPool({
|
||||
const connection = await mariadb.createConnection({
|
||||
host,
|
||||
user: username,
|
||||
password: password,
|
||||
database: database,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
});
|
||||
|
||||
const results = await connDbHandler(connection, query);
|
||||
|
@ -22,9 +22,11 @@ export default async function varDatabaseDbHandler({
|
||||
tableSchema,
|
||||
debug,
|
||||
}: Param): Promise<any> {
|
||||
let CONNECTION = grabDSQLConnection({ fa: true });
|
||||
if (global.DSQL_USE_LOCAL) CONNECTION = grabDSQLConnection({ local: true });
|
||||
if (database?.match(/^datasquirel$/)) CONNECTION = grabDSQLConnection();
|
||||
let CONNECTION = await grabDSQLConnection({ fa: true });
|
||||
if (global.DSQL_USE_LOCAL)
|
||||
CONNECTION = await grabDSQLConnection({ local: true });
|
||||
if (database?.match(/^datasquirel$/))
|
||||
CONNECTION = await grabDSQLConnection();
|
||||
|
||||
if (debug) {
|
||||
console.log(`varDatabaseDbHandler:query:`, queryString);
|
||||
|
@ -38,7 +38,6 @@ export default async function varReadOnlyDatabaseDbHandler({
|
||||
|
||||
return error.message;
|
||||
} finally {
|
||||
DB_CONN?.end();
|
||||
}
|
||||
|
||||
if (results) {
|
||||
|
@ -1,117 +0,0 @@
|
||||
import mysql, { Connection } from "mysql";
|
||||
import { exec } from "child_process";
|
||||
import { promisify } from "util";
|
||||
|
||||
// Configuration interface
|
||||
interface DatabaseConfig {
|
||||
host: string;
|
||||
user: string;
|
||||
password: string;
|
||||
database?: string; // Optional for global connection
|
||||
}
|
||||
|
||||
// Master status interface
|
||||
interface MasterStatus {
|
||||
File: string;
|
||||
Position: number;
|
||||
Binlog_Do_DB?: string;
|
||||
Binlog_Ignore_DB?: string;
|
||||
}
|
||||
|
||||
function getConnection(config: DatabaseConfig): Connection {
|
||||
return mysql.createConnection(config);
|
||||
}
|
||||
|
||||
function getMasterStatus(config: DatabaseConfig): Promise<MasterStatus> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const connection = getConnection(config);
|
||||
connection.query("SHOW MASTER STATUS", (error, results) => {
|
||||
connection.end();
|
||||
if (error) reject(error);
|
||||
else resolve(results[0] as MasterStatus);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function syncDatabases() {
|
||||
const config: DatabaseConfig = {
|
||||
host: "localhost",
|
||||
user: "root",
|
||||
password: "your_password",
|
||||
};
|
||||
|
||||
let lastPosition: number | null = null; // Track last synced position
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
// Get current master status
|
||||
const { File, Position } = await getMasterStatus(config);
|
||||
|
||||
// Determine start position (use lastPosition or 4 if first run)
|
||||
const startPosition = lastPosition !== null ? lastPosition + 1 : 4;
|
||||
if (startPosition >= Position) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 5000)); // Wait 5 seconds if no new changes
|
||||
continue;
|
||||
}
|
||||
|
||||
// Execute mysqlbinlog to get changes
|
||||
const execPromise = promisify(exec);
|
||||
const { stdout } = await execPromise(
|
||||
`mysqlbinlog --database=db_master ${File} --start-position=${startPosition} --stop-position=${Position}`
|
||||
);
|
||||
|
||||
if (stdout) {
|
||||
const connection = getConnection({
|
||||
...config,
|
||||
database: "db_slave",
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
connection.query(stdout, (error) => {
|
||||
connection.end();
|
||||
if (error) reject(error);
|
||||
else {
|
||||
lastPosition = Position;
|
||||
console.log(
|
||||
`Synced up to position ${Position} at ${new Date().toISOString()}`
|
||||
);
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Sync error:", error);
|
||||
}
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 5000)); // Check every 5 seconds
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize db_slave with db_master data
|
||||
async function initializeSlave() {
|
||||
const config: DatabaseConfig = {
|
||||
host: "localhost",
|
||||
user: "root",
|
||||
password: "your_password",
|
||||
};
|
||||
|
||||
try {
|
||||
await promisify(exec)(
|
||||
`mysqldump -u ${config.user} -p${config.password} db_master > db_master_backup.sql`
|
||||
);
|
||||
await promisify(exec)(
|
||||
`mysql -u ${config.user} -p${config.password} db_slave < db_master_backup.sql`
|
||||
);
|
||||
console.log("Slave initialized with master data");
|
||||
} catch (error) {
|
||||
console.error("Initialization error:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// Run the sync process
|
||||
async function main() {
|
||||
await initializeSlave();
|
||||
await syncDatabases();
|
||||
}
|
||||
|
||||
main().catch(console.error);
|
@ -13,7 +13,7 @@ import grabDSQLConnection from "../utils/grab-dsql-connection";
|
||||
* @returns {Promise<object|null>}
|
||||
*/
|
||||
(async () => {
|
||||
const CONNECTION = grabDSQLConnection();
|
||||
const CONNECTION = await grabDSQLConnection();
|
||||
|
||||
try {
|
||||
const result = await CONNECTION.query(
|
||||
@ -24,7 +24,7 @@ import grabDSQLConnection from "../utils/grab-dsql-connection";
|
||||
console.log("Connection query ERROR =>", error.message);
|
||||
global.ERROR_CALLBACK?.(`Error Checking DB`, error as Error);
|
||||
} finally {
|
||||
CONNECTION?.end();
|
||||
await CONNECTION?.end();
|
||||
process.exit();
|
||||
}
|
||||
})();
|
||||
|
@ -13,7 +13,7 @@ import grabDSQLConnection from "../utils/grab-dsql-connection";
|
||||
* @returns {Promise<object|null>}
|
||||
*/
|
||||
(async () => {
|
||||
const CONNECTION = grabDSQLConnection({ noDb: true });
|
||||
const CONNECTION = await grabDSQLConnection({ noDb: true });
|
||||
|
||||
/**
|
||||
* Switch Database
|
||||
@ -32,7 +32,7 @@ import grabDSQLConnection from "../utils/grab-dsql-connection";
|
||||
error as Error
|
||||
);
|
||||
} finally {
|
||||
CONNECTION?.end();
|
||||
await CONNECTION?.end();
|
||||
process.exit();
|
||||
}
|
||||
})();
|
||||
|
@ -14,7 +14,7 @@ import grabSQLKeyName from "../utils/grab-sql-key-name";
|
||||
* @returns {Promise<object|null>}
|
||||
*/
|
||||
(async () => {
|
||||
const CONNECTION = grabDSQLConnection();
|
||||
const CONNECTION = await grabDSQLConnection();
|
||||
|
||||
try {
|
||||
const result = await CONNECTION.query(
|
||||
@ -48,7 +48,7 @@ import grabSQLKeyName from "../utils/grab-sql-key-name";
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(`Error Updating SSL Users`, error as Error);
|
||||
} finally {
|
||||
CONNECTION.end();
|
||||
await CONNECTION?.end();
|
||||
process.exit();
|
||||
}
|
||||
})();
|
||||
|
@ -5,7 +5,7 @@ import grabDSQLConnection from "../../grab-dsql-connection";
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
export default async function DB_HANDLER(query: string, values?: any[]) {
|
||||
const CONNECTION = grabDSQLConnection();
|
||||
const CONNECTION = await grabDSQLConnection();
|
||||
|
||||
try {
|
||||
if (!CONNECTION)
|
||||
@ -13,7 +13,11 @@ export default async function DB_HANDLER(query: string, values?: any[]) {
|
||||
|
||||
const results = await CONNECTION.query(query, values);
|
||||
|
||||
return JSON.parse(JSON.stringify(results));
|
||||
if (Array.isArray(results)) {
|
||||
return Array.from(results);
|
||||
} else {
|
||||
return results;
|
||||
}
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(`DB_HANDLER Error`, error as Error);
|
||||
return {
|
||||
|
@ -17,8 +17,8 @@ export default async function DSQL_USER_DB_HANDLER({
|
||||
}: Param) {
|
||||
const CONNECTION =
|
||||
paradigm == "Read Only"
|
||||
? grabDSQLConnection({ ro: true })
|
||||
: grabDSQLConnection({ fa: true });
|
||||
? await grabDSQLConnection({ ro: true })
|
||||
: await grabDSQLConnection({ fa: true });
|
||||
|
||||
try {
|
||||
return await connDbHandler(CONNECTION, queryString, queryValues);
|
||||
@ -26,6 +26,6 @@ export default async function DSQL_USER_DB_HANDLER({
|
||||
global.ERROR_CALLBACK?.(`DSQL_USER_DB_HANDLER Error`, error as Error);
|
||||
return null;
|
||||
} finally {
|
||||
CONNECTION?.end();
|
||||
await CONNECTION?.end();
|
||||
}
|
||||
}
|
||||
|
@ -4,12 +4,16 @@ import grabDSQLConnection from "../../grab-dsql-connection";
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
export default async function LOCAL_DB_HANDLER(query: string, values?: any[]) {
|
||||
const MASTER = grabDSQLConnection();
|
||||
const CONNECTION = await grabDSQLConnection();
|
||||
|
||||
try {
|
||||
const results = await MASTER.query(query, values);
|
||||
const results = await CONNECTION.query(query, values);
|
||||
|
||||
return JSON.parse(JSON.stringify(results));
|
||||
if (Array.isArray(results)) {
|
||||
return Array.from(results);
|
||||
} else {
|
||||
return results;
|
||||
}
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(`LOCAL_DB_HANDLER Error`, error as Error);
|
||||
return {
|
||||
@ -17,6 +21,6 @@ export default async function LOCAL_DB_HANDLER(query: string, values?: any[]) {
|
||||
error: error.message,
|
||||
};
|
||||
} finally {
|
||||
await MASTER?.end();
|
||||
await CONNECTION?.end();
|
||||
}
|
||||
}
|
||||
|
@ -3,22 +3,27 @@ import grabDSQLConnection from "../../grab-dsql-connection";
|
||||
/**
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
export default function NO_DB_HANDLER(query: string, values?: any[]) {
|
||||
const CONNECTION = grabDSQLConnection();
|
||||
export default async function NO_DB_HANDLER(query: string, values?: any[]) {
|
||||
const CONNECTION = await grabDSQLConnection();
|
||||
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
CONNECTION.query(query, values)
|
||||
.then((results) => {
|
||||
CONNECTION.end();
|
||||
resolve(JSON.parse(JSON.stringify(results)));
|
||||
.then(async (results) => {
|
||||
if (Array.isArray(results)) {
|
||||
resolve(Array.from(results));
|
||||
} else {
|
||||
resolve(results);
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
CONNECTION.end();
|
||||
.catch(async (err) => {
|
||||
resolve({
|
||||
error: err.message,
|
||||
sql: err.sql,
|
||||
});
|
||||
})
|
||||
.finally(async () => {
|
||||
await CONNECTION?.end();
|
||||
});
|
||||
});
|
||||
} catch (error: any) {
|
||||
@ -27,7 +32,5 @@ export default function NO_DB_HANDLER(query: string, values?: any[]) {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
} finally {
|
||||
CONNECTION?.end();
|
||||
}
|
||||
}
|
||||
|
@ -1,33 +0,0 @@
|
||||
import grabDSQLConnection from "../../grab-dsql-connection";
|
||||
|
||||
/**
|
||||
* # Root DB handler
|
||||
*/
|
||||
export default function ROOT_DB_HANDLER(query: string, values?: any[]) {
|
||||
const CONNECTION = grabDSQLConnection();
|
||||
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
CONNECTION.query(query, values)
|
||||
.then((results) => {
|
||||
CONNECTION.end();
|
||||
resolve(JSON.parse(JSON.stringify(results)));
|
||||
})
|
||||
.catch((err) => {
|
||||
CONNECTION.end();
|
||||
resolve({
|
||||
error: err.message,
|
||||
sql: err.sql,
|
||||
});
|
||||
});
|
||||
});
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(`ROOT_DB_HANDLER Error`, error as Error);
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
} finally {
|
||||
CONNECTION?.end();
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
import debugLog from "../logging/debug-log";
|
||||
import { DSQLErrorObject } from "../../types";
|
||||
import type { ConnectionConfig, Pool } from "mariadb";
|
||||
import mariadb, { Connection, ConnectionConfig, Pool } from "mariadb";
|
||||
|
||||
export type ConnDBHandlerQueryObject = {
|
||||
query: string;
|
||||
@ -20,9 +20,9 @@ type Return<ReturnType = any> =
|
||||
*/
|
||||
export default async function connDbHandler<ReturnType = any>(
|
||||
/**
|
||||
* MariaDB Connection Pool Object
|
||||
* MariaDB Connection
|
||||
*/
|
||||
connPool?: Pool,
|
||||
conn?: mariadb.Connection,
|
||||
/**
|
||||
* String Or `ConnDBHandlerQueryObject` Array
|
||||
*/
|
||||
@ -34,13 +34,13 @@ export default async function connDbHandler<ReturnType = any>(
|
||||
debug?: boolean
|
||||
): Promise<Return<ReturnType>> {
|
||||
try {
|
||||
if (!connPool) throw new Error("No Connection Found!");
|
||||
if (!conn) throw new Error("No Connection Found!");
|
||||
if (!query) throw new Error("Query String Required!");
|
||||
|
||||
let queryErrorArray: DSQLErrorObject[] = [];
|
||||
|
||||
if (typeof query == "string") {
|
||||
const res = await connPool.query(trimQuery(query), values);
|
||||
const res = await conn.query(trimQuery(query), values);
|
||||
|
||||
if (debug) {
|
||||
debugLog({
|
||||
@ -67,7 +67,7 @@ export default async function connDbHandler<ReturnType = any>(
|
||||
currentQueryError.sql = queryObj.query;
|
||||
currentQueryError.sqlValues = queryObj.values;
|
||||
|
||||
const queryObjRes = await connPool.query(
|
||||
const queryObjRes = await conn.query(
|
||||
trimQuery(queryObj.query),
|
||||
queryObj.values
|
||||
);
|
||||
@ -90,6 +90,9 @@ export default async function connDbHandler<ReturnType = any>(
|
||||
`Connection DB Handler Query Error`,
|
||||
error as Error
|
||||
);
|
||||
|
||||
console.log("query", query);
|
||||
|
||||
resArray.push(null);
|
||||
currentQueryError["error"] = error.message;
|
||||
queryErrorArray.push(currentQueryError);
|
||||
@ -130,7 +133,7 @@ export default async function connDbHandler<ReturnType = any>(
|
||||
// config: conn,
|
||||
};
|
||||
} finally {
|
||||
connPool?.end();
|
||||
await conn?.end();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import mariadb, { Pool } from "mariadb";
|
||||
import mariadb, { Connection, Pool } from "mariadb";
|
||||
import grabDbSSL from "./backend/grabDbSSL";
|
||||
|
||||
type Param = {
|
||||
@ -23,11 +23,13 @@ type Param = {
|
||||
/**
|
||||
* # Grab General CONNECTION for DSQL
|
||||
*/
|
||||
export default function grabDSQLConnection(param?: Param): Pool {
|
||||
export default async function grabDSQLConnection(
|
||||
param?: Param
|
||||
): Promise<Connection> {
|
||||
if (global.DSQL_USE_LOCAL || param?.local) {
|
||||
return (
|
||||
global.DSQL_DB_CONN ||
|
||||
mariadb.createPool({
|
||||
(await mariadb.createConnection({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
@ -37,14 +39,17 @@ export default function grabDSQLConnection(param?: Param): Pool {
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
})
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
if (param?.ro) {
|
||||
return (
|
||||
global.DSQL_READ_ONLY_DB_CONN ||
|
||||
mariadb.createPool({
|
||||
(await mariadb.createConnection({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_READ_ONLY_USERNAME,
|
||||
password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
|
||||
@ -53,14 +58,17 @@ export default function grabDSQLConnection(param?: Param): Pool {
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
})
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
if (param?.fa) {
|
||||
return (
|
||||
global.DSQL_FULL_ACCESS_DB_CONN ||
|
||||
mariadb.createPool({
|
||||
(await mariadb.createConnection({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_FULL_ACCESS_USERNAME,
|
||||
password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD,
|
||||
@ -69,13 +77,16 @@ export default function grabDSQLConnection(param?: Param): Pool {
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
})
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
global.DSQL_DB_CONN ||
|
||||
mariadb.createPool({
|
||||
(await mariadb.createConnection({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
@ -85,6 +96,9 @@ export default function grabDSQLConnection(param?: Param): Pool {
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
})
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
@ -8,15 +8,15 @@ type Params = {
|
||||
connectionLimit?: number;
|
||||
};
|
||||
|
||||
export default function setupDSQLDb({
|
||||
export default async function setupDSQLDb({
|
||||
useLocal,
|
||||
dbConfig,
|
||||
ssl,
|
||||
connectionLimit = 100,
|
||||
connectionLimit = 20,
|
||||
}: Params) {
|
||||
global.DSQL_USE_LOCAL = useLocal || true;
|
||||
|
||||
const pool = mariadb.createPool({
|
||||
const conn = await mariadb.createConnection({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
@ -24,10 +24,26 @@ export default function setupDSQLDb({
|
||||
charset: "utf8mb4",
|
||||
...dbConfig,
|
||||
ssl: ssl ? grabDbSSL() : undefined,
|
||||
connectionLimit,
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
});
|
||||
|
||||
global.DSQL_DB_CONN = pool;
|
||||
// const conn = mariadb.createPool({
|
||||
// host: process.env.DSQL_DB_HOST,
|
||||
// user: process.env.DSQL_DB_USERNAME,
|
||||
// password: process.env.DSQL_DB_PASSWORD,
|
||||
// database: process.env.DSQL_DB_NAME,
|
||||
// charset: "utf8mb4",
|
||||
// ...dbConfig,
|
||||
// ssl: ssl ? grabDbSSL() : undefined,
|
||||
// connectionLimit,
|
||||
// supportBigNumbers: true,
|
||||
// bigNumberStrings: false,
|
||||
// dateStrings: true,
|
||||
// });
|
||||
|
||||
global.DSQL_DB_CONN = conn;
|
||||
|
||||
// let readOnlyConnection;
|
||||
|
||||
@ -47,7 +63,7 @@ export default function setupDSQLDb({
|
||||
// }
|
||||
|
||||
return {
|
||||
pool,
|
||||
conn,
|
||||
// readOnlyConnection,
|
||||
};
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@moduletrace/datasquirel",
|
||||
"version": "4.8.7",
|
||||
"version": "4.8.8",
|
||||
"description": "Cloud-based SQL data management tool",
|
||||
"main": "dist/index.js",
|
||||
"bin": {
|
||||
|
Loading…
Reference in New Issue
Block a user