2024-11-05 11:12:42 +00:00
"use strict" ;
( ( ) => {
var exports = { } ;
exports . id = 2316 ;
exports . ids = [ 2316 ] ;
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" ) ;
/***/ } ) ,
/***/ 4577 :
/***/ ( ( _ _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 _utils _backend _global _db _DB _HANDLER _ _WEBPACK _IMPORTED _MODULE _0 _ _ = _ _webpack _require _ _ ( 2224 ) ;
/* harmony import */ var _package _shared _utils _backend _global _db _DB _HANDLER _ _WEBPACK _IMPORTED _MODULE _0 _ _ _default = /*#__PURE__*/ _ _webpack _require _ _ . n ( _package _shared _utils _backend _global _db _DB _HANDLER _ _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 _db _addDbEntry _ _WEBPACK _IMPORTED _MODULE _3 _ _ = _ _webpack _require _ _ ( 5338 ) ;
/* harmony import */ var _package _shared _functions _backend _db _addDbEntry _ _WEBPACK _IMPORTED _MODULE _3 _ _ _default = /*#__PURE__*/ _ _webpack _require _ _ . n ( _package _shared _functions _backend _db _addDbEntry _ _WEBPACK _IMPORTED _MODULE _3 _ _ ) ;
/* harmony import */ var _package _shared _functions _backend _noDatabaseDbHandler _ _WEBPACK _IMPORTED _MODULE _4 _ _ = _ _webpack _require _ _ ( 9132 ) ;
/* harmony import */ var _package _shared _functions _backend _noDatabaseDbHandler _ _WEBPACK _IMPORTED _MODULE _4 _ _ _default = /*#__PURE__*/ _ _webpack _require _ _ . n ( _package _shared _functions _backend _noDatabaseDbHandler _ _WEBPACK _IMPORTED _MODULE _4 _ _ ) ;
// @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 _ 2 _ _ _ d e f a u l t ( ) ( r e q , r e s , t r u e ) ;
if ( ! user ) {
return res . json ( {
success : false ,
msg : "Unauthorized"
} ) ;
}
const fullUserArray = await _package _shared _utils _backend _global _db _DB _HANDLER _ _WEBPACK _IMPORTED _MODULE _0 _ _ _default ( ) ( ` SELECT * FROM users WHERE id = ? ` , [
user . id ,
] ) ;
const fullUser = fullUserArray ? . [ 0 ] ;
/ * *
* User auth
*
* @ description Authenticate user
* / c o n s t s a n i t i z e d R e q B o d y = r e q . b o d y ;
const { db _name , db _slug , db _description , db _image , duplicate , keepUpdated , keepDataUpdated , } = sanitizedReqBody ;
if ( db _slug . match ( /[^a-zA-Z0-9-_]/ ) ) {
return res . json ( {
success : false ,
msg : "Input Error"
} ) ;
}
const db _full _name = ` datasquirel_user_ ${ user . id } _ ${ db _slug } ` ;
const createDatabaseForUser = await _package _shared _functions _backend _noDatabaseDbHandler _ _WEBPACK _IMPORTED _MODULE _4 _ _ _default ( ) ( ` CREATE DATABASE IF NOT EXISTS \` ${ db _full _name } \` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin ` ) ;
/ * *
* User auth
*
* @ description Authenticate user
* / i f ( ! c r e a t e D a t a b a s e F o r U s e r ? . a f f e c t e d R o w s ) {
return res . json ( {
success : false ,
msg : "Database Could Not be created!"
} ) ;
}
/** @type {any} */ let newDbData = {
user _id : user . id ,
db _name ,
db _slug ,
db _full _name ,
db _description ,
db _image
} ;
if ( duplicate && keepUpdated ) {
newDbData [ "active_clone" ] = "1" ;
newDbData [ "active_clone_parent_db" ] = duplicate ;
}
if ( duplicate && keepDataUpdated ) {
newDbData [ "active_data" ] = "1" ;
}
const newDatabaseEntry = await _package _shared _functions _backend _db _addDbEntry _ _WEBPACK _IMPORTED _MODULE _3 _ _ _default ( ) ( {
dbFullName : "datasquirel" ,
tableName : "user_databases" ,
data : newDbData
} ) ;
/ * *
* Send Response
*
* @ description Send a boolean response
* / i f ( n e w D a t a b a s e E n t r y . i n s e r t I d ) {
try {
/ * *
* Create new user folder and file
*
* @ description Create new user folder and file
2024-11-05 14:18:40 +00:00
* / c o n s t u s e r S c h e m a M a i n F i l e P a t h = ` $ { p r o c e s s . e n v . D S Q L _ U S E R _ D B _ S C H E M A _ P A T H } / u s e r - $ { u s e r . i d } / m a i n . j s o n ` ;
2024-11-05 11:12:42 +00:00
/** @type {import("@/package-shared/types").DSQL_DatabaseSchemaType[]} */ let userSchemaData = JSON . parse ( fs . readFileSync ( userSchemaMainFilePath , "utf8" ) ) ;
/** @type {any} */ let newDbSchemaObject = {
dbName : db _name ,
dbSlug : db _slug ,
dbFullName : db _full _name ,
dbDescription : db _description ,
dbImage : db _image ,
tables : [ ]
} ;
if ( duplicate ) {
try {
const parentDatabase = userSchemaData . filter ( ( db ) => db . dbFullName === duplicate ) [ 0 ] ;
newDbSchemaObject . tables = parentDatabase . tables ;
if ( keepUpdated ) {
newDbSchemaObject [ "childDatabase" ] = true ;
newDbSchemaObject [ "childDatabaseDbFullName" ] = duplicate ;
if ( keepDataUpdated ) newDbSchemaObject [ "updateData" ] = true ;
if ( parentDatabase . childrenDatabases ) {
parentDatabase . childrenDatabases . push ( {
dbFullName : db _full _name
} ) ;
} else {
parentDatabase . childrenDatabases = [
{
dbFullName : db _full _name
} ,
] ;
}
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
for ( let i = 0 ; i < newDbSchemaObject . tables . length ; i ++ ) {
const newDbTable = newDbSchemaObject . tables [ i ] ;
const newDbTableRecord = await _package _shared _functions _backend _db _addDbEntry _ _WEBPACK _IMPORTED _MODULE _3 _ _ _default ( ) ( {
dbFullName : "datasquirel" ,
tableName : "user_database_tables" ,
data : {
user _id : user . id ,
db _id : newDatabaseEntry . insertId ,
db _slug : db _slug ,
table _name : newDbTable . tableFullName ,
table _slug : newDbTable . tableName
}
} ) ;
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} catch ( /** @type {any} */ error ) {
_functions _backend _serverError _ _WEBPACK _IMPORTED _MODULE _1 _ _ _default ( ) ( {
component : "/api/createUserDatabase/lines-117-159" ,
message : error . message ,
user : user
} ) ;
}
}
userSchemaData . push ( newDbSchemaObject ) ;
fs . writeFileSync ( ` ${ userSchemaMainFilePath } ` , JSON . stringify ( userSchemaData ) , "utf8" ) ;
////////////////////////////////////////
if ( duplicate ) {
const targetPath = path . join ( process . cwd ( ) , "/shell" ) ;
const dbShellUpdate = execSync ( ` node createDbFromSchema.js --user ${ user . id } --database ${ db _full _name } ` , {
cwd : targetPath
} ) ;
}
////////////////////////////////////////
const grantDbPriviledges = await _package _shared _functions _backend _noDatabaseDbHandler _ _WEBPACK _IMPORTED _MODULE _4 _ _ _default ( ) ( ` GRANT ALL PRIVILEGES ON ${ db _full _name } .* TO ' ${ process . env . DSQL _DB _FULL _ACCESS _USERNAME } '@' ${ process . env . DSQL _DB _TARGET _IP _ADDRESS || "%" } ' WITH GRANT OPTION ` ) ;
if ( grantDbPriviledges . error ) {
throw new Error ( grantDbPriviledges . error ) ;
}
const grantRead = await _package _shared _functions _backend _noDatabaseDbHandler _ _WEBPACK _IMPORTED _MODULE _4 _ _ _default ( ) ( ` GRANT SELECT ON ${ db _full _name } .* TO ' ${ process . env . DSQL _DB _READ _ONLY _USERNAME } '@' ${ process . env . DSQL _DB _TARGET _IP _ADDRESS || "%" } ' ` ) ;
if ( grantRead . error ) {
throw new Error ( grantRead . error ) ;
}
if ( fullUser ? . mariadb _user && fullUser ? . mariadb _pass ) {
const grantDbPriviledgesToSqlUser = await _package _shared _functions _backend _noDatabaseDbHandler _ _WEBPACK _IMPORTED _MODULE _4 _ _ _default ( ) ( ` GRANT ALL PRIVILEGES ON ${ db _full _name } .* TO ' ${ fullUser . mariadb _user } '@' ${ fullUser . mariadb _host } ' ` ) ;
}
await _package _shared _functions _backend _noDatabaseDbHandler _ _WEBPACK _IMPORTED _MODULE _4 _ _ _default ( ) ( ` FLUSH PRIVILEGES ` ) ;
////////////////////////////////////////
res . json ( {
success : true
} ) ;
////////////////////////////////////////
} catch ( /** @type {any} */ error1 ) {
////////////////////////////////////////
_functions _backend _serverError _ _WEBPACK _IMPORTED _MODULE _1 _ _ _default ( ) ( {
component : "/api/createUserDatabase/main-catch-error" ,
message : error1 . message ,
user : user
} ) ;
res . json ( {
success : false ,
msg : "Database Files could not be written!"
} ) ;
////////////////////////////////////////
}
} else {
res . json ( {
success : false ,
msg : "Database Could Not Be Recorded!"
} ) ;
}
}
/***/ } )
} ;
;
// 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 , 7487 , 9132 ] , ( ) => ( _ _webpack _exec _ _ ( 4577 ) ) ) ;
2024-11-05 11:12:42 +00:00
module . exports = _ _webpack _exports _ _ ;
} ) ( ) ;