372 lines
14 KiB
JavaScript
372 lines
14 KiB
JavaScript
|
"use strict";
|
||
|
(() => {
|
||
|
var exports = {};
|
||
|
exports.id = 2488;
|
||
|
exports.ids = [2488];
|
||
|
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");
|
||
|
|
||
|
/***/ }),
|
||
|
|
||
|
/***/ 2037:
|
||
|
/***/ ((module) => {
|
||
|
|
||
|
module.exports = require("os");
|
||
|
|
||
|
/***/ }),
|
||
|
|
||
|
/***/ 1017:
|
||
|
/***/ ((module) => {
|
||
|
|
||
|
module.exports = require("path");
|
||
|
|
||
|
/***/ }),
|
||
|
|
||
|
/***/ 6345:
|
||
|
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
|
||
|
|
||
|
// @ts-check
|
||
|
|
||
|
const varDatabaseDbHandler = __webpack_require__(1311);
|
||
|
const slugToCamelTitle = __webpack_require__(4885);
|
||
|
const { default: grabUserSchemaData } = __webpack_require__(8164);
|
||
|
const { default: setUserSchemaData } = __webpack_require__(7638);
|
||
|
const addDbEntry = __webpack_require__(5338);
|
||
|
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
|
||
|
* Grab Schema
|
||
|
*
|
||
|
* @description Grab Schema
|
||
|
*
|
||
|
* @param {{
|
||
|
* userId: number|string,
|
||
|
* database: import("@/package-shared/types").DSQL_MYSQL_user_databases_Type
|
||
|
* }} params
|
||
|
*/ module.exports = async function createDbSchemaFromDb({ userId , database }) {
|
||
|
try {
|
||
|
if (!userId) {
|
||
|
console.log("No user Id provided");
|
||
|
return;
|
||
|
}
|
||
|
/**
|
||
|
* Initialize
|
||
|
*
|
||
|
* @description Initialize
|
||
|
*/ const userSchemaData = grabUserSchemaData({
|
||
|
userId
|
||
|
});
|
||
|
if (!userSchemaData) throw new Error("User schema data not found!");
|
||
|
/** @type {{ tables: object[] }} */ const targetDb = userSchemaData.filter((dbObject)=>dbObject.dbFullName === database.db_full_name)[0];
|
||
|
////////////////////////////////////////////////
|
||
|
////////////////////////////////////////////////
|
||
|
////////////////////////////////////////////////
|
||
|
/**
|
||
|
* Add Tables
|
||
|
*
|
||
|
* @description Add Tables
|
||
|
*/ const existingTables = await varDatabaseDbHandler({
|
||
|
database: database.db_full_name,
|
||
|
queryString: `SHOW TABLES`
|
||
|
});
|
||
|
if (!existingTables) throw new Error("No Existing Tables");
|
||
|
for(let i = 0; i < existingTables.length; i++){
|
||
|
const table = existingTables[i];
|
||
|
const tableName = Object.values(table)[0];
|
||
|
const tableInsert = await addDbEntry({
|
||
|
dbFullName: "datasquirel",
|
||
|
tableName: "user_database_tables",
|
||
|
data: {
|
||
|
user_id: userId,
|
||
|
db_id: database.id,
|
||
|
db_slug: database.db_slug,
|
||
|
table_name: slugToCamelTitle(tableName),
|
||
|
table_slug: tableName
|
||
|
}
|
||
|
});
|
||
|
////////////////////////////////////////////////
|
||
|
/** @type {import("@/package-shared/types").DSQL_TableSchemaType} */ const tableObject = {
|
||
|
tableName: tableName,
|
||
|
tableFullName: slugToCamelTitle(tableName) || "",
|
||
|
fields: [],
|
||
|
indexes: []
|
||
|
};
|
||
|
////////////////////////////////////////////////
|
||
|
const tableColumns = await varDatabaseDbHandler({
|
||
|
database: database.db_full_name,
|
||
|
queryString: `SHOW COLUMNS FROM ${tableName}`
|
||
|
});
|
||
|
if (tableColumns) {
|
||
|
for(let k = 0; k < tableColumns.length; k++){
|
||
|
const tableColumn = tableColumns[k];
|
||
|
const { Field , Type , Null , Key , Default , Extra } = tableColumn;
|
||
|
/** @type {import("@/package-shared/types").DSQL_FieldSchemaType} */ const fieldObject = {
|
||
|
fieldName: Field,
|
||
|
dataType: Type.toUpperCase()
|
||
|
};
|
||
|
if (Null?.match(/^no$/i)) fieldObject.notNullValue = true;
|
||
|
if (Key?.match(/^pri$/i)) fieldObject.primaryKey = true;
|
||
|
if (Default?.toString()?.match(/./)) fieldObject.defaultValue = Default;
|
||
|
if (Default?.toString()?.match(/timestamp/i)) {
|
||
|
delete fieldObject.defaultValue;
|
||
|
fieldObject.defaultValueLiteral = Default;
|
||
|
}
|
||
|
if (Extra?.toString()?.match(/auto_increment/i)) fieldObject.autoIncrement = true;
|
||
|
tableObject.fields.push(fieldObject);
|
||
|
}
|
||
|
}
|
||
|
////////////////////////////////////////////////
|
||
|
////////////////////////////////////////////////
|
||
|
////////////////////////////////////////////////
|
||
|
/**
|
||
|
* Add Indexes
|
||
|
*
|
||
|
* @description Add Indexes
|
||
|
*/ const tableIndexes = await varDatabaseDbHandler({
|
||
|
database: database.db_full_name,
|
||
|
queryString: `SHOW INDEXES FROM ${tableName}`
|
||
|
});
|
||
|
if (tableIndexes) {
|
||
|
for(let m = 0; m < tableIndexes.length; m++){
|
||
|
const indexObject = tableIndexes[m];
|
||
|
const { Table , Key_name , Column_name , Null: Null1 , Index_type , Index_comment , } = indexObject;
|
||
|
////////////////////////////////////////////////
|
||
|
if (!Index_comment?.match(/^schema_index$/)) continue;
|
||
|
////////////////////////////////////////////////
|
||
|
/** @type {import("@/package-shared/types").DSQL_IndexSchemaType} */ const indexNewObject = {
|
||
|
indexType: Index_type?.match(/fulltext/i) ? "fullText" : "regular",
|
||
|
indexName: Key_name,
|
||
|
indexTableFields: []
|
||
|
};
|
||
|
const targetTableFieldObject = tableColumns?.filter((/** @type {any} */ col)=>col.Field === Column_name)[0];
|
||
|
const existingIndexField = tableObject.indexes?.filter((indx)=>indx.indexName == Key_name);
|
||
|
if (existingIndexField && existingIndexField[0]) {
|
||
|
existingIndexField[0].indexTableFields?.push({
|
||
|
value: Column_name,
|
||
|
dataType: targetTableFieldObject.Type.toUpperCase()
|
||
|
});
|
||
|
} else {
|
||
|
////////////////////////////////////////////////
|
||
|
indexNewObject.indexTableFields = [
|
||
|
{
|
||
|
value: Column_name,
|
||
|
dataType: targetTableFieldObject.Type.toUpperCase()
|
||
|
},
|
||
|
];
|
||
|
tableObject.indexes?.push(indexNewObject);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
////////////////////////////////////////////////
|
||
|
targetDb.tables.push(tableObject);
|
||
|
}
|
||
|
////////////////////////////////////////////////
|
||
|
////////////////////////////////////////////////
|
||
|
////////////////////////////////////////////////
|
||
|
/**
|
||
|
* Update User Specific Database
|
||
|
*
|
||
|
* @description Update User Specific Database
|
||
|
*/ setUserSchemaData({
|
||
|
schemaData: userSchemaData,
|
||
|
userId
|
||
|
});
|
||
|
return true;
|
||
|
////////////////////////////////////////////////
|
||
|
////////////////////////////////////////////////
|
||
|
////////////////////////////////////////////////
|
||
|
} catch (error) {
|
||
|
console.log(error);
|
||
|
return false;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
|
||
|
/***/ }),
|
||
|
|
||
|
/***/ 2369:
|
||
|
/***/ ((__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_createDbSchemaFromDb__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(6345);
|
||
|
/* harmony import */ var _functions_backend_createDbSchemaFromDb__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_createDbSchemaFromDb__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__);
|
||
|
// @ts-check
|
||
|
/**
|
||
|
* ==============================================================================
|
||
|
* Imports
|
||
|
* ==============================================================================
|
||
|
*/ const fs = __webpack_require__(7147);
|
||
|
const os = __webpack_require__(2037);
|
||
|
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 { database , fileBase64 } = sanitizedReqBody;
|
||
|
if (!database?.db_full_name?.match(/datasquirel_user_.*/)) {
|
||
|
res.json({
|
||
|
success: false,
|
||
|
msg: "Unauthorized"
|
||
|
});
|
||
|
}
|
||
|
/**
|
||
|
* Send Response
|
||
|
*
|
||
|
* @description Send a boolean response
|
||
|
*/ try {
|
||
|
/**
|
||
|
* Create new user folder and file
|
||
|
*
|
||
|
* @description Create new user folder and file
|
||
|
*/ const sqlName = `user-${user.id}-temp-sql`;
|
||
|
const mysqlPath = os.platform().match(/win/i) ? "'" + "C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysql.exe" + "'" : "mysql";
|
||
|
fs.writeFileSync(`./.tmp/${sqlName}.sql`, fileBase64, "base64");
|
||
|
/** @type {import("child_process").ExecOptions} */ let execSyncOptions = {
|
||
|
cwd: process.cwd()
|
||
|
};
|
||
|
if (os.platform().match(/win/i)) execSyncOptions.shell = "bash.exe";
|
||
|
const importToNewDb = execSync(`${mysqlPath} -u ${process.env.DSQL_DB_USERNAME} -p${process.env.DSQL_DB_PASSWORD} ${database.db_full_name} < .tmp/${sqlName}.sql`, execSyncOptions);
|
||
|
const updateSchema = await _functions_backend_createDbSchemaFromDb__WEBPACK_IMPORTED_MODULE_0___default()({
|
||
|
database: database,
|
||
|
userId: user.id
|
||
|
});
|
||
|
res.json({
|
||
|
success: updateSchema ? true : false
|
||
|
});
|
||
|
////////////////////////////////////////
|
||
|
} catch (/** @type {any} */ error) {
|
||
|
////////////////////////////////////////
|
||
|
console.log(error);
|
||
|
_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1___default()({
|
||
|
component: "/api/importDatabase/catch-error",
|
||
|
message: error.message,
|
||
|
user: user
|
||
|
});
|
||
|
res.json({
|
||
|
success: false,
|
||
|
msg: "Import Failed!"
|
||
|
});
|
||
|
////////////////////////////////////////
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/***/ }),
|
||
|
|
||
|
/***/ 4885:
|
||
|
/***/ ((module) => {
|
||
|
|
||
|
// @ts-check
|
||
|
|
||
|
module.exports = function slugToCamelTitle(/** @type {String} */ text) {
|
||
|
if (text) {
|
||
|
let addArray = text.split("-").filter((item)=>item !== "");
|
||
|
let camelArray = addArray.map((item)=>{
|
||
|
return item.substr(0, 1).toUpperCase() + item.substr(1).toLowerCase();
|
||
|
});
|
||
|
let parsedAddress = camelArray.join(" ");
|
||
|
return parsedAddress;
|
||
|
} else {
|
||
|
return null;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
|
||
|
/***/ })
|
||
|
|
||
|
};
|
||
|
;
|
||
|
|
||
|
// 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,5338,8326,1311,8164,7638], () => (__webpack_exec__(2369)));
|
||
|
module.exports = __webpack_exports__;
|
||
|
|
||
|
})();
|