dsql-admin/dsql-app/.local_dist/server/pages/api/createUserDatabase.js
Benjamin Toby 81cf010cb5 Updates
2025-01-14 16:27:08 +01:00

1 line
4.3 KiB
JavaScript

"use strict";(()=>{var e={};e.id=8893,e.ids=[8893],e.modules={39908:e=>{e.exports=require("@moduletrace/datasquirel/dist/users/user-auth")},75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},72583:e=>{e.exports=require("sanitize-html")},9973:e=>{e.exports=require("serverless-mysql")},79428:e=>{e.exports=require("buffer")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},33873:e=>{e.exports=require("path")},8325:(e,a,s)=>{s.r(a),s.d(a,{config:()=>D,default:()=>_,routeModule:()=>E});var r={};s.r(r),s.d(r,{default:()=>A});var t=s(89947),i=s(2706),l=s(96762),n=s(29021),d=s.n(n),u=s(48786),o=s(67619),c=s(69842),b=s(98e3),m=s(14160),p=s(76856);async function A(e,a){if("POST"!==e.method)return a.json({msg:"Failed!"});let s=await (0,c.A)(e,a,!0);if(!s)return a.json({success:!1,msg:"Unauthorized"});let r=await (0,u.A)("SELECT * FROM users WHERE id = ?",[s.id]),t=r?.[0],{db_name:i,db_slug:l,db_description:n,db_image:A,duplicate:_,keepUpdated:D,keepDataUpdated:E}=e.body;if(l.match(/[^a-zA-Z0-9-_]/))return a.json({success:!1,msg:"Input Error"});let S=`datasquirel_user_${s.id}_${l}`,f=await (0,m.A)(`CREATE DATABASE IF NOT EXISTS \`${S}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`);if(!f?.affectedRows)return a.json({success:!1,msg:"Database Could Not be created!"});let g={user_id:s.id,db_name:i,db_slug:l,db_full_name:S,db_description:n,db_image:A};_&&D&&(g.active_clone="1",g.active_clone_parent_db=_),_&&E&&(g.active_data="1");let N=await (0,b.A)({dbFullName:"datasquirel",tableName:"user_databases",data:g});if(N.insertId)try{let e=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${s.id}/main.json`,r=JSON.parse(d().readFileSync(e,"utf8")),u={dbName:i,dbSlug:l,dbFullName:S,dbDescription:n,dbImage:A,tables:[]};if(_)try{let e=r.filter(e=>e.dbFullName===_)[0];u.tables=e.tables,D&&(u.childDatabase=!0,u.childDatabaseDbFullName=_,E&&(u.updateData=!0),e.childrenDatabases?e.childrenDatabases.push({dbFullName:S}):e.childrenDatabases=[{dbFullName:S}]);for(let e=0;e<u.tables.length;e++){let a=u.tables[e];await (0,b.A)({dbFullName:"datasquirel",tableName:"user_database_tables",data:{user_id:s.id,db_id:N.insertId,db_slug:l,table_name:a.tableFullName,table_slug:a.tableName}})}}catch(e){(0,o.A)({component:"/api/createUserDatabase/lines-117-159",message:e.message,user:s})}r.push(u),d().writeFileSync(`${e}`,JSON.stringify(r),"utf8"),_&&await (0,p.A)({userId:s.id,targetDatabase:S});let c=await (0,m.A)(`GRANT ALL PRIVILEGES ON ${S}.* TO '${process.env.DSQL_DB_FULL_ACCESS_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS||"%"}' WITH GRANT OPTION`);if(c.error)throw Error(c.error);let f=await (0,m.A)(`GRANT SELECT ON ${S}.* TO '${process.env.DSQL_DB_READ_ONLY_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS||"%"}'`);if(f.error)throw Error(f.error);t?.mariadb_user&&t?.mariadb_pass&&await (0,m.A)(`GRANT ALL PRIVILEGES ON ${S}.* TO '${t.mariadb_user}'@'${t.mariadb_host}'`),await (0,m.A)("FLUSH PRIVILEGES"),a.json({success:!0})}catch(e){(0,o.A)({component:"/api/createUserDatabase/main-catch-error",message:e.message,user:s}),a.json({success:!1,msg:"Database Files could not be written!"})}else a.json({success:!1,msg:"Database Could Not Be Recorded!"})}let _=(0,l.M)(r,"default"),D=(0,l.M)(r,"config"),E=new t.PagesAPIRouteModule({definition:{kind:i.A.PAGES_API,page:"/api/createUserDatabase",pathname:"/api/createUserDatabase",bundlePath:"",filename:""},userland:r})},69842:(e,a,s)=>{s.d(a,{A:()=>i});var r=s(39908),t=s.n(r);async function i(e,a,s){let r=t()({req:e,level:s?"deep":void 0});return console.log(r),r.payload}},14160:(e,a,s)=>{s.d(a,{A:()=>n});var r=s(29021),t=s.n(r),i=s(67619),l=s(47546);async function n(e){let a;"production".match(/dev/)&&t().appendFileSync("./.tmp/sqlQuery.sql",e+"\n"+Date()+"\n\n\n","utf8");try{a=await (0,l.A)(e)}catch(e){(0,i.A)({component:"noDatabaseDbHandler",message:e.message}),console.log("ERROR in noDatabaseDbHandler =>",e.message)}return a||null}},47546:(e,a,s)=>{s.d(a,{A:()=>t});var r=s(7447);function t(...e){let a=(0,r.A)();try{return new Promise((s,r)=>{a.query(...e).then(e=>{a.end(),s(JSON.parse(JSON.stringify(e)))}).catch(e=>{a.end(),s({error:e.message,sql:e.sql})})})}catch(e){return{success:!1,error:e.message}}finally{a?.end()}}}};var a=require("../../webpack-api-runtime.js");a.C(e);var s=e=>a(a.s=e),r=a.X(0,[1251,9814],()=>s(8325));module.exports=r})();