This commit is contained in:
Benjamin Toby
2025-02-13 08:18:46 +01:00
parent b578843c6d
commit 8b3553fcd5
48 changed files with 1642 additions and 203 deletions
@@ -0,0 +1,44 @@
import { execSync, ExecSyncOptions } from "child_process";
import os from "os";
import connDbHandler from "../db/conn-db-handler";
export type ExportMariaDBDatabaseParam = {
dbFullName: string;
targetFilePath: string;
mariadbUser?: string;
mariadbHost?: string;
mariadbPass?: string;
};
export default async function importMariadbDatabase({
dbFullName,
targetFilePath,
mariadbHost,
mariadbPass,
mariadbUser,
}: ExportMariaDBDatabaseParam) {
const mysqlPath = os.platform().match(/win/i)
? "'" +
"C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysql.exe" +
"'"
: "mysql";
const finalMariadbUser = mariadbUser || process.env.DSQL_DB_USERNAME;
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}`
);
const cmd = `${mysqlPath} -u ${finalMariadbUser} -h ${finalMariadbHost} -p${finalMariadbPass} ${dbFullName} < ${targetFilePath}`;
let execSyncOptions: ExecSyncOptions = {
encoding: "utf-8",
};
const importDb = execSync(cmd, execSyncOptions);
return importDb;
}
@@ -20,7 +20,7 @@ export default function grabDirNames(param?: Param) {
const pakageSharedDir = path.join(appDir, `package-shared`);
const mainDbTypeDefFile = path.join(appDir, `types/dsql.ts`);
const mainDbTypeDefFile = path.join(pakageSharedDir, `types/dsql.ts`);
const mainShemaJSONFilePath = path.join(schemasDir, `main.json`);
const defaultTableFieldsJSONFilePath = path.join(
pakageSharedDir,
@@ -57,6 +57,11 @@ export default function grabDirNames(param?: Param) {
? path.join(userPrivateSQLExportsDir, userPrivateDbExportZipFileName)
: undefined;
const userPrivateDbImportZipFileName = `db-export.zip`;
const userPrivateDbImportZipFilePath = userPrivateSQLExportsDir
? path.join(userPrivateSQLExportsDir, userPrivateDbImportZipFileName)
: undefined;
return {
schemasDir,
userDirPath,
@@ -73,5 +78,7 @@ export default function grabDirNames(param?: Param) {
userPrivateTempJSONSchemaFilePath,
userPrivateDbExportZipFileName,
userPrivateDbExportZipFilePath,
userPrivateDbImportZipFileName,
userPrivateDbImportZipFilePath,
};
}
@@ -0,0 +1,14 @@
type Param = {
str: string;
userId: string | number;
};
export default function replaceDatasquirelDbName({
str,
userId,
}: Param): string {
const dbNamePrefix = process.env.DSQL_USER_DB_PREFIX;
const userNameRegex = new RegExp(`${dbNamePrefix}\\d+_`, "g");
const newPrefix = `${dbNamePrefix}${userId}_`;
return str.replace(userNameRegex, newPrefix);
}
+32
View File
@@ -0,0 +1,32 @@
const consoleColors = {
Reset: "\x1b[0m",
Bright: "\x1b[1m",
Dim: "\x1b[2m",
Underscore: "\x1b[4m",
Blink: "\x1b[5m",
Reverse: "\x1b[7m",
Hidden: "\x1b[8m",
FgBlack: "\x1b[30m",
FgRed: "\x1b[31m",
FgGreen: "\x1b[32m",
FgYellow: "\x1b[33m",
FgBlue: "\x1b[34m",
FgMagenta: "\x1b[35m",
FgCyan: "\x1b[36m",
FgWhite: "\x1b[37m",
FgGray: "\x1b[90m",
BgBlack: "\x1b[40m",
BgRed: "\x1b[41m",
BgGreen: "\x1b[42m",
BgYellow: "\x1b[43m",
BgBlue: "\x1b[44m",
BgMagenta: "\x1b[45m",
BgCyan: "\x1b[46m",
BgWhite: "\x1b[47m",
BgGray: "\x1b[100m",
};
export default consoleColors;
export const ccol = consoleColors;
+60
View File
@@ -0,0 +1,60 @@
import { ccol } from "../console-colors";
const LogTypes = ["error", "warning"] as const;
type Param = {
/**
* data to be logged.
*/
log: any;
/**
* Log Title. Could be name of function or name of variable
*/
title?: string;
/**
* Label for the log
*/
label?: string;
/**
* Log type. `error` or `warning` or default
*/
type?: (typeof LogTypes)[number];
/**
* Whether to add a time stamp
*/
addTime?: boolean;
};
export default function debugLog({ log, label, title, type, addTime }: Param) {
const logType = (() => {
switch (type) {
case "error":
return ccol.FgRed;
case "warning":
return ccol.FgYellow;
default:
return ccol.FgGreen;
}
})();
let logTxt = `${logType}DEBUG${ccol.Reset}:::`;
const date = new Date();
const time = date.toLocaleTimeString("en-US", {
hour: "numeric",
minute: "numeric",
second: "numeric",
hour12: true,
});
const logTime = `${date.toLocaleDateString()}][${time}`;
if (addTime) logTxt = `${ccol.BgWhite}[${logTime}]${ccol.Reset} ` + logTxt;
if (title) logTxt += `${ccol.FgBlue}${title}${ccol.Reset}::`;
if (label)
logTxt += `${ccol.FgWhite}${ccol.Bright}${label}${ccol.Reset} =>`;
console.log(logTxt, log);
}