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 {
|
declare global {
|
||||||
var DSQL_DB_CONN: Pool | undefined;
|
var DSQL_DB_CONN: Connection | undefined;
|
||||||
var DSQL_READ_ONLY_DB_CONN: Pool | undefined;
|
var DSQL_READ_ONLY_DB_CONN: Connection | undefined;
|
||||||
var DSQL_FULL_ACCESS_DB_CONN: Pool | undefined;
|
var DSQL_FULL_ACCESS_DB_CONN: Connection | undefined;
|
||||||
var DSQL_USE_LOCAL: boolean | undefined;
|
var DSQL_USE_LOCAL: boolean | undefined;
|
||||||
var ERROR_CALLBACK: ErrorCallback | undefined;
|
var ERROR_CALLBACK: ErrorCallback | undefined;
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ const grab_dsql_connection_1 = __importDefault(require("../../utils/grab-dsql-co
|
|||||||
function dbHandler(_a) {
|
function dbHandler(_a) {
|
||||||
return __awaiter(this, arguments, void 0, function* ({ query, values, noErrorLogs, }) {
|
return __awaiter(this, arguments, void 0, function* ({ query, values, noErrorLogs, }) {
|
||||||
var _b;
|
var _b;
|
||||||
const CONNECTION = (0, grab_dsql_connection_1.default)();
|
const CONNECTION = yield (0, grab_dsql_connection_1.default)();
|
||||||
let results;
|
let results;
|
||||||
try {
|
try {
|
||||||
if (query && values) {
|
if (query && values) {
|
||||||
@ -54,7 +54,12 @@ function dbHandler(_a) {
|
|||||||
yield (CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end());
|
yield (CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end());
|
||||||
}
|
}
|
||||||
if (results) {
|
if (results) {
|
||||||
return JSON.parse(JSON.stringify(results));
|
if (Array.isArray(results)) {
|
||||||
|
return Array.from(results);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return results;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return null;
|
return null;
|
||||||
|
@ -53,7 +53,6 @@ function fullAccessDbHandler(_a) {
|
|||||||
return error.message;
|
return error.message;
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
DB_CONN === null || DB_CONN === void 0 ? void 0 : DB_CONN.end();
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Return results
|
* 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"));
|
const mariadb_1 = __importDefault(require("mariadb"));
|
||||||
function suDbHandler(_a) {
|
function suDbHandler(_a) {
|
||||||
return __awaiter(this, arguments, void 0, function* ({ query, database, user, values, }) {
|
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,
|
host: process.env.DSQL_DB_HOST,
|
||||||
user: process.env.DSQL_DB_USERNAME,
|
user: process.env.DSQL_DB_USERNAME,
|
||||||
password: process.env.DSQL_DB_PASSWORD,
|
password: process.env.DSQL_DB_PASSWORD,
|
||||||
database: database,
|
database: database,
|
||||||
charset: "utf8mb4",
|
charset: "utf8mb4",
|
||||||
ssl: (0, grabDbSSL_1.default)(),
|
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);
|
const results = yield (0, conn_db_handler_1.default)(connection, query);
|
||||||
return results;
|
return results;
|
||||||
});
|
});
|
||||||
|
@ -20,13 +20,16 @@ const grabDbSSL_1 = __importDefault(require("../../utils/backend/grabDbSSL"));
|
|||||||
function userDbHandler(_a) {
|
function userDbHandler(_a) {
|
||||||
return __awaiter(this, arguments, void 0, function* ({ query, user, database, debug, tableSchema, values, }) {
|
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 { 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,
|
host,
|
||||||
user: username,
|
user: username,
|
||||||
password: password,
|
password: password,
|
||||||
database: database,
|
database: database,
|
||||||
charset: "utf8mb4",
|
charset: "utf8mb4",
|
||||||
ssl: (0, grabDbSSL_1.default)(),
|
ssl: (0, grabDbSSL_1.default)(),
|
||||||
|
supportBigNumbers: true,
|
||||||
|
bigNumberStrings: false,
|
||||||
|
dateStrings: true,
|
||||||
});
|
});
|
||||||
const results = yield (0, conn_db_handler_1.default)(connection, query);
|
const results = yield (0, conn_db_handler_1.default)(connection, query);
|
||||||
return results;
|
return results;
|
||||||
|
@ -23,11 +23,11 @@ const conn_db_handler_1 = __importDefault(require("../../utils/db/conn-db-handle
|
|||||||
function varDatabaseDbHandler(_a) {
|
function varDatabaseDbHandler(_a) {
|
||||||
return __awaiter(this, arguments, void 0, function* ({ queryString, queryValuesArray, database, tableSchema, debug, }) {
|
return __awaiter(this, arguments, void 0, function* ({ queryString, queryValuesArray, database, tableSchema, debug, }) {
|
||||||
var _b;
|
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)
|
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$/))
|
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) {
|
if (debug) {
|
||||||
console.log(`varDatabaseDbHandler:query:`, queryString);
|
console.log(`varDatabaseDbHandler:query:`, queryString);
|
||||||
console.log(`varDatabaseDbHandler:values:`, queryValuesArray);
|
console.log(`varDatabaseDbHandler:values:`, queryValuesArray);
|
||||||
|
@ -40,7 +40,6 @@ function varReadOnlyDatabaseDbHandler(_a) {
|
|||||||
return error.message;
|
return error.message;
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
DB_CONN === null || DB_CONN === void 0 ? void 0 : DB_CONN.end();
|
|
||||||
}
|
}
|
||||||
if (results) {
|
if (results) {
|
||||||
const unparsedResults = 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* () {
|
(() => __awaiter(void 0, void 0, void 0, function* () {
|
||||||
var _a;
|
var _a;
|
||||||
const CONNECTION = (0, grab_dsql_connection_1.default)();
|
const CONNECTION = yield (0, grab_dsql_connection_1.default)();
|
||||||
try {
|
try {
|
||||||
const result = yield CONNECTION.query("SELECT id,first_name,last_name FROM users LIMIT 3");
|
const result = yield CONNECTION.query("SELECT id,first_name,last_name FROM users LIMIT 3");
|
||||||
console.log("Connection Query Success =>", result);
|
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);
|
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `Error Checking DB`, error);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end();
|
yield (CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end());
|
||||||
process.exit();
|
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* () {
|
(() => __awaiter(void 0, void 0, void 0, function* () {
|
||||||
var _a;
|
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
|
* 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);
|
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `Error Testing External Server`, error);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end();
|
yield (CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end());
|
||||||
process.exit();
|
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* () {
|
(() => __awaiter(void 0, void 0, void 0, function* () {
|
||||||
var _a, _b;
|
var _a, _b;
|
||||||
const CONNECTION = (0, grab_dsql_connection_1.default)();
|
const CONNECTION = yield (0, grab_dsql_connection_1.default)();
|
||||||
try {
|
try {
|
||||||
const result = yield CONNECTION.query("SELECT user,host,ssl_type FROM mysql.user");
|
const result = yield CONNECTION.query("SELECT user,host,ssl_type FROM mysql.user");
|
||||||
const parsedResults = JSON.parse(JSON.stringify(result));
|
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);
|
(_b = global.ERROR_CALLBACK) === null || _b === void 0 ? void 0 : _b.call(global, `Error Updating SSL Users`, error);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
CONNECTION.end();
|
yield (CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end());
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
}))();
|
}))();
|
||||||
|
@ -21,12 +21,17 @@ const grab_dsql_connection_1 = __importDefault(require("../../grab-dsql-connecti
|
|||||||
function DB_HANDLER(query, values) {
|
function DB_HANDLER(query, values) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
var _a;
|
var _a;
|
||||||
const CONNECTION = (0, grab_dsql_connection_1.default)();
|
const CONNECTION = yield (0, grab_dsql_connection_1.default)();
|
||||||
try {
|
try {
|
||||||
if (!CONNECTION)
|
if (!CONNECTION)
|
||||||
throw new Error("No Connection provided to DB_HANDLER function!");
|
throw new Error("No Connection provided to DB_HANDLER function!");
|
||||||
const results = yield CONNECTION.query(query, values);
|
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) {
|
catch (error) {
|
||||||
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `DB_HANDLER Error`, 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, }) {
|
return __awaiter(this, arguments, void 0, function* ({ paradigm, queryString, queryValues, }) {
|
||||||
var _b;
|
var _b;
|
||||||
const CONNECTION = paradigm == "Read Only"
|
const CONNECTION = paradigm == "Read Only"
|
||||||
? (0, grab_dsql_connection_1.default)({ ro: true })
|
? yield (0, grab_dsql_connection_1.default)({ ro: true })
|
||||||
: (0, grab_dsql_connection_1.default)({ fa: true });
|
: yield (0, grab_dsql_connection_1.default)({ fa: true });
|
||||||
try {
|
try {
|
||||||
return yield (0, conn_db_handler_1.default)(CONNECTION, queryString, queryValues);
|
return yield (0, conn_db_handler_1.default)(CONNECTION, queryString, queryValues);
|
||||||
}
|
}
|
||||||
@ -32,7 +32,7 @@ function DSQL_USER_DB_HANDLER(_a) {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
finally {
|
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) {
|
function LOCAL_DB_HANDLER(query, values) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
var _a;
|
var _a;
|
||||||
const MASTER = (0, grab_dsql_connection_1.default)();
|
const CONNECTION = yield (0, grab_dsql_connection_1.default)();
|
||||||
try {
|
try {
|
||||||
const results = yield MASTER.query(query, values);
|
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) {
|
catch (error) {
|
||||||
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `LOCAL_DB_HANDLER Error`, 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 {
|
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
|
* # DSQL user read-only DB handler
|
||||||
*/
|
*/
|
||||||
export default function NO_DB_HANDLER(query: string, values?: any[]): Promise<unknown> | {
|
export default function NO_DB_HANDLER(query: string, values?: any[]): Promise<unknown>;
|
||||||
success: boolean;
|
|
||||||
error: any;
|
|
||||||
};
|
|
||||||
|
@ -1,4 +1,13 @@
|
|||||||
"use strict";
|
"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) {
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||||
};
|
};
|
||||||
@ -9,22 +18,29 @@ const grab_dsql_connection_1 = __importDefault(require("../../grab-dsql-connecti
|
|||||||
* # DSQL user read-only DB handler
|
* # DSQL user read-only DB handler
|
||||||
*/
|
*/
|
||||||
function NO_DB_HANDLER(query, values) {
|
function NO_DB_HANDLER(query, values) {
|
||||||
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
var _a;
|
var _a;
|
||||||
const CONNECTION = (0, grab_dsql_connection_1.default)();
|
const CONNECTION = yield (0, grab_dsql_connection_1.default)();
|
||||||
try {
|
try {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
CONNECTION.query(query, values)
|
CONNECTION.query(query, values)
|
||||||
.then((results) => {
|
.then((results) => __awaiter(this, void 0, void 0, function* () {
|
||||||
CONNECTION.end();
|
if (Array.isArray(results)) {
|
||||||
resolve(JSON.parse(JSON.stringify(results)));
|
resolve(Array.from(results));
|
||||||
})
|
}
|
||||||
.catch((err) => {
|
else {
|
||||||
CONNECTION.end();
|
resolve(results);
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
.catch((err) => __awaiter(this, void 0, void 0, function* () {
|
||||||
resolve({
|
resolve({
|
||||||
error: err.message,
|
error: err.message,
|
||||||
sql: err.sql,
|
sql: err.sql,
|
||||||
});
|
});
|
||||||
});
|
}))
|
||||||
|
.finally(() => __awaiter(this, void 0, void 0, function* () {
|
||||||
|
yield (CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end());
|
||||||
|
}));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
@ -34,7 +50,5 @@ function NO_DB_HANDLER(query, values) {
|
|||||||
error: error.message,
|
error: error.message,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
finally {
|
});
|
||||||
CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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 { DSQLErrorObject } from "../../types";
|
||||||
import type { ConnectionConfig, Pool } from "mariadb";
|
import mariadb, { ConnectionConfig } from "mariadb";
|
||||||
export type ConnDBHandlerQueryObject = {
|
export type ConnDBHandlerQueryObject = {
|
||||||
query: string;
|
query: string;
|
||||||
values?: (string | number | undefined)[];
|
values?: (string | number | undefined)[];
|
||||||
@ -16,9 +16,9 @@ type Return<ReturnType = any> = ReturnType | ReturnType[] | null | {
|
|||||||
*/
|
*/
|
||||||
export default function connDbHandler<ReturnType = any>(
|
export default function connDbHandler<ReturnType = any>(
|
||||||
/**
|
/**
|
||||||
* MariaDB Connection Pool Object
|
* MariaDB Connection
|
||||||
*/
|
*/
|
||||||
connPool?: Pool,
|
conn?: mariadb.Connection,
|
||||||
/**
|
/**
|
||||||
* String Or `ConnDBHandlerQueryObject` Array
|
* 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(
|
function connDbHandler(
|
||||||
/**
|
/**
|
||||||
* MariaDB Connection Pool Object
|
* MariaDB Connection
|
||||||
*/
|
*/
|
||||||
connPool,
|
conn,
|
||||||
/**
|
/**
|
||||||
* String Or `ConnDBHandlerQueryObject` Array
|
* String Or `ConnDBHandlerQueryObject` Array
|
||||||
*/
|
*/
|
||||||
@ -35,13 +35,13 @@ values, debug) {
|
|||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
var _a, _b;
|
var _a, _b;
|
||||||
try {
|
try {
|
||||||
if (!connPool)
|
if (!conn)
|
||||||
throw new Error("No Connection Found!");
|
throw new Error("No Connection Found!");
|
||||||
if (!query)
|
if (!query)
|
||||||
throw new Error("Query String Required!");
|
throw new Error("Query String Required!");
|
||||||
let queryErrorArray = [];
|
let queryErrorArray = [];
|
||||||
if (typeof query == "string") {
|
if (typeof query == "string") {
|
||||||
const res = yield connPool.query(trimQuery(query), values);
|
const res = yield conn.query(trimQuery(query), values);
|
||||||
if (debug) {
|
if (debug) {
|
||||||
(0, debug_log_1.default)({
|
(0, debug_log_1.default)({
|
||||||
log: res,
|
log: res,
|
||||||
@ -62,7 +62,7 @@ values, debug) {
|
|||||||
const queryObj = query[i];
|
const queryObj = query[i];
|
||||||
currentQueryError.sql = queryObj.query;
|
currentQueryError.sql = queryObj.query;
|
||||||
currentQueryError.sqlValues = queryObj.values;
|
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) {
|
if (debug) {
|
||||||
(0, debug_log_1.default)({
|
(0, debug_log_1.default)({
|
||||||
log: queryObjRes,
|
log: queryObjRes,
|
||||||
@ -79,6 +79,7 @@ values, debug) {
|
|||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `Connection DB Handler Query Error`, 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);
|
resArray.push(null);
|
||||||
currentQueryError["error"] = error.message;
|
currentQueryError["error"] = error.message;
|
||||||
queryErrorArray.push(currentQueryError);
|
queryErrorArray.push(currentQueryError);
|
||||||
@ -117,7 +118,7 @@ values, debug) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
finally {
|
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 = {
|
type Param = {
|
||||||
/**
|
/**
|
||||||
* Read Only?
|
* Read Only?
|
||||||
@ -20,5 +20,5 @@ type Param = {
|
|||||||
/**
|
/**
|
||||||
* # Grab General CONNECTION for DSQL
|
* # Grab General CONNECTION for DSQL
|
||||||
*/
|
*/
|
||||||
export default function grabDSQLConnection(param?: Param): Pool;
|
export default function grabDSQLConnection(param?: Param): Promise<Connection>;
|
||||||
export {};
|
export {};
|
||||||
|
@ -1,4 +1,13 @@
|
|||||||
"use strict";
|
"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) {
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||||
};
|
};
|
||||||
@ -10,9 +19,10 @@ const grabDbSSL_1 = __importDefault(require("./backend/grabDbSSL"));
|
|||||||
* # Grab General CONNECTION for DSQL
|
* # Grab General CONNECTION for DSQL
|
||||||
*/
|
*/
|
||||||
function grabDSQLConnection(param) {
|
function grabDSQLConnection(param) {
|
||||||
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
if (global.DSQL_USE_LOCAL || (param === null || param === void 0 ? void 0 : param.local)) {
|
if (global.DSQL_USE_LOCAL || (param === null || param === void 0 ? void 0 : param.local)) {
|
||||||
return (global.DSQL_DB_CONN ||
|
return (global.DSQL_DB_CONN ||
|
||||||
mariadb_1.default.createPool({
|
(yield mariadb_1.default.createConnection({
|
||||||
host: process.env.DSQL_DB_HOST,
|
host: process.env.DSQL_DB_HOST,
|
||||||
user: process.env.DSQL_DB_USERNAME,
|
user: process.env.DSQL_DB_USERNAME,
|
||||||
password: process.env.DSQL_DB_PASSWORD,
|
password: process.env.DSQL_DB_PASSWORD,
|
||||||
@ -22,11 +32,14 @@ function grabDSQLConnection(param) {
|
|||||||
: undefined,
|
: undefined,
|
||||||
charset: "utf8mb4",
|
charset: "utf8mb4",
|
||||||
ssl: (0, grabDbSSL_1.default)(),
|
ssl: (0, grabDbSSL_1.default)(),
|
||||||
}));
|
supportBigNumbers: true,
|
||||||
|
bigNumberStrings: false,
|
||||||
|
dateStrings: true,
|
||||||
|
})));
|
||||||
}
|
}
|
||||||
if (param === null || param === void 0 ? void 0 : param.ro) {
|
if (param === null || param === void 0 ? void 0 : param.ro) {
|
||||||
return (global.DSQL_READ_ONLY_DB_CONN ||
|
return (global.DSQL_READ_ONLY_DB_CONN ||
|
||||||
mariadb_1.default.createPool({
|
(yield mariadb_1.default.createConnection({
|
||||||
host: process.env.DSQL_DB_HOST,
|
host: process.env.DSQL_DB_HOST,
|
||||||
user: process.env.DSQL_DB_READ_ONLY_USERNAME,
|
user: process.env.DSQL_DB_READ_ONLY_USERNAME,
|
||||||
password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
|
password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
|
||||||
@ -35,11 +48,14 @@ function grabDSQLConnection(param) {
|
|||||||
: undefined,
|
: undefined,
|
||||||
charset: "utf8mb4",
|
charset: "utf8mb4",
|
||||||
ssl: (0, grabDbSSL_1.default)(),
|
ssl: (0, grabDbSSL_1.default)(),
|
||||||
}));
|
supportBigNumbers: true,
|
||||||
|
bigNumberStrings: false,
|
||||||
|
dateStrings: true,
|
||||||
|
})));
|
||||||
}
|
}
|
||||||
if (param === null || param === void 0 ? void 0 : param.fa) {
|
if (param === null || param === void 0 ? void 0 : param.fa) {
|
||||||
return (global.DSQL_FULL_ACCESS_DB_CONN ||
|
return (global.DSQL_FULL_ACCESS_DB_CONN ||
|
||||||
mariadb_1.default.createPool({
|
(yield mariadb_1.default.createConnection({
|
||||||
host: process.env.DSQL_DB_HOST,
|
host: process.env.DSQL_DB_HOST,
|
||||||
user: process.env.DSQL_DB_FULL_ACCESS_USERNAME,
|
user: process.env.DSQL_DB_FULL_ACCESS_USERNAME,
|
||||||
password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD,
|
password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD,
|
||||||
@ -48,10 +64,13 @@ function grabDSQLConnection(param) {
|
|||||||
: undefined,
|
: undefined,
|
||||||
charset: "utf8mb4",
|
charset: "utf8mb4",
|
||||||
ssl: (0, grabDbSSL_1.default)(),
|
ssl: (0, grabDbSSL_1.default)(),
|
||||||
}));
|
supportBigNumbers: true,
|
||||||
|
bigNumberStrings: false,
|
||||||
|
dateStrings: true,
|
||||||
|
})));
|
||||||
}
|
}
|
||||||
return (global.DSQL_DB_CONN ||
|
return (global.DSQL_DB_CONN ||
|
||||||
mariadb_1.default.createPool({
|
(yield mariadb_1.default.createConnection({
|
||||||
host: process.env.DSQL_DB_HOST,
|
host: process.env.DSQL_DB_HOST,
|
||||||
user: process.env.DSQL_DB_USERNAME,
|
user: process.env.DSQL_DB_USERNAME,
|
||||||
password: process.env.DSQL_DB_PASSWORD,
|
password: process.env.DSQL_DB_PASSWORD,
|
||||||
@ -61,5 +80,9 @@ function grabDSQLConnection(param) {
|
|||||||
: undefined,
|
: undefined,
|
||||||
charset: "utf8mb4",
|
charset: "utf8mb4",
|
||||||
ssl: (0, grabDbSSL_1.default)(),
|
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;
|
ssl?: boolean;
|
||||||
connectionLimit?: number;
|
connectionLimit?: number;
|
||||||
};
|
};
|
||||||
export default function setupDSQLDb({ useLocal, dbConfig, ssl, connectionLimit, }: Params): {
|
export default function setupDSQLDb({ useLocal, dbConfig, ssl, connectionLimit, }: Params): Promise<{
|
||||||
pool: mariadb.Pool;
|
conn: mariadb.Connection;
|
||||||
};
|
}>;
|
||||||
export {};
|
export {};
|
||||||
|
32
dist/package-shared/utils/setup-db.js
vendored
32
dist/package-shared/utils/setup-db.js
vendored
@ -1,4 +1,13 @@
|
|||||||
"use strict";
|
"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) {
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||||
};
|
};
|
||||||
@ -6,10 +15,24 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|||||||
exports.default = setupDSQLDb;
|
exports.default = setupDSQLDb;
|
||||||
const grabDbSSL_1 = __importDefault(require("./backend/grabDbSSL"));
|
const grabDbSSL_1 = __importDefault(require("./backend/grabDbSSL"));
|
||||||
const mariadb_1 = __importDefault(require("mariadb"));
|
const mariadb_1 = __importDefault(require("mariadb"));
|
||||||
function setupDSQLDb({ useLocal, dbConfig, ssl, connectionLimit = 100, }) {
|
function setupDSQLDb(_a) {
|
||||||
|
return __awaiter(this, arguments, void 0, function* ({ useLocal, dbConfig, ssl, connectionLimit = 20, }) {
|
||||||
global.DSQL_USE_LOCAL = useLocal || true;
|
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 }));
|
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 }));
|
||||||
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;
|
// let readOnlyConnection;
|
||||||
// if (addReadOnlyConn) {
|
// if (addReadOnlyConn) {
|
||||||
// readOnlyConnection = mariadb.createPool({
|
// readOnlyConnection = mariadb.createPool({
|
||||||
@ -25,7 +48,8 @@ function setupDSQLDb({ useLocal, dbConfig, ssl, connectionLimit = 100, }) {
|
|||||||
// global.DSQL_READ_ONLY_DB_CONN = readOnlyConnection;
|
// global.DSQL_READ_ONLY_DB_CONN = readOnlyConnection;
|
||||||
// }
|
// }
|
||||||
return {
|
return {
|
||||||
pool,
|
conn,
|
||||||
// readOnlyConnection,
|
// readOnlyConnection,
|
||||||
};
|
};
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
8
index.ts
8
index.ts
@ -1,9 +1,9 @@
|
|||||||
import type { Pool } from "mariadb";
|
import type { Connection } from "mariadb";
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
var DSQL_DB_CONN: Pool | undefined;
|
var DSQL_DB_CONN: Connection | undefined;
|
||||||
var DSQL_READ_ONLY_DB_CONN: Pool | undefined;
|
var DSQL_READ_ONLY_DB_CONN: Connection | undefined;
|
||||||
var DSQL_FULL_ACCESS_DB_CONN: Pool | undefined;
|
var DSQL_FULL_ACCESS_DB_CONN: Connection | undefined;
|
||||||
var DSQL_USE_LOCAL: boolean | undefined;
|
var DSQL_USE_LOCAL: boolean | undefined;
|
||||||
var ERROR_CALLBACK: ErrorCallback | undefined;
|
var ERROR_CALLBACK: ErrorCallback | undefined;
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ export default async function dbHandler({
|
|||||||
values,
|
values,
|
||||||
noErrorLogs,
|
noErrorLogs,
|
||||||
}: Param): Promise<any[] | object | null> {
|
}: Param): Promise<any[] | object | null> {
|
||||||
const CONNECTION = grabDSQLConnection();
|
const CONNECTION = await grabDSQLConnection();
|
||||||
|
|
||||||
let results;
|
let results;
|
||||||
|
|
||||||
@ -58,7 +58,11 @@ export default async function dbHandler({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (results) {
|
if (results) {
|
||||||
return JSON.parse(JSON.stringify(results));
|
if (Array.isArray(results)) {
|
||||||
|
return Array.from(results);
|
||||||
|
} else {
|
||||||
|
return results;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,6 @@ export default async function fullAccessDbHandler({
|
|||||||
*/
|
*/
|
||||||
return error.message;
|
return error.message;
|
||||||
} finally {
|
} 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,
|
user,
|
||||||
values,
|
values,
|
||||||
}: Params) {
|
}: Params) {
|
||||||
const connection = mariadb.createPool({
|
const connection = await mariadb.createConnection({
|
||||||
host: process.env.DSQL_DB_HOST,
|
host: process.env.DSQL_DB_HOST,
|
||||||
user: process.env.DSQL_DB_USERNAME,
|
user: process.env.DSQL_DB_USERNAME,
|
||||||
password: process.env.DSQL_DB_PASSWORD,
|
password: process.env.DSQL_DB_PASSWORD,
|
||||||
database: database,
|
database: database,
|
||||||
charset: "utf8mb4",
|
charset: "utf8mb4",
|
||||||
ssl: grabDbSSL(),
|
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);
|
const results = await connDbHandler(connection, query);
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
|
@ -24,13 +24,16 @@ export default async function userDbHandler({
|
|||||||
const { fullName, host, username, password } =
|
const { fullName, host, username, password } =
|
||||||
await grabMariadbMainUserForUser({ user });
|
await grabMariadbMainUserForUser({ user });
|
||||||
|
|
||||||
const connection = mariadb.createPool({
|
const connection = await mariadb.createConnection({
|
||||||
host,
|
host,
|
||||||
user: username,
|
user: username,
|
||||||
password: password,
|
password: password,
|
||||||
database: database,
|
database: database,
|
||||||
charset: "utf8mb4",
|
charset: "utf8mb4",
|
||||||
ssl: grabDbSSL(),
|
ssl: grabDbSSL(),
|
||||||
|
supportBigNumbers: true,
|
||||||
|
bigNumberStrings: false,
|
||||||
|
dateStrings: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const results = await connDbHandler(connection, query);
|
const results = await connDbHandler(connection, query);
|
||||||
|
@ -22,9 +22,11 @@ export default async function varDatabaseDbHandler({
|
|||||||
tableSchema,
|
tableSchema,
|
||||||
debug,
|
debug,
|
||||||
}: Param): Promise<any> {
|
}: Param): Promise<any> {
|
||||||
let CONNECTION = grabDSQLConnection({ fa: true });
|
let CONNECTION = await grabDSQLConnection({ fa: true });
|
||||||
if (global.DSQL_USE_LOCAL) CONNECTION = grabDSQLConnection({ local: true });
|
if (global.DSQL_USE_LOCAL)
|
||||||
if (database?.match(/^datasquirel$/)) CONNECTION = grabDSQLConnection();
|
CONNECTION = await grabDSQLConnection({ local: true });
|
||||||
|
if (database?.match(/^datasquirel$/))
|
||||||
|
CONNECTION = await grabDSQLConnection();
|
||||||
|
|
||||||
if (debug) {
|
if (debug) {
|
||||||
console.log(`varDatabaseDbHandler:query:`, queryString);
|
console.log(`varDatabaseDbHandler:query:`, queryString);
|
||||||
|
@ -38,7 +38,6 @@ export default async function varReadOnlyDatabaseDbHandler({
|
|||||||
|
|
||||||
return error.message;
|
return error.message;
|
||||||
} finally {
|
} finally {
|
||||||
DB_CONN?.end();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (results) {
|
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>}
|
* @returns {Promise<object|null>}
|
||||||
*/
|
*/
|
||||||
(async () => {
|
(async () => {
|
||||||
const CONNECTION = grabDSQLConnection();
|
const CONNECTION = await grabDSQLConnection();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await CONNECTION.query(
|
const result = await CONNECTION.query(
|
||||||
@ -24,7 +24,7 @@ import grabDSQLConnection from "../utils/grab-dsql-connection";
|
|||||||
console.log("Connection query ERROR =>", error.message);
|
console.log("Connection query ERROR =>", error.message);
|
||||||
global.ERROR_CALLBACK?.(`Error Checking DB`, error as Error);
|
global.ERROR_CALLBACK?.(`Error Checking DB`, error as Error);
|
||||||
} finally {
|
} finally {
|
||||||
CONNECTION?.end();
|
await CONNECTION?.end();
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
@ -13,7 +13,7 @@ import grabDSQLConnection from "../utils/grab-dsql-connection";
|
|||||||
* @returns {Promise<object|null>}
|
* @returns {Promise<object|null>}
|
||||||
*/
|
*/
|
||||||
(async () => {
|
(async () => {
|
||||||
const CONNECTION = grabDSQLConnection({ noDb: true });
|
const CONNECTION = await grabDSQLConnection({ noDb: true });
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Switch Database
|
* Switch Database
|
||||||
@ -32,7 +32,7 @@ import grabDSQLConnection from "../utils/grab-dsql-connection";
|
|||||||
error as Error
|
error as Error
|
||||||
);
|
);
|
||||||
} finally {
|
} finally {
|
||||||
CONNECTION?.end();
|
await CONNECTION?.end();
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
@ -14,7 +14,7 @@ import grabSQLKeyName from "../utils/grab-sql-key-name";
|
|||||||
* @returns {Promise<object|null>}
|
* @returns {Promise<object|null>}
|
||||||
*/
|
*/
|
||||||
(async () => {
|
(async () => {
|
||||||
const CONNECTION = grabDSQLConnection();
|
const CONNECTION = await grabDSQLConnection();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await CONNECTION.query(
|
const result = await CONNECTION.query(
|
||||||
@ -48,7 +48,7 @@ import grabSQLKeyName from "../utils/grab-sql-key-name";
|
|||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
global.ERROR_CALLBACK?.(`Error Updating SSL Users`, error as Error);
|
global.ERROR_CALLBACK?.(`Error Updating SSL Users`, error as Error);
|
||||||
} finally {
|
} finally {
|
||||||
CONNECTION.end();
|
await CONNECTION?.end();
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
@ -5,7 +5,7 @@ import grabDSQLConnection from "../../grab-dsql-connection";
|
|||||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||||
*/
|
*/
|
||||||
export default async function DB_HANDLER(query: string, values?: any[]) {
|
export default async function DB_HANDLER(query: string, values?: any[]) {
|
||||||
const CONNECTION = grabDSQLConnection();
|
const CONNECTION = await grabDSQLConnection();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!CONNECTION)
|
if (!CONNECTION)
|
||||||
@ -13,7 +13,11 @@ export default async function DB_HANDLER(query: string, values?: any[]) {
|
|||||||
|
|
||||||
const results = await CONNECTION.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) {
|
} catch (error: any) {
|
||||||
global.ERROR_CALLBACK?.(`DB_HANDLER Error`, error as Error);
|
global.ERROR_CALLBACK?.(`DB_HANDLER Error`, error as Error);
|
||||||
return {
|
return {
|
||||||
|
@ -17,8 +17,8 @@ export default async function DSQL_USER_DB_HANDLER({
|
|||||||
}: Param) {
|
}: Param) {
|
||||||
const CONNECTION =
|
const CONNECTION =
|
||||||
paradigm == "Read Only"
|
paradigm == "Read Only"
|
||||||
? grabDSQLConnection({ ro: true })
|
? await grabDSQLConnection({ ro: true })
|
||||||
: grabDSQLConnection({ fa: true });
|
: await grabDSQLConnection({ fa: true });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return await connDbHandler(CONNECTION, queryString, queryValues);
|
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);
|
global.ERROR_CALLBACK?.(`DSQL_USER_DB_HANDLER Error`, error as Error);
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
CONNECTION?.end();
|
await CONNECTION?.end();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,12 +4,16 @@ import grabDSQLConnection from "../../grab-dsql-connection";
|
|||||||
* # DSQL user read-only DB handler
|
* # DSQL user read-only DB handler
|
||||||
*/
|
*/
|
||||||
export default async function LOCAL_DB_HANDLER(query: string, values?: any[]) {
|
export default async function LOCAL_DB_HANDLER(query: string, values?: any[]) {
|
||||||
const MASTER = grabDSQLConnection();
|
const CONNECTION = await grabDSQLConnection();
|
||||||
|
|
||||||
try {
|
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) {
|
} catch (error: any) {
|
||||||
global.ERROR_CALLBACK?.(`LOCAL_DB_HANDLER Error`, error as Error);
|
global.ERROR_CALLBACK?.(`LOCAL_DB_HANDLER Error`, error as Error);
|
||||||
return {
|
return {
|
||||||
@ -17,6 +21,6 @@ export default async function LOCAL_DB_HANDLER(query: string, values?: any[]) {
|
|||||||
error: error.message,
|
error: error.message,
|
||||||
};
|
};
|
||||||
} finally {
|
} finally {
|
||||||
await MASTER?.end();
|
await CONNECTION?.end();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,22 +3,27 @@ import grabDSQLConnection from "../../grab-dsql-connection";
|
|||||||
/**
|
/**
|
||||||
* # DSQL user read-only DB handler
|
* # DSQL user read-only DB handler
|
||||||
*/
|
*/
|
||||||
export default function NO_DB_HANDLER(query: string, values?: any[]) {
|
export default async function NO_DB_HANDLER(query: string, values?: any[]) {
|
||||||
const CONNECTION = grabDSQLConnection();
|
const CONNECTION = await grabDSQLConnection();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
CONNECTION.query(query, values)
|
CONNECTION.query(query, values)
|
||||||
.then((results) => {
|
.then(async (results) => {
|
||||||
CONNECTION.end();
|
if (Array.isArray(results)) {
|
||||||
resolve(JSON.parse(JSON.stringify(results)));
|
resolve(Array.from(results));
|
||||||
|
} else {
|
||||||
|
resolve(results);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch(async (err) => {
|
||||||
CONNECTION.end();
|
|
||||||
resolve({
|
resolve({
|
||||||
error: err.message,
|
error: err.message,
|
||||||
sql: err.sql,
|
sql: err.sql,
|
||||||
});
|
});
|
||||||
|
})
|
||||||
|
.finally(async () => {
|
||||||
|
await CONNECTION?.end();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
@ -27,7 +32,5 @@ export default function NO_DB_HANDLER(query: string, values?: any[]) {
|
|||||||
success: false,
|
success: false,
|
||||||
error: error.message,
|
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 debugLog from "../logging/debug-log";
|
||||||
import { DSQLErrorObject } from "../../types";
|
import { DSQLErrorObject } from "../../types";
|
||||||
import type { ConnectionConfig, Pool } from "mariadb";
|
import mariadb, { Connection, ConnectionConfig, Pool } from "mariadb";
|
||||||
|
|
||||||
export type ConnDBHandlerQueryObject = {
|
export type ConnDBHandlerQueryObject = {
|
||||||
query: string;
|
query: string;
|
||||||
@ -20,9 +20,9 @@ type Return<ReturnType = any> =
|
|||||||
*/
|
*/
|
||||||
export default async function connDbHandler<ReturnType = any>(
|
export default async function connDbHandler<ReturnType = any>(
|
||||||
/**
|
/**
|
||||||
* MariaDB Connection Pool Object
|
* MariaDB Connection
|
||||||
*/
|
*/
|
||||||
connPool?: Pool,
|
conn?: mariadb.Connection,
|
||||||
/**
|
/**
|
||||||
* String Or `ConnDBHandlerQueryObject` Array
|
* String Or `ConnDBHandlerQueryObject` Array
|
||||||
*/
|
*/
|
||||||
@ -34,13 +34,13 @@ export default async function connDbHandler<ReturnType = any>(
|
|||||||
debug?: boolean
|
debug?: boolean
|
||||||
): Promise<Return<ReturnType>> {
|
): Promise<Return<ReturnType>> {
|
||||||
try {
|
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!");
|
if (!query) throw new Error("Query String Required!");
|
||||||
|
|
||||||
let queryErrorArray: DSQLErrorObject[] = [];
|
let queryErrorArray: DSQLErrorObject[] = [];
|
||||||
|
|
||||||
if (typeof query == "string") {
|
if (typeof query == "string") {
|
||||||
const res = await connPool.query(trimQuery(query), values);
|
const res = await conn.query(trimQuery(query), values);
|
||||||
|
|
||||||
if (debug) {
|
if (debug) {
|
||||||
debugLog({
|
debugLog({
|
||||||
@ -67,7 +67,7 @@ export default async function connDbHandler<ReturnType = any>(
|
|||||||
currentQueryError.sql = queryObj.query;
|
currentQueryError.sql = queryObj.query;
|
||||||
currentQueryError.sqlValues = queryObj.values;
|
currentQueryError.sqlValues = queryObj.values;
|
||||||
|
|
||||||
const queryObjRes = await connPool.query(
|
const queryObjRes = await conn.query(
|
||||||
trimQuery(queryObj.query),
|
trimQuery(queryObj.query),
|
||||||
queryObj.values
|
queryObj.values
|
||||||
);
|
);
|
||||||
@ -90,6 +90,9 @@ export default async function connDbHandler<ReturnType = any>(
|
|||||||
`Connection DB Handler Query Error`,
|
`Connection DB Handler Query Error`,
|
||||||
error as Error
|
error as Error
|
||||||
);
|
);
|
||||||
|
|
||||||
|
console.log("query", query);
|
||||||
|
|
||||||
resArray.push(null);
|
resArray.push(null);
|
||||||
currentQueryError["error"] = error.message;
|
currentQueryError["error"] = error.message;
|
||||||
queryErrorArray.push(currentQueryError);
|
queryErrorArray.push(currentQueryError);
|
||||||
@ -130,7 +133,7 @@ export default async function connDbHandler<ReturnType = any>(
|
|||||||
// config: conn,
|
// config: conn,
|
||||||
};
|
};
|
||||||
} finally {
|
} 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";
|
import grabDbSSL from "./backend/grabDbSSL";
|
||||||
|
|
||||||
type Param = {
|
type Param = {
|
||||||
@ -23,11 +23,13 @@ type Param = {
|
|||||||
/**
|
/**
|
||||||
* # Grab General CONNECTION for DSQL
|
* # 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) {
|
if (global.DSQL_USE_LOCAL || param?.local) {
|
||||||
return (
|
return (
|
||||||
global.DSQL_DB_CONN ||
|
global.DSQL_DB_CONN ||
|
||||||
mariadb.createPool({
|
(await mariadb.createConnection({
|
||||||
host: process.env.DSQL_DB_HOST,
|
host: process.env.DSQL_DB_HOST,
|
||||||
user: process.env.DSQL_DB_USERNAME,
|
user: process.env.DSQL_DB_USERNAME,
|
||||||
password: process.env.DSQL_DB_PASSWORD,
|
password: process.env.DSQL_DB_PASSWORD,
|
||||||
@ -37,14 +39,17 @@ export default function grabDSQLConnection(param?: Param): Pool {
|
|||||||
: undefined,
|
: undefined,
|
||||||
charset: "utf8mb4",
|
charset: "utf8mb4",
|
||||||
ssl: grabDbSSL(),
|
ssl: grabDbSSL(),
|
||||||
})
|
supportBigNumbers: true,
|
||||||
|
bigNumberStrings: false,
|
||||||
|
dateStrings: true,
|
||||||
|
}))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (param?.ro) {
|
if (param?.ro) {
|
||||||
return (
|
return (
|
||||||
global.DSQL_READ_ONLY_DB_CONN ||
|
global.DSQL_READ_ONLY_DB_CONN ||
|
||||||
mariadb.createPool({
|
(await mariadb.createConnection({
|
||||||
host: process.env.DSQL_DB_HOST,
|
host: process.env.DSQL_DB_HOST,
|
||||||
user: process.env.DSQL_DB_READ_ONLY_USERNAME,
|
user: process.env.DSQL_DB_READ_ONLY_USERNAME,
|
||||||
password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
|
password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
|
||||||
@ -53,14 +58,17 @@ export default function grabDSQLConnection(param?: Param): Pool {
|
|||||||
: undefined,
|
: undefined,
|
||||||
charset: "utf8mb4",
|
charset: "utf8mb4",
|
||||||
ssl: grabDbSSL(),
|
ssl: grabDbSSL(),
|
||||||
})
|
supportBigNumbers: true,
|
||||||
|
bigNumberStrings: false,
|
||||||
|
dateStrings: true,
|
||||||
|
}))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (param?.fa) {
|
if (param?.fa) {
|
||||||
return (
|
return (
|
||||||
global.DSQL_FULL_ACCESS_DB_CONN ||
|
global.DSQL_FULL_ACCESS_DB_CONN ||
|
||||||
mariadb.createPool({
|
(await mariadb.createConnection({
|
||||||
host: process.env.DSQL_DB_HOST,
|
host: process.env.DSQL_DB_HOST,
|
||||||
user: process.env.DSQL_DB_FULL_ACCESS_USERNAME,
|
user: process.env.DSQL_DB_FULL_ACCESS_USERNAME,
|
||||||
password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD,
|
password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD,
|
||||||
@ -69,13 +77,16 @@ export default function grabDSQLConnection(param?: Param): Pool {
|
|||||||
: undefined,
|
: undefined,
|
||||||
charset: "utf8mb4",
|
charset: "utf8mb4",
|
||||||
ssl: grabDbSSL(),
|
ssl: grabDbSSL(),
|
||||||
})
|
supportBigNumbers: true,
|
||||||
|
bigNumberStrings: false,
|
||||||
|
dateStrings: true,
|
||||||
|
}))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
global.DSQL_DB_CONN ||
|
global.DSQL_DB_CONN ||
|
||||||
mariadb.createPool({
|
(await mariadb.createConnection({
|
||||||
host: process.env.DSQL_DB_HOST,
|
host: process.env.DSQL_DB_HOST,
|
||||||
user: process.env.DSQL_DB_USERNAME,
|
user: process.env.DSQL_DB_USERNAME,
|
||||||
password: process.env.DSQL_DB_PASSWORD,
|
password: process.env.DSQL_DB_PASSWORD,
|
||||||
@ -85,6 +96,9 @@ export default function grabDSQLConnection(param?: Param): Pool {
|
|||||||
: undefined,
|
: undefined,
|
||||||
charset: "utf8mb4",
|
charset: "utf8mb4",
|
||||||
ssl: grabDbSSL(),
|
ssl: grabDbSSL(),
|
||||||
})
|
supportBigNumbers: true,
|
||||||
|
bigNumberStrings: false,
|
||||||
|
dateStrings: true,
|
||||||
|
}))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -8,15 +8,15 @@ type Params = {
|
|||||||
connectionLimit?: number;
|
connectionLimit?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function setupDSQLDb({
|
export default async function setupDSQLDb({
|
||||||
useLocal,
|
useLocal,
|
||||||
dbConfig,
|
dbConfig,
|
||||||
ssl,
|
ssl,
|
||||||
connectionLimit = 100,
|
connectionLimit = 20,
|
||||||
}: Params) {
|
}: Params) {
|
||||||
global.DSQL_USE_LOCAL = useLocal || true;
|
global.DSQL_USE_LOCAL = useLocal || true;
|
||||||
|
|
||||||
const pool = mariadb.createPool({
|
const conn = await mariadb.createConnection({
|
||||||
host: process.env.DSQL_DB_HOST,
|
host: process.env.DSQL_DB_HOST,
|
||||||
user: process.env.DSQL_DB_USERNAME,
|
user: process.env.DSQL_DB_USERNAME,
|
||||||
password: process.env.DSQL_DB_PASSWORD,
|
password: process.env.DSQL_DB_PASSWORD,
|
||||||
@ -24,10 +24,26 @@ export default function setupDSQLDb({
|
|||||||
charset: "utf8mb4",
|
charset: "utf8mb4",
|
||||||
...dbConfig,
|
...dbConfig,
|
||||||
ssl: ssl ? grabDbSSL() : undefined,
|
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;
|
// let readOnlyConnection;
|
||||||
|
|
||||||
@ -47,7 +63,7 @@ export default function setupDSQLDb({
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
return {
|
return {
|
||||||
pool,
|
conn,
|
||||||
// readOnlyConnection,
|
// readOnlyConnection,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@moduletrace/datasquirel",
|
"name": "@moduletrace/datasquirel",
|
||||||
"version": "4.8.7",
|
"version": "4.8.8",
|
||||||
"description": "Cloud-based SQL data management tool",
|
"description": "Cloud-based SQL data management tool",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"bin": {
|
"bin": {
|
||||||
|
Loading…
Reference in New Issue
Block a user