Updates
This commit is contained in:
@@ -1,30 +0,0 @@
|
||||
import grabDSQLConnection from "../../grab-dsql-connection";
|
||||
|
||||
/**
|
||||
* # DSQL user read-only DB handler
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
export default async function DB_HANDLER(query: string, values?: any[]) {
|
||||
const CONNECTION = await grabDSQLConnection();
|
||||
|
||||
try {
|
||||
if (!CONNECTION)
|
||||
throw new Error("No Connection provided to DB_HANDLER function!");
|
||||
|
||||
const results = await CONNECTION.query(query, values);
|
||||
|
||||
if (Array.isArray(results)) {
|
||||
return Array.from(results);
|
||||
} else {
|
||||
return results;
|
||||
}
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(`DB_HANDLER Error`, error as Error);
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
} finally {
|
||||
await CONNECTION?.end();
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
import connDbHandler from "../../db/conn-db-handler";
|
||||
import grabDSQLConnection from "../../grab-dsql-connection";
|
||||
|
||||
type Param = {
|
||||
paradigm: "Full Access" | "FA" | "Read Only";
|
||||
queryString: string;
|
||||
queryValues?: string[];
|
||||
};
|
||||
|
||||
/**
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
export default async function DSQL_USER_DB_HANDLER({
|
||||
paradigm,
|
||||
queryString,
|
||||
queryValues,
|
||||
}: Param) {
|
||||
const CONNECTION =
|
||||
paradigm == "Read Only"
|
||||
? await grabDSQLConnection({ ro: true })
|
||||
: await grabDSQLConnection({ fa: true });
|
||||
|
||||
try {
|
||||
return await connDbHandler(CONNECTION, queryString, queryValues);
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(`DSQL_USER_DB_HANDLER Error`, error as Error);
|
||||
return null;
|
||||
} finally {
|
||||
await CONNECTION?.end();
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
import grabDSQLConnection from "../../grab-dsql-connection";
|
||||
|
||||
/**
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
export default async function LOCAL_DB_HANDLER(query: string, values?: any[]) {
|
||||
const CONNECTION = await grabDSQLConnection();
|
||||
|
||||
try {
|
||||
const results = await CONNECTION.query(query, values);
|
||||
|
||||
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 {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
} finally {
|
||||
await CONNECTION?.end();
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
import grabDSQLConnection from "../../grab-dsql-connection";
|
||||
|
||||
/**
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
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(async (results) => {
|
||||
if (Array.isArray(results)) {
|
||||
resolve(Array.from(results));
|
||||
} else {
|
||||
resolve(results);
|
||||
}
|
||||
})
|
||||
.catch(async (err) => {
|
||||
resolve({
|
||||
error: err.message,
|
||||
sql: err.sql,
|
||||
});
|
||||
})
|
||||
.finally(async () => {
|
||||
await CONNECTION?.end();
|
||||
});
|
||||
});
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(`NO_DB_HANDLER Error`, error as Error);
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -8,20 +8,21 @@ type Return = ConnectionConfig["ssl"] | undefined;
|
||||
* # Grab SSL
|
||||
*/
|
||||
export default function grabDbSSL(): Return {
|
||||
const { maxscaleSSLDir } = grabDirNames();
|
||||
if (!maxscaleSSLDir?.match(/./)) {
|
||||
const { maxscaleSSLCaCertFile } = grabDirNames();
|
||||
|
||||
const caFilePath = process.env.DSQL_SSL_CA_CERT || maxscaleSSLCaCertFile;
|
||||
|
||||
if (!caFilePath?.match(/./)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const caFilePath = `${maxscaleSSLDir}/ca-cert.pem`;
|
||||
|
||||
if (!fs.existsSync(caFilePath)) {
|
||||
console.log(`${caFilePath} does not exist`);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
ca: fs.readFileSync(`${maxscaleSSLDir}/ca-cert.pem`),
|
||||
ca: fs.readFileSync(caFilePath),
|
||||
rejectUnauthorized: false,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -27,10 +27,7 @@ export default async function importMariadbDatabase({
|
||||
const finalMariadbHost = mariadbHost || process.env.DSQL_DB_HOST;
|
||||
const finalMariadbPass = mariadbPass || process.env.DSQL_DB_PASSWORD;
|
||||
|
||||
await connDbHandler(
|
||||
global.DSQL_DB_CONN,
|
||||
`CREATE DATABASE IF NOT EXISTS ${dbFullName}`
|
||||
);
|
||||
await connDbHandler(null, `CREATE DATABASE IF NOT EXISTS ${dbFullName}`);
|
||||
|
||||
const cmd = `${mysqlPath} -u ${finalMariadbUser} -h ${finalMariadbHost} -p"${finalMariadbPass}" ${dbFullName} < ${targetFilePath}`;
|
||||
|
||||
|
||||
@@ -45,11 +45,43 @@ export default function grabDirNames(param?: Param) {
|
||||
const appSSLDir = path.join(appDir, "ssl");
|
||||
const mainSSLDir = path.join(DATA_DIR, "ssl");
|
||||
|
||||
const installScriptFile = path.join(
|
||||
appDir,
|
||||
"scripts",
|
||||
"shell",
|
||||
"installation",
|
||||
"install.sh"
|
||||
);
|
||||
const updateScriptFile = path.join(
|
||||
appDir,
|
||||
"scripts",
|
||||
"shell",
|
||||
"installation",
|
||||
"update.sh"
|
||||
);
|
||||
|
||||
const installTypescriptFile = path.join(
|
||||
appDir,
|
||||
"scripts",
|
||||
"shell",
|
||||
"installation",
|
||||
"install.ts"
|
||||
);
|
||||
const updateTypescriptFile = path.join(
|
||||
appDir,
|
||||
"scripts",
|
||||
"shell",
|
||||
"installation",
|
||||
"update.ts"
|
||||
);
|
||||
|
||||
const maxscaleSSLDir = path.join(mainSSLDir, "maxscale");
|
||||
const mainDBSSLDir = path.join(mainSSLDir, "main");
|
||||
const replica1DBSSLDir = path.join(mainSSLDir, "replica-1");
|
||||
const replica2DBSSLDir = path.join(mainSSLDir, "replica-2");
|
||||
|
||||
const maxscaleSSLCaCertFile = path.join(maxscaleSSLDir, "ca-cert.pem");
|
||||
|
||||
const privateDataDir = path.join(DATA_DIR, "private");
|
||||
|
||||
/**
|
||||
@@ -158,15 +190,32 @@ export default function grabDirNames(param?: Param) {
|
||||
|
||||
let dockerComposeFile = path.join(appDir, "docker-compose.yml");
|
||||
let dockerComposeFileAlt = path.join(appDir, "docker-compose.yaml");
|
||||
const dbDockerComposeFileName = "db.docker-compose.yml";
|
||||
const dbDockerComposeFileNameAlt = "db.docker-compose.yaml";
|
||||
const dsqlDockerComposeFileName = "dsql.docker-compose.yml";
|
||||
const dsqlDockerComposeFileNameAlt = "dsql.docker-compose.yaml";
|
||||
const dsqlDbDockerComposeFileName = "dsql-db.docker-compose.yml";
|
||||
const dsqlDbDockerComposeFileNameAlt = "dsql-db.docker-compose.yaml";
|
||||
|
||||
const dsqlDockerComposeFile = path.join(appDir, dsqlDockerComposeFileName);
|
||||
const dsqlDockerComposeFileAlt = path.join(
|
||||
appDir,
|
||||
dsqlDockerComposeFileNameAlt
|
||||
);
|
||||
const dbDockerComposeFile = path.join(appDir, "db.docker-compose.yml");
|
||||
const dbDockerComposeFileAlt = path.join(appDir, "db.docker-compose.yaml");
|
||||
const dbDockerComposeFile = path.join(appDir, dbDockerComposeFileName);
|
||||
const dbDockerComposeFileAlt = path.join(
|
||||
appDir,
|
||||
dbDockerComposeFileNameAlt
|
||||
);
|
||||
const dsqlDbDockerComposeFile = path.join(
|
||||
appDir,
|
||||
dsqlDbDockerComposeFileName
|
||||
);
|
||||
const dsqlDbDockerComposeFileAlt = path.join(
|
||||
appDir,
|
||||
dsqlDbDockerComposeFileNameAlt
|
||||
);
|
||||
|
||||
const extraDockerComposeFile = path.join(
|
||||
appDir,
|
||||
"extra.docker-compose.yml"
|
||||
@@ -282,6 +331,7 @@ export default function grabDirNames(param?: Param) {
|
||||
schemasBackupDirName,
|
||||
userMainShemaJSONFilePath,
|
||||
maxscaleSSLDir,
|
||||
maxscaleSSLCaCertFile,
|
||||
mainDBSSLDir,
|
||||
replica1DBSSLDir,
|
||||
replica2DBSSLDir,
|
||||
@@ -304,5 +354,13 @@ export default function grabDirNames(param?: Param) {
|
||||
distroEnterpriseExportTarName,
|
||||
communityDistroTempDir,
|
||||
communityDistroDir,
|
||||
installScriptFile,
|
||||
updateScriptFile,
|
||||
installTypescriptFile,
|
||||
updateTypescriptFile,
|
||||
dsqlDbDockerComposeFile,
|
||||
dsqlDbDockerComposeFileAlt,
|
||||
dsqlDbDockerComposeFileName,
|
||||
dsqlDbDockerComposeFileNameAlt,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import grabDockerResourceIPNumbers from "../../grab-docker-resource-ip-numbers";
|
||||
|
||||
export default function grabIPAddresses() {
|
||||
const globalIPPrefix = process.env.DSQL_NETWORK_IP_PREFIX || "172.72.0";
|
||||
|
||||
const {
|
||||
cron,
|
||||
db,
|
||||
@@ -19,6 +20,7 @@ export default function grabIPAddresses() {
|
||||
const maxScaleIP = `${globalIPPrefix}.${maxscale}`;
|
||||
const mainDBIP = `${globalIPPrefix}.${db}`;
|
||||
const webSocketIP = `${globalIPPrefix}.${websocket}`;
|
||||
const dbCronIP = `${globalIPPrefix}.${db_cron}`;
|
||||
const localHostIP = `${globalIPPrefix}.1`;
|
||||
|
||||
return {
|
||||
@@ -29,5 +31,6 @@ export default function grabIPAddresses() {
|
||||
localHostIP,
|
||||
globalIPPrefix,
|
||||
webSocketIP,
|
||||
dbCronIP,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -45,7 +45,6 @@ export default function parseCookies({
|
||||
|
||||
return cookieObject;
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(`Parse Cookies Error`, error as Error);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,6 @@ type Param = {
|
||||
export default function checkIfIsMaster({ dbContext, dbFullName }: Param) {
|
||||
return dbContext?.match(/dsql.user/i)
|
||||
? false
|
||||
: global.DSQL_USE_LOCAL
|
||||
? true
|
||||
: dbFullName && !dbFullName.match(/^datasquirel$/)
|
||||
? false
|
||||
: true;
|
||||
|
||||
@@ -33,8 +33,6 @@ export default async function dsqlCrud<
|
||||
sanitize ? sanitize({ batchData }) : batchData
|
||||
) as T[];
|
||||
|
||||
const DB_CONN = global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;
|
||||
|
||||
switch (action) {
|
||||
case "get":
|
||||
return await dsqlCrudGet(params);
|
||||
@@ -79,7 +77,7 @@ export default async function dsqlCrud<
|
||||
});
|
||||
|
||||
const res = (await connDbHandler(
|
||||
DB_CONN,
|
||||
undefined,
|
||||
deleteQuery?.query,
|
||||
deleteQuery?.values
|
||||
)) as PostInsertReturn;
|
||||
|
||||
@@ -213,7 +213,6 @@ export default async function dsqlMethodCrud<
|
||||
}
|
||||
return result;
|
||||
} catch (error) {
|
||||
global.ERROR_CALLBACK?.(`Method Crud Error`, error as Error);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ export default async function connDbHandler<ReturnType = any>(
|
||||
/**
|
||||
* MariaDB Connection
|
||||
*/
|
||||
conn?: mariadb.Connection,
|
||||
conn?: mariadb.Connection | null,
|
||||
/**
|
||||
* String Or `ConnDBHandlerQueryObject` Array
|
||||
*/
|
||||
@@ -43,21 +43,20 @@ export default async function connDbHandler<ReturnType = any>(
|
||||
let queryErrorArray: DSQLErrorObject[] = [];
|
||||
|
||||
if (typeof query == "string") {
|
||||
const res = await finalConnection.query(trimQuery(query), values);
|
||||
const [results] = await finalConnection.query(
|
||||
trimQuery(query),
|
||||
values
|
||||
);
|
||||
|
||||
if (debug) {
|
||||
debugLog({
|
||||
log: res,
|
||||
log: results,
|
||||
addTime: true,
|
||||
label: "res",
|
||||
});
|
||||
}
|
||||
|
||||
if (Array.isArray(res)) {
|
||||
return Array.from(res);
|
||||
}
|
||||
|
||||
return res;
|
||||
return results;
|
||||
} else if (typeof query == "object") {
|
||||
const resArray = [];
|
||||
|
||||
@@ -75,25 +74,18 @@ export default async function connDbHandler<ReturnType = any>(
|
||||
queryObj.values
|
||||
);
|
||||
|
||||
const results = queryObjRes[0];
|
||||
|
||||
if (debug) {
|
||||
debugLog({
|
||||
log: queryObjRes,
|
||||
log: results,
|
||||
addTime: true,
|
||||
label: "queryObjRes",
|
||||
});
|
||||
}
|
||||
|
||||
if (Array.isArray(queryObjRes)) {
|
||||
resArray.push(Array.from(queryObjRes));
|
||||
} else {
|
||||
resArray.push(queryObjRes);
|
||||
}
|
||||
resArray.push(results);
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(
|
||||
`Connection DB Handler Query Error`,
|
||||
error as Error
|
||||
);
|
||||
|
||||
console.log("query", query);
|
||||
|
||||
resArray.push(null);
|
||||
@@ -121,8 +113,6 @@ export default async function connDbHandler<ReturnType = any>(
|
||||
return null;
|
||||
}
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(`Connection DB Handler Error`, error as Error);
|
||||
|
||||
if (debug) {
|
||||
debugLog({
|
||||
log: `Connection DB Handler Error: ${error.message}`,
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import debugLog from "./logging/debug-log";
|
||||
import mariaDBlocalQuery from "./mariadb-local-query";
|
||||
|
||||
let checkDbRetries = 0;
|
||||
const MAX_CHECK_DB_RETRIES = 10;
|
||||
const DEFAULT_SLEEP_TIME = 3000;
|
||||
|
||||
type Params = {
|
||||
maxRetries?: number;
|
||||
sleepTime?: number;
|
||||
};
|
||||
|
||||
export default async function dockerTestDbConnection(params?: Params) {
|
||||
const maxRetries = params?.maxRetries || MAX_CHECK_DB_RETRIES;
|
||||
const sleepTime = params?.sleepTime || DEFAULT_SLEEP_TIME;
|
||||
|
||||
while (true) {
|
||||
if (checkDbRetries > maxRetries) {
|
||||
debugLog({
|
||||
log: `Max Retries for checking Database. Exiting ...`,
|
||||
addTime: true,
|
||||
label: "MaxRetries",
|
||||
});
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const checkDb = mariaDBlocalQuery(`SHOW DATABASES`);
|
||||
|
||||
const isDbReady =
|
||||
typeof checkDb == "string" &&
|
||||
Boolean(checkDb.match(/\ninformation_schema\n/));
|
||||
|
||||
if (isDbReady) {
|
||||
break;
|
||||
} else {
|
||||
checkDbRetries++;
|
||||
await Bun.sleep(sleepTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import debugLog from "./logging/debug-log";
|
||||
import mariaDBlocalQuery from "./mariadb-local-query";
|
||||
|
||||
let checkDbRetries = 0;
|
||||
const MAX_CHECK_DB_RETRIES = 10;
|
||||
const DEFAULT_SLEEP_TIME = 3000;
|
||||
|
||||
type Params = {
|
||||
maxRetries?: number;
|
||||
sleepTime?: number;
|
||||
};
|
||||
|
||||
export default async function dockerTestMaxscaleConnection(params?: Params) {
|
||||
const maxRetries = params?.maxRetries || MAX_CHECK_DB_RETRIES;
|
||||
const sleepTime = params?.sleepTime || DEFAULT_SLEEP_TIME;
|
||||
|
||||
// while (true) {
|
||||
// if (checkDbRetries > maxRetries) {
|
||||
// debugLog({
|
||||
// log: `Max Retries for checking Database. Exiting ...`,
|
||||
// addTime: true,
|
||||
// label: "MaxRetries",
|
||||
// });
|
||||
// process.exit(1);
|
||||
// }
|
||||
|
||||
// const checkDb = mariaDBlocalQuery(`SHOW DATABASES`);
|
||||
|
||||
// const isDbReady =
|
||||
// typeof checkDb == "string" &&
|
||||
// Boolean(checkDb.match(/\ninformation_schema\n/));
|
||||
|
||||
// if (isDbReady) {
|
||||
// break;
|
||||
// } else {
|
||||
// checkDbRetries++;
|
||||
// await Bun.sleep(sleepTime);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import { ExecOptions, execSync, ExecSyncOptions } from "child_process";
|
||||
|
||||
export default function execute(
|
||||
cmd: string | string[],
|
||||
options?: ExecSyncOptions
|
||||
): string | (string | undefined)[] | undefined {
|
||||
function runCmd(cmd: string) {
|
||||
try {
|
||||
const res = execSync(cmd, {
|
||||
encoding: "utf-8",
|
||||
...options,
|
||||
});
|
||||
|
||||
if (typeof res == "string") {
|
||||
return res.trim();
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.log(`Execute Run Error =>`, error.message);
|
||||
console.log(`Execute CMD =>`, cmd);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (typeof cmd == "string") {
|
||||
return runCmd(cmd);
|
||||
} else if (Array.isArray(cmd)) {
|
||||
let resArr: (string | undefined)[] = [];
|
||||
|
||||
for (let i = 0; i < cmd.length; i++) {
|
||||
const singleCmd = cmd[i];
|
||||
const res = runCmd(singleCmd);
|
||||
resArr.push(res);
|
||||
}
|
||||
|
||||
return resArr;
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.log(`Execute Error =>`, error.message);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
export default function grabAPIKey(key?: string) {
|
||||
return (
|
||||
key ||
|
||||
process.env.DSQL_API_KEY ||
|
||||
process.env.DSQL_FULL_ACCESS_API_KEY ||
|
||||
process.env.DSQL_READ_ONLY_API_KEY
|
||||
);
|
||||
}
|
||||
@@ -1,23 +1,19 @@
|
||||
import mariadb, { Connection, Pool } from "mariadb";
|
||||
import mariadb, { Connection, ConnectionConfig } from "mariadb";
|
||||
import grabDbSSL from "./backend/grabDbSSL";
|
||||
|
||||
type Param = {
|
||||
/**
|
||||
* Read Only?
|
||||
*/
|
||||
ro?: boolean;
|
||||
/**
|
||||
* Full Access?
|
||||
*/
|
||||
fa?: boolean;
|
||||
/**
|
||||
* No Database Connection
|
||||
*/
|
||||
noDb?: boolean;
|
||||
/**
|
||||
* Is this a local connection?
|
||||
* Database Name
|
||||
*/
|
||||
local?: boolean;
|
||||
database?: string;
|
||||
/**
|
||||
* Debug
|
||||
*/
|
||||
config?: ConnectionConfig;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -26,94 +22,106 @@ type Param = {
|
||||
export default async function grabDSQLConnection(
|
||||
param?: Param
|
||||
): Promise<Connection> {
|
||||
return await mariadb.createConnection({
|
||||
const config: ConnectionConfig = {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: param?.noDb ? undefined : process.env.DSQL_DB_NAME,
|
||||
database:
|
||||
param?.database ||
|
||||
(param?.noDb ? undefined : process.env.DSQL_DB_NAME),
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
bigIntAsNumber: true,
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
});
|
||||
metaAsArray: true,
|
||||
...param?.config,
|
||||
};
|
||||
|
||||
if (global.DSQL_USE_LOCAL || param?.local) {
|
||||
return (
|
||||
global.DSQL_DB_CONN ||
|
||||
(await mariadb.createConnection({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: param?.noDb ? undefined : process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
}))
|
||||
);
|
||||
try {
|
||||
return await mariadb.createConnection(config);
|
||||
} catch (error) {
|
||||
console.log(`Error Grabbing DSQL Connection =>`, config);
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (param?.ro) {
|
||||
return (
|
||||
global.DSQL_READ_ONLY_DB_CONN ||
|
||||
(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,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
}))
|
||||
);
|
||||
}
|
||||
// if (global.DSQL_USE_LOCAL || param?.local) {
|
||||
// return (
|
||||
// global.DSQL_DB_CONN ||
|
||||
// (await mariadb.createConnection({
|
||||
// host: process.env.DSQL_DB_HOST,
|
||||
// user: process.env.DSQL_DB_USERNAME,
|
||||
// password: process.env.DSQL_DB_PASSWORD,
|
||||
// database: param?.noDb ? undefined : process.env.DSQL_DB_NAME,
|
||||
// port: process.env.DSQL_DB_PORT
|
||||
// ? Number(process.env.DSQL_DB_PORT)
|
||||
// : undefined,
|
||||
// charset: "utf8mb4",
|
||||
// ssl: grabDbSSL(),
|
||||
// supportBigNumbers: true,
|
||||
// bigNumberStrings: false,
|
||||
// dateStrings: true,
|
||||
// }))
|
||||
// );
|
||||
// }
|
||||
|
||||
if (param?.fa) {
|
||||
return (
|
||||
global.DSQL_FULL_ACCESS_DB_CONN ||
|
||||
(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,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
}))
|
||||
);
|
||||
}
|
||||
// if (param?.ro) {
|
||||
// return (
|
||||
// global.DSQL_READ_ONLY_DB_CONN ||
|
||||
// (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,
|
||||
// port: process.env.DSQL_DB_PORT
|
||||
// ? Number(process.env.DSQL_DB_PORT)
|
||||
// : undefined,
|
||||
// charset: "utf8mb4",
|
||||
// ssl: grabDbSSL(),
|
||||
// supportBigNumbers: true,
|
||||
// bigNumberStrings: false,
|
||||
// dateStrings: true,
|
||||
// }))
|
||||
// );
|
||||
// }
|
||||
|
||||
return (
|
||||
global.DSQL_DB_CONN ||
|
||||
(await mariadb.createConnection({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: param?.noDb ? undefined : process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
}))
|
||||
);
|
||||
// if (param?.fa) {
|
||||
// return (
|
||||
// global.DSQL_FULL_ACCESS_DB_CONN ||
|
||||
// (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,
|
||||
// port: process.env.DSQL_DB_PORT
|
||||
// ? Number(process.env.DSQL_DB_PORT)
|
||||
// : undefined,
|
||||
// charset: "utf8mb4",
|
||||
// ssl: grabDbSSL(),
|
||||
// supportBigNumbers: true,
|
||||
// bigNumberStrings: false,
|
||||
// dateStrings: true,
|
||||
// }))
|
||||
// );
|
||||
// }
|
||||
|
||||
// return (
|
||||
// global.DSQL_DB_CONN ||
|
||||
// (await mariadb.createConnection({
|
||||
// host: process.env.DSQL_DB_HOST,
|
||||
// user: process.env.DSQL_DB_USERNAME,
|
||||
// password: process.env.DSQL_DB_PASSWORD,
|
||||
// database: param?.noDb ? undefined : process.env.DSQL_DB_NAME,
|
||||
// port: process.env.DSQL_DB_PORT
|
||||
// ? Number(process.env.DSQL_DB_PORT)
|
||||
// : undefined,
|
||||
// charset: "utf8mb4",
|
||||
// ssl: grabDbSSL(),
|
||||
// supportBigNumbers: true,
|
||||
// bigNumberStrings: false,
|
||||
// dateStrings: true,
|
||||
// }))
|
||||
// );
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
export default function grabDSQLRemoteURLs() {
|
||||
const communityTarballURL =
|
||||
"https://static.datasquirel.com/images/user-images/user-2/dsql/distro/dsql-community.tar.xz";
|
||||
const communityTarballShortURL =
|
||||
"https://datasquirel.com/api/media/dsql-community";
|
||||
|
||||
const installScriptURL = "https://datasquirel.com/api/media/install";
|
||||
const updateScriptURL = "https://datasquirel.com/api/media/update";
|
||||
|
||||
return {
|
||||
communityTarballURL,
|
||||
communityTarballShortURL,
|
||||
installScriptURL,
|
||||
updateScriptURL,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import grabDockerStackServicesNames from "@/package-shared/utils/backend/names/grab-docker-stack-services-names";
|
||||
import normalizeText from "@/package-shared/utils/normalize-text";
|
||||
import execute from "./execute";
|
||||
|
||||
export default function mariaDBlocalQuery(query: string | string[]) {
|
||||
const { dbServiceName, maxScaleServiceName } =
|
||||
grabDockerStackServicesNames();
|
||||
|
||||
const MARIADB_CMD_PREFIX = `docker exec ${dbServiceName} mariadb -u root -p"${process.env.DSQL_MARIADB_ROOT_PASSWORD}"`;
|
||||
|
||||
function grabMariadbDockerCmd(cmd: string) {
|
||||
return `${MARIADB_CMD_PREFIX} -e "${removeQueryDoubleQuotes(
|
||||
normalizeText(cmd)
|
||||
)}"`;
|
||||
}
|
||||
|
||||
const finalQuery = Array.isArray(query)
|
||||
? query.map((qry) => grabMariadbDockerCmd(qry))
|
||||
: grabMariadbDockerCmd(query);
|
||||
|
||||
return execute(finalQuery);
|
||||
}
|
||||
|
||||
export function removeQueryDoubleQuotes(query: string) {
|
||||
return query.replace(/\"/gm, '\\"');
|
||||
}
|
||||
@@ -8,14 +8,7 @@ type Params = {
|
||||
connectionLimit?: number;
|
||||
};
|
||||
|
||||
export default async function setupDSQLDb({
|
||||
useLocal,
|
||||
dbConfig,
|
||||
ssl,
|
||||
connectionLimit = 20,
|
||||
}: Params) {
|
||||
global.DSQL_USE_LOCAL = useLocal || true;
|
||||
|
||||
export default async function setupDSQLDb({ dbConfig, ssl }: Params) {
|
||||
const conn = await mariadb.createConnection({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
@@ -27,6 +20,8 @@ export default async function setupDSQLDb({
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
bigIntAsNumber: true,
|
||||
metaAsArray: true,
|
||||
});
|
||||
|
||||
// const conn = mariadb.createPool({
|
||||
@@ -43,8 +38,6 @@ export default async function setupDSQLDb({
|
||||
// dateStrings: true,
|
||||
// });
|
||||
|
||||
global.DSQL_DB_CONN = conn;
|
||||
|
||||
// let readOnlyConnection;
|
||||
|
||||
// if (addReadOnlyConn) {
|
||||
|
||||
@@ -18,5 +18,8 @@ export default function setupGlobalNetwork() {
|
||||
newNtwkCmd += ` ${globalNetworkName}`;
|
||||
|
||||
execSync(newNtwkCmd);
|
||||
} catch (error) {}
|
||||
} catch (error) {
|
||||
console.log(`Failed to create global network ${globalNetworkName}`);
|
||||
console.log(globalIPPrefix);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import dbHandler from "../functions/backend/dbHandler";
|
||||
|
||||
let testDbConnRetries = 0;
|
||||
const MAX_TEST_DB_CONN_RETRIES = 10;
|
||||
const SLEEP_TIME = 2000;
|
||||
|
||||
type Params = {
|
||||
maxRetries?: number;
|
||||
sleepTime?: number;
|
||||
};
|
||||
|
||||
export default async function testDbConnection(params?: Params) {
|
||||
console.log("Testing Database Connection ...", testDbConnRetries);
|
||||
|
||||
while (true) {
|
||||
testDbConnRetries++;
|
||||
|
||||
try {
|
||||
const res = (await dbHandler({ query: `SHOW DATABASES` })) as
|
||||
| any[]
|
||||
| null;
|
||||
|
||||
if (res?.[0]) {
|
||||
console.log("Database Connection Complete!");
|
||||
break;
|
||||
}
|
||||
|
||||
await Bun.sleep(params?.sleepTime || SLEEP_TIME);
|
||||
|
||||
if (
|
||||
testDbConnRetries >
|
||||
(params?.maxRetries || MAX_TEST_DB_CONN_RETRIES)
|
||||
) {
|
||||
console.log("Database Connection Failed!");
|
||||
process.exit(1);
|
||||
}
|
||||
} catch (error) {
|
||||
await Bun.sleep(params?.sleepTime || SLEEP_TIME);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user