2024-11-05 11:12:42 +00:00
"use strict" ;
( ( ) => {
var exports = { } ;
exports . id = 2873 ;
exports . ids = [ 2873 ] ;
exports . modules = {
/***/ 6517 :
/***/ ( ( module ) => {
module . exports = require ( "lodash" ) ;
/***/ } ) ,
/***/ 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" ) ;
/***/ } ) ,
/***/ 188 :
/***/ ( ( _ _unused _webpack _module , _ _webpack _exports _ _ , _ _webpack _require _ _ ) => {
// ESM COMPAT FLAG
_ _webpack _require _ _ . r ( _ _webpack _exports _ _ ) ;
// EXPORTS
_ _webpack _require _ _ . d ( _ _webpack _exports _ _ , {
"default" : ( ) => ( /* binding */ handler )
} ) ;
// EXTERNAL MODULE: ./package-shared/utils/backend/global-db/DB_HANDLER.js
var DB _HANDLER = _ _webpack _require _ _ ( 2224 ) ;
var DB _HANDLER _default = /*#__PURE__*/ _ _webpack _require _ _ . n ( DB _HANDLER ) ;
// EXTERNAL MODULE: ./functions/backend/grabDelegatedUserFromCookie.js
var grabDelegatedUserFromCookie = _ _webpack _require _ _ ( 1352 ) ;
var grabDelegatedUserFromCookie _default = /*#__PURE__*/ _ _webpack _require _ _ . n ( grabDelegatedUserFromCookie ) ;
// EXTERNAL MODULE: ./functions/backend/serverError.js
var serverError = _ _webpack _require _ _ ( 2163 ) ;
var serverError _default = /*#__PURE__*/ _ _webpack _require _ _ . n ( serverError ) ;
// EXTERNAL MODULE: ./functions/backend/userAuth.js
var userAuth = _ _webpack _require _ _ ( 6825 ) ;
var userAuth _default = /*#__PURE__*/ _ _webpack _require _ _ . n ( userAuth ) ;
// EXTERNAL MODULE: ./functions/backend/grabUserSchemaData.js
var grabUserSchemaData = _ _webpack _require _ _ ( 8164 ) ;
// EXTERNAL MODULE: external "fs"
var external _fs _ = _ _webpack _require _ _ ( 7147 ) ;
var external _fs _default = /*#__PURE__*/ _ _webpack _require _ _ . n ( external _fs _ ) ;
// EXTERNAL MODULE: external "lodash"
var external _lodash _ = _ _webpack _require _ _ ( 6517 ) ;
var external _lodash _default = /*#__PURE__*/ _ _webpack _require _ _ . n ( external _lodash _ ) ;
; // CONCATENATED MODULE: ./functions/backend/cleanUpSchemaData.js
// @ts-check
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ / * *
* @ typedef { object } updateSchemaDataReturn
* / / * *
* @ param { object } params
* @ param { DSQL _DatabaseSchemaType [ ] } params . dbSchemaData - Existing Schema
* @ param { string } params . dbFullName - Target Database full name
* @ param { DSQL _TableSchemaType } params . table - New Table Schema Data
* @ param { number } params . userId
*
* @ return { DSQL _DatabaseSchemaType [ ] | null }
* / f u n c t i o n c l e a n U p S c h e m a D a t a ( { d b S c h e m a D a t a , d b F u l l N a m e , t a b l e , u s e r I d , } ) {
try {
let newDbSchemaData = _ . cloneDeep ( dbSchemaData ) ;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/ * *
* Check For Expired Child Tables
*
* @ description Check For Expired Child Tables
* / n e w D b S c h e m a D a t a . f o r E a c h ( ( d b , i n d e x ) = > {
db . tables . forEach ( ( tableObject , _index ) => {
if ( tableObject . childrenTables ) {
tableObject . childrenTables . forEach ( ( childTable , _ _index ) => {
try {
const childDbTest = newDbSchemaData . filter ( ( _db ) => _db . dbFullName === childTable . dbNameFull ) ;
if ( ! childDbTest ? . [ 0 ] ) {
tableObject . childrenTables ? . splice ( _ _index , 1 ) ;
return ;
}
const childTableObjectTest = db . tables . filter ( ( _table ) => _table . tableName === childTable . tableName ) ;
if ( ! childTableObjectTest || ! childTableObjectTest [ 0 ] ) {
tableObject . childrenTables ? . splice ( _ _index , 1 ) ;
return ;
}
} catch ( /** @type {any} */ error ) { }
} ) ;
}
} ) ;
} ) ;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/ * *
* Start process
* === === === === === === === === === === === === === === === === === === ===
* @ description Analyze existing schema data and parse / update
* / c o n s t d b I n d e x = n e w D b S c h e m a D a t a . f i n d I n d e x ( ( d b ) = > d b . d b F u l l N a m e = = = d b F u l l N a m e ) ;
const targetDb = newDbSchemaData [ dbIndex ] ;
const targetTableIndex = targetDb . tables . findIndex ( ( _table ) => _table . tableName === table . tableName ) ;
const targetTable = targetDb . tables [ targetTableIndex ] ;
if ( targetTable . childrenTables ) {
targetTable . childrenTables . forEach ( ( childTable , index ) => {
try {
const childDbTest = newDbSchemaData . filter ( ( db ) => db . dbFullName === childTable . dbNameFull ) ;
if ( ! childDbTest || ! childDbTest [ 0 ] ) {
targetTable . childrenTables ? . splice ( index , 1 ) ;
return ;
}
const childDb = childDbTest [ 0 ] ;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
const childTableObjectTest = childDb . tables . filter ( ( tb ) => tb . tableName === childTable . tableName ) ;
if ( ! childTableObjectTest || ! childTableObjectTest [ 0 ] ) {
targetTable . childrenTables ? . splice ( index , 1 ) ;
return ;
}
const childTableObject = childTableObjectTest [ 0 ] ;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
const originChildTableFieldsClone = _ . cloneDeep ( targetTable . fields ) ;
// const originChildTableFields = [...targetTable.fields];
const originChildTableIndexesClone = _ . cloneDeep ( targetTable . indexes ) ;
// const originChildTableIndexes = [...targetTable.indexes];
childTableObject . fields = originChildTableFieldsClone ;
childTableObject . indexes = originChildTableIndexesClone ;
childTableObject . fields . forEach ( ( childFieldObject , index ) => {
const targetParentField = targetTable . fields [ index ] ;
if ( targetParentField ? . foreignKey ) {
const foreignKeyTable = childDb . tables . filter ( ( tbl ) => tbl . tableName === targetParentField . foreignKey ? . destinationTableName ) ;
if ( foreignKeyTable && foreignKeyTable [ 0 ] ) {
const foreignKeyField = foreignKeyTable [ 0 ] . fields . filter ( ( fld ) => fld . fieldName === targetParentField ? . foreignKey ? . destinationTableColumnName ) ;
if ( foreignKeyField && foreignKeyField [ 0 ] ) {
childFieldObject . foreignKey = {
... targetParentField . foreignKey
} ;
childFieldObject . foreignKey . foreignKeyName = childFieldObject . foreignKey . foreignKeyName ? . replace ( /dsql_\d+_[^_]+_/ , ` dsql_ ${ userId } _ ${ childDb . dbSlug } _ ` ) ;
childFieldObject . dataType = foreignKeyField [ 0 ] . dataType ;
} else {
delete childFieldObject . foreignKey ;
}
} else {
delete childFieldObject . foreignKey ;
}
} else {
delete childFieldObject . foreignKey ;
}
} ) ;
} catch ( /** @type {any} */ error ) {
targetTable . childrenTables ? . splice ( index , 1 ) ;
}
} ) ;
}
if ( targetDb . childrenDatabases ) {
targetDb . childrenDatabases . forEach ( ( childDb , index ) => {
try {
const { dbFullName } = childDb ;
const targetChildDb = newDbSchemaData . filter ( ( db ) => db . dbFullName === dbFullName ) [ 0 ] ;
if ( ! targetChildDb || ! targetChildDb . tables ) {
targetDb . childrenDatabases ? . splice ( index , 1 ) ;
return ;
}
targetChildDb . tables = targetDb . tables ;
} catch ( /** @type {any} */ error ) { }
} ) ;
}
return newDbSchemaData ;
} catch ( error ) {
return null ;
}
} ////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
; // CONCATENATED MODULE: ./functions/backend/updateSchemaData.js
// @ts-check
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ / * *
* @ typedef { object } updateSchemaDataReturn
* / / * *
* @ param { object } params
* @ param { import ( "@/package-shared/types" ) . DSQL _DatabaseSchemaType [ ] } params . dbSchemaData - Existing Schema
* @ param { import ( "@/package-shared/types" ) . DSQL _DatabaseSchemaType [ ] } params . schema - New Schema ( Partial )
* @ param { string } params . dbFullName - Target Database full name
* @ param { import ( "./grabDelegatedUserFromCookie" ) . grabDelegatedUserFromCookieReturn | null } params . delegatedUser
* @ param { import ( "@/package-shared/types" ) . DSQL _TableSchemaType } params . table - New Table Schema Data
* @ param { number } params . userId
*
* @ returns { boolean }
* / f u n c t i o n u p d a t e S c h e m a D a t a ( { d b S c h e m a D a t a , d e l e g a t e d U s e r , d b F u l l N a m e , s c h e m a , t a b l e , u s e r I d , } ) {
2024-11-05 14:18:40 +00:00
const userSchemaMainFilePath = ` ${ process . env . DSQL _USER _DB _SCHEMA _PATH } /user- ${ userId } /main.json ` ;
2024-11-05 11:12:42 +00:00
try {
if ( ! dbSchemaData ) throw new Error ( "Database schema not found!" ) ;
let newDbSchemaData = external _lodash _default ( ) . cloneDeep ( dbSchemaData ) ;
let targetDatabase = newDbSchemaData . find ( ( db ) => db . dbFullName == dbFullName ) ;
if ( ! targetDatabase ) throw new Error ( "Old Database schema not found!" ) ;
let targetDatabaseIndex = newDbSchemaData . findIndex ( ( db ) => db . dbFullName == dbFullName ) ;
if ( targetDatabaseIndex < 0 ) throw new Error ( "Target Database index not found!" ) ;
const newSchemaDatabase = schema . find ( ( db ) => db . dbFullName === dbFullName ) ;
if ( ! newSchemaDatabase ) throw new Error ( "New Database schema not found!" ) ;
if ( delegatedUser ? . delegated ) {
const createTablePriviledge = Boolean ( delegatedUser . priviledges ? . match ( /create.?table/i ) ) ;
const editTablePriviledge = Boolean ( delegatedUser . priviledges ? . match ( /edit.?table/i ) ) ;
if ( ! editTablePriviledge ) {
throw new Error ( "Unauthorized!" ) ;
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
}
const schemaTable = newSchemaDatabase . tables . find ( ( tbl ) => tbl . tableName == table . tableName ) ;
if ( ! schemaTable ) throw new Error ( "Table schema not found!" ) ;
const targetTableIndex = targetDatabase . tables . findIndex ( ( tbl ) => tbl . tableName == table . tableName ) ;
if ( schemaTable ) {
newDbSchemaData [ targetDatabaseIndex ] . tables [ targetTableIndex ] = schemaTable ;
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
const childrenDatabases = targetDatabase ? . childrenDatabases ;
childrenDatabases ? . forEach ( ( chDb ) => {
const chDbIndex = newDbSchemaData . findIndex ( ( db ) => db . dbFullName === chDb . dbFullName ) ;
if ( newDbSchemaData [ chDbIndex ] && targetDatabase ) {
newDbSchemaData [ chDbIndex ] . tables = targetDatabase . tables . map ( ( tbl ) => {
const newTbl = external _lodash _default ( ) . cloneDeep ( tbl ) ;
delete newTbl . childrenTables ;
return newTbl ;
} ) ;
}
} ) ;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
const childrenTables = newDbSchemaData ? . [ targetDatabaseIndex ] ? . tables ? . [ targetTableIndex ] ? . childrenTables ;
if ( childrenTables ) {
childrenTables . forEach ( ( childTbl ) => {
const childTblDbindex = newDbSchemaData . findIndex ( ( db ) => db . dbFullName == childTbl . dbNameFull ) ;
const childTblDbTblIndex = newDbSchemaData [ childTblDbindex ] ? . tables ? . findIndex ( ( tbl ) => tbl . tableName === childTbl . tableName ) ;
const childTblDbTbl = newDbSchemaData [ childTblDbindex ] ? . tables [ childTblDbTblIndex ] ;
if ( childTblDbTbl ) {
newDbSchemaData [ childTblDbindex ] . tables [ childTblDbTblIndex ] . fields = external _lodash _default ( ) . cloneDeep ( schemaTable . fields ) ;
newDbSchemaData [ childTblDbindex ] . tables [ childTblDbTblIndex ] . indexes = external _lodash _default ( ) . cloneDeep ( schemaTable . indexes ) ;
}
} ) ;
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
external _fs _default ( ) . writeFileSync ( ` ${ userSchemaMainFilePath } ` , JSON . stringify ( newDbSchemaData ) , "utf8" ) ;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
return true ;
} catch ( /** @type {any} */ error ) {
console . log ( "updateSchemaData.js ERROR: " , error . message ) ;
return false ;
}
} ////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
; // CONCATENATED MODULE: ./pages/api/updateDbSchemaDataForUser.js
// @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 u s e r A u t h _ 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"
} ) ;
}
/ * *
* User auth
*
* @ description Authenticate user
* / / * * @ type { import ( "@/package-shared/types" ) . DSQL _DatabaseSchemaType [ ] } * / c o n s t s c h e m a = r e q . b o d y . s c h e m a ;
const database = req . body . database ;
/** @type {import("@/package-shared/types").DSQL_TableSchemaType} */ const table = req . body . table ;
const delegated = req . body . delegated ;
/ * *
* Send Response
*
* @ description Send a boolean response
* / t r y {
/ * *
* Check if user has rights to this table
*
* @ description Check if user has rights to this table
* / c o n s t d e l e g a t e d U s e r = a w a i t g r a b D e l e g a t e d U s e r F r o m C o o k i e _ d e f a u l t ( ) ( {
request : req ,
user : user ,
databaseSlug : database ,
query : req . query
} ) ;
const userId = delegatedUser ? . rootUserId ? delegatedUser . rootUserId : user . id ;
const dbFullName = ` datasquirel_user_ ${ userId } _ ${ database } ` . replace ( /[^a-z\_0-9]/g , "" ) ;
/ * *
* Check if user has rights to this table
*
* @ description Check if user has rights to this table
* / l e t c o n f i r m ;
if ( delegatedUser ? . delegated ) {
const checkDelegatedUser = await DB _HANDLER _default ( ) ( ` SELECT * FROM user_users WHERE invited_user_id=? AND user_id=? AND user_priviledge LIKE '%Edit Tables%' ` , [
user . id ,
delegatedUser . rootUserId
] ) ;
if ( checkDelegatedUser && checkDelegatedUser [ 0 ] ) confirm = true ;
} else {
confirm = true ;
}
if ( ! confirm ) {
return res . json ( {
success : false ,
msg : "Unauthorized"
} ) ;
}
/ * *
* Create new user folder and file
*
* @ description Create new user folder and file
* / c o n s t d b S c h e m a D a t a = ( 0 , g r a b U s e r S c h e m a D a t a [ " d e f a u l t " ] ) ( {
userId : userId
} ) ;
if ( ! dbSchemaData ) throw new Error ( "Database schema not found!" ) ;
const updateDbSchema = updateSchemaData ( {
dbFullName ,
dbSchemaData ,
delegatedUser ,
schema ,
table ,
userId
} ) ;
if ( ! updateDbSchema ) {
throw new Error ( "Update Failed!" ) ;
}
////////////////////////////////////////
const targetPath = path . join ( process . cwd ( ) , "/shell" ) ;
const dbShellUpdate = execSync ( ` node createDbFromSchema.js --user ${ userId } --database ${ dbFullName } ` , {
cwd : targetPath
} ) ;
////////////////////////////////////////
res . json ( {
success : true
} ) ;
////////////////////////////////////////
} catch ( /** @type {any} */ error ) {
////////////////////////////////////////
console . log ( "updateDbSchemaDataForUser ERROR:" , error . message ) ;
serverError _default ( ) ( {
component : "/api/updateDbSchemaDataForUser/main-catch-error" ,
message : error . message ,
user : user
} ) ;
res . json ( {
success : false ,
msg : error . message
} ) ;
////////////////////////////////////////
}
}
/***/ } )
} ;
;
// 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 , 1352 , 8164 ] , ( ) => ( _ _webpack _exec _ _ ( 188 ) ) ) ;
2024-11-05 11:12:42 +00:00
module . exports = _ _webpack _exports _ _ ;
} ) ( ) ;