2024-11-05 11:12:42 +00:00
"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
* === === === === === === === === === === === === === === === === === === === === === === === === === ===
* / c o n s t f s = _ _ w e b p a c k _ r e q u i r e _ _ ( 7 1 4 7 ) ;
const path = _ _webpack _require _ _ ( 1017 ) ;
const { execSync } = _ _webpack _require _ _ ( 2081 ) ;
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ / * *
* API handler
* === === === === === === === === === === === === === === === === === === === === === === === === === ===
* @ type { import ( "next" ) . NextApiHandler }
* / a s y n c f u n c t i o n h a n d l e r ( r e q , r e s ) {
/ * *
* Check method
*
* @ description Check request method and return if invalid
* / i f ( r e q . m e t h o d ! = = " P O S T " ) r e t u r n r e s . j s o n ( {
msg : "Failed!"
} ) ;
/ * *
* User auth
*
* @ description Authenticate user
* / c o n s t u s e r = a w a i t _ f u n c t i o n s _ b a c k e n d _ u s e r A u t h _ _ W E B P A C K _ I M P O R T E D _ M O D U L E _ 1 _ _ _ d e f a u l t ( ) ( r e q , r e s , t r u e ) ;
if ( ! user ) {
console . log ( "No User" ) ;
return res . json ( {
success : false ,
msg : "Unauthorized"
} ) ;
}
/ * *
* User auth
*
* @ description Authenticate user
* / c o n s t { p a y l o a d } = r e q . b o d y ;
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
* / t r y {
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
* / i f ( t a r g e t D a t a b a s e . c h i l d r e n D a t a b a s e s ) {
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 ) )
2024-11-05 14:18:40 +00:00
var _ _webpack _exports _ _ = _ _webpack _require _ _ . X ( 0 , [ 2224 , 2163 , 6825 , 3017 , 3403 , 7547 , 5886 , 5338 , 8164 , 7638 ] , ( ) => ( _ _webpack _exec _ _ ( 8583 ) ) ) ;
2024-11-05 11:12:42 +00:00
module . exports = _ _webpack _exports _ _ ;
} ) ( ) ;