dsql-admin/dsql-app/.local_dist/server/pages/api/updateUserDatabase.js
2024-11-05 12:12:42 +01:00

253 lines
9.8 KiB
JavaScript

"use strict";
(() => {
var exports = {};
exports.id = 9354;
exports.ids = [9354];
exports.modules = {
/***/ 6109:
/***/ ((module) => {
module.exports = require("sanitize-html");
/***/ }),
/***/ 2261:
/***/ ((module) => {
module.exports = require("serverless-mysql");
/***/ }),
/***/ 4300:
/***/ ((module) => {
module.exports = require("buffer");
/***/ }),
/***/ 2081:
/***/ ((module) => {
module.exports = require("child_process");
/***/ }),
/***/ 6113:
/***/ ((module) => {
module.exports = require("crypto");
/***/ }),
/***/ 7147:
/***/ ((module) => {
module.exports = require("fs");
/***/ }),
/***/ 3685:
/***/ ((module) => {
module.exports = require("http");
/***/ }),
/***/ 2037:
/***/ ((module) => {
module.exports = require("os");
/***/ }),
/***/ 1017:
/***/ ((module) => {
module.exports = require("path");
/***/ }),
/***/ 3755:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "default": () => (/* binding */ handler)
/* harmony export */ });
/* harmony import */ var _package_shared_functions_backend_db_updateDbEntry__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(5886);
/* harmony import */ var _package_shared_functions_backend_db_updateDbEntry__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_package_shared_functions_backend_db_updateDbEntry__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(2163);
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(6825);
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var _package_shared_functions_backend_varDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(1311);
/* harmony import */ var _package_shared_functions_backend_varDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(_package_shared_functions_backend_varDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_3__);
// @ts-check
/**
* ==============================================================================
* Imports
* ==============================================================================
*/ const fs = __webpack_require__(7147);
const os = __webpack_require__(2037);
const path = __webpack_require__(1017);
const { execSync } = __webpack_require__(2081);
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
* API handler
* ==============================================================================
* @type {import("next").NextApiHandler}
*/ async function handler(req, res) {
/**
* Check method
*
* @description Check request method and return if invalid
*/ if (req.method !== "POST") return res.json({
msg: "Failed!"
});
/**
* User auth
*
* @description Authenticate user
*/ const user = await _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_2___default()(req, res, true);
if (!user) {
return res.json({
success: false,
msg: "Unauthorized"
});
}
/**
* User auth
*
* @description Authenticate user
*/ const sanitizedReqBody = req.body;
const { id , db_name , db_slug , db_description , db_image , db_full_name , active_clone , active_clone_parent_db , } = sanitizedReqBody;
if (!db_full_name?.match(/datasquirel_user_.*/)) {
res.json({
success: false,
msg: "Unauthorized"
});
}
const newDbFullName = `datasquirel_user_${user.id}_${db_slug}`;
/**
* Update Database info in main db
*
* @description Update Database info in main db
*/ const updateDatabase = await _package_shared_functions_backend_db_updateDbEntry__WEBPACK_IMPORTED_MODULE_0___default()({
dbFullName: "datasquirel",
tableName: "user_databases",
identifierColumnName: "id",
identifierValue: id,
data: {
db_name,
db_slug,
db_description,
db_image,
db_full_name: newDbFullName
}
});
/**
* Update User Specific Database
*
* @description Update User Specific Database
*/ if (db_full_name && db_full_name !== newDbFullName) {
console.log("Renaming Database");
const mysqlPath = os.platform().match(/win/i) ? "'" + "C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysql.exe" + "'" : "mysql";
const mysqlDumpPath = os.platform().match(/win/i) ? "'" + "C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysqldump.exe" + "'" : "mysqldump";
try {
const createNewDb = await _package_shared_functions_backend_varDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_3___default()({
queryString: `CREATE DATABASE \`${newDbFullName}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`,
database: db_full_name
});
/** @type {import("child_process").ExecSyncOptions} */ let execSyncOptions = {
cwd: process.cwd()
};
if (os.platform().match(/win/i)) execSyncOptions.shell = "bash.exe";
const exe = `${mysqlDumpPath} -u ${process.env.DSQL_DB_USERNAME} -h ${process.env.DSQL_DB_HOST} -p${process.env.DSQL_DB_PASSWORD} ${db_full_name} > tmpDbFile.sql`;
console.log(`updateUserDatabase.js exe => ${exe}`);
const dumpDb = execSync(exe, execSyncOptions);
const importToNewDb = execSync(`${mysqlPath} -u ${process.env.DSQL_DB_USERNAME} -h ${process.env.DSQL_DB_HOST} -p${process.env.DSQL_DB_PASSWORD} ${newDbFullName} < tmpDbFile.sql`, execSyncOptions);
const deleteOldDb = await _package_shared_functions_backend_varDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_3___default()({
queryString: `DROP DATABASE \`${db_full_name}\``,
database: db_full_name
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} catch (/** @type {any} */ error) {
_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1___default()({
component: "/api/updateUserDatabase/lines-88-111",
message: error.message,
user: user
});
}
// const updateUserSpecificDatabase = await varDatabaseDbHandler({
// queryString: `RENAME DATABASE \`${db_full_name}\` TO \`${newDbFullName}\``,
// database: db_full_name,
// });
// console.log(updateUserSpecificDatabase);
}
/**
* Send Response
*
* @description Send a boolean response
*/ try {
/**
* Create new user folder and file
*
* @description Create new user folder and file
*/ const userSchemaMainFilePath = `./jsonData/dbSchemas/users/user-${user.id}/main.json`;
/** @type {import("@/package-shared/types").DSQL_DatabaseSchemaType[]} */ let userSchemaData = JSON.parse(fs.readFileSync(userSchemaMainFilePath, "utf8"));
let targetDatabase = userSchemaData.filter((db)=>db.dbFullName === db_full_name);
targetDatabase[0].dbName = db_name;
targetDatabase[0].dbFullName = newDbFullName;
targetDatabase[0].dbDescription = db_description;
targetDatabase[0].dbImage = db_image;
targetDatabase[0].dbSlug = db_slug;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
if (active_clone) {
const parentDb = userSchemaData.filter((db)=>db.dbFullName === targetDatabase[0].childDatabaseDbFullName)[0];
const targetChild = parentDb.childrenDatabases?.find((db)=>db.dbFullName === db_full_name);
if (targetChild) targetChild.dbFullName = newDbFullName;
}
////////////////////////////////////////
fs.writeFileSync(`${userSchemaMainFilePath}`, JSON.stringify(userSchemaData), "utf8");
////////////////////////////////////////
res.json({
success: true
});
////////////////////////////////////////
} catch (/** @type {any} */ error1) {
////////////////////////////////////////
_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1___default()({
component: "/api/updateUserDatabase/main-catch-error",
message: error1.message,
user: user
});
res.json({
success: false,
msg: "Database Files could not be written!"
});
////////////////////////////////////////
}
}
/***/ })
};
;
// load runtime
var __webpack_require__ = require("../../webpack-api-runtime.js");
__webpack_require__.C(exports);
var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
var __webpack_exports__ = __webpack_require__.X(0, [5425,2224,2163,6825,3017,3403,7547,5886,8326,1311], () => (__webpack_exec__(3755)));
module.exports = __webpack_exports__;
})();