304 lines
12 KiB
JavaScript
304 lines
12 KiB
JavaScript
"use strict";
|
|
(() => {
|
|
var exports = {};
|
|
exports.id = 8448;
|
|
exports.ids = [8448];
|
|
exports.modules = {
|
|
|
|
/***/ 6517:
|
|
/***/ ((module) => {
|
|
|
|
module.exports = require("lodash");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 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");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 1017:
|
|
/***/ ((module) => {
|
|
|
|
module.exports = require("path");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 8583:
|
|
/***/ ((__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 _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2163);
|
|
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_0__);
|
|
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(6825);
|
|
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_1__);
|
|
/* harmony import */ var _functions_backend_grabUserSchemaData__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(8164);
|
|
/* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(6517);
|
|
/* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(lodash__WEBPACK_IMPORTED_MODULE_3__);
|
|
/* harmony import */ var _functions_backend_setUserSchemaData__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(7638);
|
|
/* harmony import */ var _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(2224);
|
|
/* harmony import */ var _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_5___default = /*#__PURE__*/__webpack_require__.n(_package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_5__);
|
|
/* harmony import */ var _package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(5338);
|
|
/* harmony import */ var _package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_6___default = /*#__PURE__*/__webpack_require__.n(_package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_6__);
|
|
// @ts-check
|
|
/**
|
|
* ==============================================================================
|
|
* Imports
|
|
* ==============================================================================
|
|
*/ const fs = __webpack_require__(7147);
|
|
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_1___default()(req, res, true);
|
|
if (!user) {
|
|
console.log("No User");
|
|
return res.json({
|
|
success: false,
|
|
msg: "Unauthorized"
|
|
});
|
|
}
|
|
/**
|
|
* User auth
|
|
*
|
|
* @description Authenticate user
|
|
*/ const { payload } = req.body;
|
|
const dbFullName = req.body.dbFullName.replace(/[^a-z\_0-9]/g, "");
|
|
const dbSlug = req.body.dbSlug;
|
|
const dbId = req.body.dbId;
|
|
const parentDatabase = req.body.parentDatabase;
|
|
const parentTable = req.body.parentTable;
|
|
/**
|
|
* Send Response
|
|
*
|
|
* @description Send a boolean response
|
|
*/ try {
|
|
let userSchemaData = (0,_functions_backend_grabUserSchemaData__WEBPACK_IMPORTED_MODULE_2__["default"])({
|
|
userId: user.id
|
|
});
|
|
if (!userSchemaData) {
|
|
throw new Error("No User Schema found!");
|
|
}
|
|
let targetDatabaseIndex = userSchemaData.findIndex((db)=>db.dbFullName === dbFullName);
|
|
/** @type {import("@/package-shared/types").DSQL_DatabaseSchemaType | undefined} */ let targetDatabase = userSchemaData[targetDatabaseIndex];
|
|
if (!targetDatabase) {
|
|
throw new Error("No Target Database Found!");
|
|
}
|
|
let existingTableIndex = targetDatabase.tables.findIndex((table)=>table.tableName === payload.tableName);
|
|
if (existingTableIndex >= 0) {
|
|
userSchemaData[targetDatabaseIndex].tables[existingTableIndex] = payload;
|
|
} else {
|
|
userSchemaData[targetDatabaseIndex].tables.push(payload);
|
|
}
|
|
if (payload?.childTable) {
|
|
/** @type {import("@/package-shared/types").DSQL_DatabaseSchemaType | undefined} */ const parentDb = userSchemaData.find((db)=>db.dbFullName === parentDatabase);
|
|
const parentTableObject = parentDb?.tables.find((table)=>{
|
|
if (table.tableName === parentTable) {
|
|
return true;
|
|
}
|
|
});
|
|
if (!parentTableObject) {
|
|
throw new Error("Child Table Parent Doesn't Exist!");
|
|
}
|
|
if (parentTableObject.childrenTables?.[0]) {
|
|
parentTableObject.childrenTables.push({
|
|
dbNameFull: dbFullName,
|
|
tableName: payload.tableName,
|
|
tableNameFull: payload.tableNameFull
|
|
});
|
|
} else {
|
|
parentTableObject.childrenTables = [
|
|
{
|
|
dbNameFull: dbFullName,
|
|
tableName: payload.tableName,
|
|
tableNameFull: payload.tableFullName
|
|
},
|
|
];
|
|
}
|
|
}
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
/**
|
|
* Check Children Databases
|
|
*
|
|
* @description Check Children Databases
|
|
*/ if (targetDatabase.childrenDatabases) {
|
|
for(let i = 0; i < targetDatabase.childrenDatabases.length; i++){
|
|
const childDb = targetDatabase.childrenDatabases[i];
|
|
try {
|
|
const targetChildIndex = userSchemaData.findIndex((db)=>db.dbFullName === childDb.dbFullName);
|
|
if (userSchemaData[targetChildIndex]) userSchemaData[targetChildIndex].tables = lodash__WEBPACK_IMPORTED_MODULE_3___default().cloneDeep(targetDatabase.tables);
|
|
const targetDbRecord = await _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_5___default()(`SELECT * FROM user_databases WHERE user_id = ? AND db_full_name = ?`, [
|
|
user.id,
|
|
childDb.dbFullName
|
|
]);
|
|
const existingEntry = await _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_5___default()(`SELECT * FROM user_database_tables WHERE db_id=? AND user_id=? AND table_slug=?`, [
|
|
targetDbRecord[0].id,
|
|
user.id,
|
|
payload.tableName
|
|
]);
|
|
if (existingEntry?.[0]) continue;
|
|
const newChildTableEntry = await _package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_6___default()({
|
|
dbFullName: "datasquirel",
|
|
tableName: "user_database_tables",
|
|
data: {
|
|
user_id: user.id,
|
|
db_id: targetDbRecord[0].id,
|
|
db_slug: targetDbRecord[0].db_slug,
|
|
table_name: payload.tableFullName,
|
|
table_slug: payload.tableName
|
|
}
|
|
});
|
|
} catch (/** @type {any} */ error) {
|
|
_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_0___default()({
|
|
component: "/api/addUserTable/lines-124-140",
|
|
message: error.message,
|
|
user: user
|
|
});
|
|
}
|
|
}
|
|
}
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
// console.log("Done ...");
|
|
// fs.writeFileSync(
|
|
// "./.tmp/newTableSchema.json",
|
|
// JSON.stringify(userSchemaData, null, 4),
|
|
// "utf8"
|
|
// );
|
|
// return res.json({
|
|
// success: false,
|
|
// msg: "Testing",
|
|
// });
|
|
(0,_functions_backend_setUserSchemaData__WEBPACK_IMPORTED_MODULE_4__["default"])({
|
|
userId: user.id,
|
|
schemaData: userSchemaData
|
|
});
|
|
////////////////////////////////////////
|
|
/** @type {any} */ const newTableInsertObject = {
|
|
user_id: user.id,
|
|
db_id: dbId,
|
|
db_slug: dbSlug,
|
|
table_name: payload.tableFullName,
|
|
table_slug: payload.tableName
|
|
};
|
|
if (payload?.childTable) {
|
|
newTableInsertObject["child_table"] = "1";
|
|
newTableInsertObject["child_table_parent_database"] = parentDatabase;
|
|
newTableInsertObject["child_table_parent_table"] = parentTable;
|
|
}
|
|
const newTableEntry = await _package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_6___default()({
|
|
dbFullName: "datasquirel",
|
|
tableName: "user_database_tables",
|
|
data: newTableInsertObject
|
|
});
|
|
////////////////////////////////////////
|
|
const targetPath = path.join(process.cwd(), "/shell");
|
|
const dbShellUpdate = execSync(`node createDbFromSchema.js --user ${user.id} --database ${dbFullName}${payload?.childTable ? " --clone" : ""}`, {
|
|
cwd: targetPath
|
|
});
|
|
////////////////////////////////////////
|
|
res.json({
|
|
success: true
|
|
});
|
|
////////////////////////////////////////
|
|
} catch (/** @type {any} */ error1) {
|
|
////////////////////////////////////////
|
|
console.log(error1.message);
|
|
_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_0___default()({
|
|
component: "/api/addUserTable/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, [2224,2163,6825,3017,3403,7547,5886,5338,8164,7638], () => (__webpack_exec__(8583)));
|
|
module.exports = __webpack_exports__;
|
|
|
|
})(); |