"use strict";(()=>{var e={};e.id=3133,e.ids=[3133],e.modules={60818:e=>{e.exports=require("dotenv")},75600:e=>{e.exports=require("next/dist/compiled/next-server/pages-api.runtime.prod.js")},9973:e=>{e.exports=require("serverless-mysql")},79428:e=>{e.exports=require("buffer")},79646:e=>{e.exports=require("child_process")},55511:e=>{e.exports=require("crypto")},29021:e=>{e.exports=require("fs")},81630:e=>{e.exports=require("http")},33873:e=>{e.exports=require("path")},96762:(e,r)=>{Object.defineProperty(r,"M",{enumerable:!0,get:function(){return function e(r,t){return t in r?r[t]:"then"in r&&"function"==typeof r.then?r.then(r=>e(r,t)):"function"==typeof r&&"default"===t?r:void 0}}})},47095:(e,r,t)=>{t.r(r),t.d(r,{config:()=>g,default:()=>m,routeModule:()=>D});var s={};t.r(s),t.d(s,{default:()=>b});var a=t(89947),o=t(2706),n=t(96762),i=t(39831),l=t.n(i),u=t(48225),d=t.n(u),c=t(6420),_=t.n(c),p=t(19261),S=t.n(p);let f=t(29021);t(33873);let{execSync:E}=t(79646);async function b(e,r){if("POST"!==e.method)return r.json({msg:"Failed!"});let t=await _()(e,r,!0);if(!t)return r.json({success:!1,msg:"Unauthorized"});let{tableData:s}=e.body,a=e.body.database,o=e.body.dbSlug;e.body.delegated;let n=a.replace(/[^a-z\_0-9]/g,"");try{let i=await l()({request:e,user:t,databaseSlug:o,query:e.query}),u=i?.rootUserId?i.rootUserId:t.id,d=`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${u}/main.json`,c=JSON.parse(f.readFileSync(d,"utf-8")),_=c.findIndex(e=>e.dbFullName===a),p=c[_].tables.findIndex(e=>e.childTable&&e.childTableDbFullName===s.childTableDbFullName&&e.childTableName===s.childTableName&&e.tableName===s.tableName);c[_].tables[p]=s,f.writeFileSync(`${d}`,JSON.stringify(c),"utf8"),await S()({userId:u,targetDatabase:n}),r.json({success:!0})}catch(e){console.log(e.message),d()({component:"/api/addApiKey/catch-error",message:e.message,user:t}),r.json({success:!1,msg:"Foreign Key update Failed."})}}let m=(0,n.M)(s,"default"),g=(0,n.M)(s,"config"),D=new a.PagesAPIRouteModule({definition:{kind:o.A.PAGES_API,page:"/api/updateClonedTableForeignKeys",pathname:"/api/updateClonedTableForeignKeys",bundlePath:"",filename:""},userland:s})},39831:(e,r,t)=>{t(81630);let s=t(72835);e.exports=async function({request:e,databaseSlug:r,user:t,query:a}){try{let o=t.id,n=null;if(!a?.delegated)return{dbUserId:o};let i=a.dbUserId,l=`${process.env.DSQL_USER_DB_PREFIX}${i}_${r}`,u=`${process.env.DSQL_USER_DELEGATED_DB_COOKIE_PREFIX}${l}`;try{if(!e.cookies?.[u])throw Error("Cookie not present");let t=s({encryptedString:e.cookies[u]});if(!t)throw Error("Invalid Token");if((n=JSON.parse(t)).databaseSlug===r)return{dbUserId:o=n.rootUserId,rootUserId:n.rootUserId,rootUserName:n.rootUserName,rootUserEmail:n.rootUserEmail,rootUserImage:n.rootUserImage,databaseFullName:n.databaseFullName,databaseSlug:n.databaseSlug,allowedTables:n.allowedTables,priviledges:n.priviledges,database:n.databaseSlug,delegated:!0}}catch(e){}return{dbUserId:o}}catch(e){return null}}},6420:(e,r,t)=>{t(81630);let s=t(51348),a=t(72835),o=t(29021),n=t(31341);async function i(e,r,t,i){let{keyCookieName:l,csrfCookieName:u}=n();if(!e.cookies?.[l]?.match(/./))return null;let d=a({encryptedString:e.cookies[l]});if(!d)return null;let c=JSON.parse(d);if(!c.csrf_k||t&&!e.headers["x-csrf-auth"]?.match(RegExp(`${c.csrf_k}`)))return null;let _=process.env.DSQL_USER_LOGIN_KEYS_PATH;if(!_)return console.log("DSQL_USER_LOGIN_KEYS_PATH env variable not found. Please set this variable."),null;if(t&&!o.existsSync(`${_}/${c.csrf_k}`))return null;if(0==c.verification_status&&!t){let e=await s(`SELECT verification_status FROM users WHERE id='${c.id}'`);e&&e[0]&&1==e[0].verification_status&&r.setHeader("Set-Cookie",["user_refresh=1"])}return c?.date&&Date.now()-c.date>6048e5?null:c}e.exports=i},31341:e=>{e.exports=function(e){let r=process.env.DSQL_COOKIES_PREFIX||"dsql_",t=process.env.DSQL_COOKIES_KEY_NAME||"key",s=process.env.DSQL_COOKIES_CSRF_NAME||"csrf",a=process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME||"one-time-code",o=e?.database?.replace(/^datasquirel_user_\d+_/,"")||process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/,""),n=r;e?.userId&&(n+=`user_${e.userId}_`),o&&(n+=`${o}_`),n+=t;let i=r;e?.userId&&(i+=`user_${e.userId}_`),o&&(i+=`${o}_`),i+=s;let l=r;return e?.userId&&(l+=`user_${e.userId}_`),o&&(l+=`${o}_`),{keyCookieName:n,csrfCookieName:i,oneTimeCodeName:l+=a}}},51348:(e,r,t)=>{let s=t(9973),a=t(21580),o=s({config:{host:process.env.DSQL_DB_HOST,user:process.env.DSQL_DB_USERNAME,password:process.env.DSQL_DB_PASSWORD,database:process.env.DSQL_DB_NAME,port:process.env.DSQL_DB_PORT?Number(process.env.DSQL_DB_PORT):void 0,charset:"utf8mb4",ssl:a()}});async function n(...e){try{let r=await o.query(...e);return await o.end(),JSON.parse(JSON.stringify(r))}catch(e){return console.log("DB Error =>",e),{success:!1,error:e.message}}}e.exports=n},21580:(e,r,t)=>{let s=t(29021);e.exports=function(){let e=process.env.DSQL_SSL_DIR;if(!e?.match(/./))return;let r=`${e}/ca-cert.pem`;if(!s.existsSync(r)){console.log(`${r} does not exist`);return}return{ca:s.readFileSync(`${e}/ca-cert.pem`)}}},2706:(e,r)=>{var t;Object.defineProperty(r,"A",{enumerable:!0,get:function(){return t}}),function(e){e.PAGES="PAGES",e.PAGES_API="PAGES_API",e.APP_PAGE="APP_PAGE",e.APP_ROUTE="APP_ROUTE",e.IMAGE="IMAGE"}(t||(t={}))},89947:(e,r,t)=>{e.exports=t(75600)}};var r=require("../../webpack-api-runtime.js");r.C(e);var t=e=>r(r.s=e),s=r.X(0,[2451],()=>t(47095));module.exports=s})();