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,8 @@
export type ExportMariaDBDatabaseParam = {
dbFullName: string;
targetFilePath: string;
mariadbUser?: string;
mariadbHost?: string;
mariadbPass?: string;
};
export default function importMariadbDatabase({ dbFullName, targetFilePath, mariadbHost, mariadbPass, mariadbUser, }: ExportMariaDBDatabaseParam): Promise<string | Buffer<ArrayBufferLike>>;
@@ -0,0 +1,37 @@
"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 = importMariadbDatabase;
const child_process_1 = require("child_process");
const os_1 = __importDefault(require("os"));
const conn_db_handler_1 = __importDefault(require("../db/conn-db-handler"));
function importMariadbDatabase(_a) {
return __awaiter(this, arguments, void 0, function* ({ dbFullName, targetFilePath, mariadbHost, mariadbPass, mariadbUser, }) {
const mysqlPath = os_1.default.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;
yield (0, conn_db_handler_1.default)(global.DSQL_DB_CONN, `CREATE DATABASE IF NOT EXISTS ${dbFullName}`);
const cmd = `${mysqlPath} -u ${finalMariadbUser} -h ${finalMariadbHost} -p${finalMariadbPass} ${dbFullName} < ${targetFilePath}`;
let execSyncOptions = {
encoding: "utf-8",
};
const importDb = (0, child_process_1.execSync)(cmd, execSyncOptions);
return importDb;
});
}
@@ -19,5 +19,7 @@ export default function grabDirNames(param?: Param): {
userPrivateTempJSONSchemaFilePath: string | undefined;
userPrivateDbExportZipFileName: string;
userPrivateDbExportZipFilePath: string | undefined;
userPrivateDbImportZipFileName: string;
userPrivateDbImportZipFilePath: string | undefined;
};
export {};
+7 -1
View File
@@ -15,7 +15,7 @@ function grabDirNames(param) {
if (!schemasDir)
throw new Error("Please provide the `DSQL_DB_SCHEMA_DIR` env variable.");
const pakageSharedDir = path_1.default.join(appDir, `package-shared`);
const mainDbTypeDefFile = path_1.default.join(appDir, `types/dsql.ts`);
const mainDbTypeDefFile = path_1.default.join(pakageSharedDir, `types/dsql.ts`);
const mainShemaJSONFilePath = path_1.default.join(schemasDir, `main.json`);
const defaultTableFieldsJSONFilePath = path_1.default.join(pakageSharedDir, `data/defaultFields.json`);
const usersSchemaDir = path_1.default.join(schemasDir, `users`);
@@ -46,6 +46,10 @@ function grabDirNames(param) {
const userPrivateDbExportZipFilePath = userPrivateSQLExportsDir
? path_1.default.join(userPrivateSQLExportsDir, userPrivateDbExportZipFileName)
: undefined;
const userPrivateDbImportZipFileName = `db-export.zip`;
const userPrivateDbImportZipFilePath = userPrivateSQLExportsDir
? path_1.default.join(userPrivateSQLExportsDir, userPrivateDbImportZipFileName)
: undefined;
return {
schemasDir,
userDirPath,
@@ -62,5 +66,7 @@ function grabDirNames(param) {
userPrivateTempJSONSchemaFilePath,
userPrivateDbExportZipFileName,
userPrivateDbExportZipFilePath,
userPrivateDbImportZipFileName,
userPrivateDbImportZipFilePath,
};
}
@@ -0,0 +1,6 @@
type Param = {
str: string;
userId: string | number;
};
export default function replaceDatasquirelDbName({ str, userId, }: Param): string;
export {};
@@ -0,0 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = replaceDatasquirelDbName;
function replaceDatasquirelDbName({ str, userId, }) {
const dbNamePrefix = process.env.DSQL_USER_DB_PREFIX;
const userNameRegex = new RegExp(`${dbNamePrefix}\\d+_`, "g");
const newPrefix = `${dbNamePrefix}${userId}_`;
return str.replace(userNameRegex, newPrefix);
}