2024-11-05 14:18:40 +00:00
|
|
|
"use strict";
|
|
|
|
exports.id = 4733;
|
|
|
|
exports.ids = [4733];
|
|
|
|
exports.modules = {
|
|
|
|
|
|
|
|
/***/ 8499:
|
|
|
|
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
|
|
|
|
|
|
|
|
/** # MODULE TRACE
|
|
|
|
======================================================================
|
|
|
|
* Detected 3 files that call this module. The files are listed below:
|
|
|
|
======================================================================
|
|
|
|
* `import` Statement Found in [get.js] => file:///d:\GitHub\datasquirel\pages\api\query\get.js
|
|
|
|
* `import` Statement Found in [post.js] => file:///d:\GitHub\datasquirel\pages\api\query\post.js
|
|
|
|
* `import` Statement Found in [add-user.js] => file:///d:\GitHub\datasquirel\pages\api\user\add-user.js
|
|
|
|
==== MODULE TRACE END ==== */ // @ts-check
|
|
|
|
|
|
|
|
const fs = __webpack_require__(7147);
|
2024-11-26 09:31:39 +00:00
|
|
|
const LOCAL_DB_HANDLER = __webpack_require__(3062);
|
2024-11-05 14:18:40 +00:00
|
|
|
const fullAccessDbHandler = __webpack_require__(8539);
|
|
|
|
const varReadOnlyDatabaseDbHandler = __webpack_require__(3118);
|
|
|
|
const serverError = __webpack_require__(3017);
|
|
|
|
const addDbEntry = __webpack_require__(5338);
|
|
|
|
const updateDbEntry = __webpack_require__(5886);
|
|
|
|
const deleteDbEntry = __webpack_require__(6147);
|
2024-11-06 12:06:51 +00:00
|
|
|
const parseDbResults = __webpack_require__(8326);
|
2024-11-26 09:31:39 +00:00
|
|
|
const trimSql = __webpack_require__(6888);
|
2024-11-05 14:18:40 +00:00
|
|
|
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
|
|
|
|
* Run DSQL users queries
|
|
|
|
* ==============================================================================
|
|
|
|
* @param {object} params - An object containing the function parameters.
|
|
|
|
* @param {string} params.dbFullName - Database full name. Eg. "datasquire_user_2_test"
|
2024-11-06 12:06:51 +00:00
|
|
|
* @param {string | any} params.query - Query string or object
|
2024-11-05 14:18:40 +00:00
|
|
|
* @param {boolean} [params.readOnly] - Is this operation read only?
|
2024-11-06 12:06:51 +00:00
|
|
|
* @param {boolean} [params.local] - Is this operation read only?
|
2024-11-05 14:18:40 +00:00
|
|
|
* @param {import("../../../types").DSQL_DatabaseSchemaType} [params.dbSchema] - Database schema
|
|
|
|
* @param {string[]} [params.queryValuesArray] - An optional array of query values if "?" is used in the query string
|
|
|
|
* @param {string} [params.tableName] - Table Name
|
|
|
|
*
|
|
|
|
* @return {Promise<any>}
|
2024-11-06 12:06:51 +00:00
|
|
|
*/ async function runQuery({ dbFullName , query , readOnly , dbSchema , queryValuesArray , tableName , local , }) {
|
2024-11-05 14:18:40 +00:00
|
|
|
/**
|
|
|
|
* Declare variables
|
|
|
|
*
|
|
|
|
* @description Declare "results" variable
|
|
|
|
*/ /** @type {any} */ let result;
|
|
|
|
/** @type {any} */ let error;
|
|
|
|
/** @type {import("../../../types").DSQL_TableSchemaType | undefined} */ let tableSchema;
|
|
|
|
if (dbSchema) {
|
|
|
|
try {
|
|
|
|
const table = tableName ? tableName : typeof query == "string" ? null : query ? query?.table : null;
|
|
|
|
if (!table) throw new Error("No table name provided");
|
|
|
|
tableSchema = dbSchema.tables.filter((tb)=>tb?.tableName === table)[0];
|
|
|
|
} catch (_err) {
|
|
|
|
// console.log("ERROR getting tableSchema: ", _err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Declare variables
|
|
|
|
*
|
|
|
|
* @description Declare "results" variable
|
|
|
|
*/ try {
|
|
|
|
if (typeof query === "string") {
|
2024-11-26 09:31:39 +00:00
|
|
|
const formattedQuery = trimSql(query);
|
|
|
|
/**
|
|
|
|
* Input Validation
|
|
|
|
*
|
|
|
|
* @description Input Validation
|
|
|
|
*/ if (readOnly && formattedQuery.match(/^alter|^delete|information_schema|databases|^create/i)) {
|
|
|
|
throw new Error("Wrong Input!");
|
|
|
|
}
|
2024-11-06 12:06:51 +00:00
|
|
|
if (local) {
|
2024-11-26 09:31:39 +00:00
|
|
|
const rawResults = await LOCAL_DB_HANDLER(formattedQuery, queryValuesArray);
|
2024-11-06 12:06:51 +00:00
|
|
|
result = tableSchema ? parseDbResults({
|
|
|
|
unparsedResults: rawResults,
|
|
|
|
tableSchema
|
|
|
|
}) : rawResults;
|
|
|
|
} else if (readOnly) {
|
2024-11-05 14:18:40 +00:00
|
|
|
result = await varReadOnlyDatabaseDbHandler({
|
2024-11-26 09:31:39 +00:00
|
|
|
queryString: formattedQuery,
|
2024-11-05 14:18:40 +00:00
|
|
|
queryValuesArray,
|
|
|
|
database: dbFullName,
|
|
|
|
tableSchema
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
result = await fullAccessDbHandler({
|
2024-11-26 09:31:39 +00:00
|
|
|
queryString: formattedQuery,
|
2024-11-05 14:18:40 +00:00
|
|
|
queryValuesArray,
|
|
|
|
database: dbFullName,
|
|
|
|
tableSchema
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else if (typeof query === "object") {
|
|
|
|
/**
|
|
|
|
* Declare variables
|
|
|
|
*
|
|
|
|
* @description Declare "results" variable
|
|
|
|
*/ const { data , action , table: table1 , identifierColumnName , identifierValue , update , duplicateColumnName , duplicateColumnValue , } = query;
|
|
|
|
switch(action.toLowerCase()){
|
|
|
|
case "insert":
|
|
|
|
result = await addDbEntry({
|
2024-11-06 12:06:51 +00:00
|
|
|
dbContext: local ? "Master" : "Dsql User",
|
2024-11-05 14:18:40 +00:00
|
|
|
paradigm: "Full Access",
|
|
|
|
dbFullName: dbFullName,
|
|
|
|
tableName: table1,
|
|
|
|
data: data,
|
|
|
|
update,
|
|
|
|
duplicateColumnName,
|
|
|
|
duplicateColumnValue,
|
|
|
|
tableSchema
|
|
|
|
});
|
|
|
|
if (!result?.insertId) {
|
|
|
|
error = new Error("Couldn't insert data");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "update":
|
|
|
|
result = await updateDbEntry({
|
2024-11-06 12:06:51 +00:00
|
|
|
dbContext: local ? "Master" : "Dsql User",
|
2024-11-05 14:18:40 +00:00
|
|
|
paradigm: "Full Access",
|
|
|
|
dbFullName: dbFullName,
|
|
|
|
tableName: table1,
|
|
|
|
data: data,
|
|
|
|
identifierColumnName,
|
|
|
|
identifierValue,
|
|
|
|
tableSchema
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "delete":
|
|
|
|
result = await deleteDbEntry({
|
2024-11-06 12:06:51 +00:00
|
|
|
dbContext: local ? "Master" : "Dsql User",
|
2024-11-05 14:18:40 +00:00
|
|
|
paradigm: "Full Access",
|
|
|
|
dbFullName: dbFullName,
|
|
|
|
tableName: table1,
|
|
|
|
identifierColumnName,
|
|
|
|
identifierValue,
|
|
|
|
tableSchema
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = null;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
} catch (/** @type {any} */ error1) {
|
|
|
|
serverError({
|
|
|
|
component: "functions/backend/runQuery",
|
|
|
|
message: error1.message
|
|
|
|
});
|
|
|
|
result = null;
|
|
|
|
error1 = error1.message;
|
|
|
|
}
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
return {
|
|
|
|
result,
|
|
|
|
error
|
|
|
|
};
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
}
|
|
|
|
module.exports = runQuery;
|
|
|
|
|
|
|
|
|
|
|
|
/***/ }),
|
|
|
|
|
|
|
|
/***/ 5425:
|
|
|
|
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
|
|
|
|
|
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const { scryptSync , createDecipheriv } = __webpack_require__(6113);
|
|
|
|
const { Buffer } = __webpack_require__(4300);
|
|
|
|
/**
|
|
|
|
* @param {string} encryptedString
|
|
|
|
* @returns {string | null}
|
|
|
|
*/ const decrypt = (encryptedString)=>{
|
|
|
|
const algorithm = "aes-192-cbc";
|
|
|
|
const password = process.env.DSQL_ENCRYPTION_PASSWORD || "";
|
|
|
|
const salt = process.env.DSQL_ENCRYPTION_SALT || "";
|
|
|
|
let key = scryptSync(password, salt, 24);
|
|
|
|
let iv = Buffer.alloc(16, 0);
|
|
|
|
// @ts-ignore
|
|
|
|
const decipher = createDecipheriv(algorithm, key, iv);
|
|
|
|
try {
|
|
|
|
let decrypted = decipher.update(encryptedString, "hex", "utf8");
|
|
|
|
decrypted += decipher.final("utf8");
|
|
|
|
return decrypted;
|
|
|
|
} catch (error) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
module.exports = decrypt;
|
|
|
|
|
|
|
|
|
|
|
|
/***/ }),
|
|
|
|
|
|
|
|
/***/ 8539:
|
|
|
|
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
|
|
|
|
|
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const DSQL_USER_DB_HANDLER = __webpack_require__(3403);
|
|
|
|
const parseDbResults = __webpack_require__(8326);
|
|
|
|
const serverError = __webpack_require__(3017);
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {object} param0
|
|
|
|
* @param {string} param0.queryString
|
|
|
|
* @param {string} param0.database
|
2024-11-06 12:06:51 +00:00
|
|
|
* @param {boolean} [param0.local]
|
2024-11-05 14:18:40 +00:00
|
|
|
* @param {import("../../types").DSQL_TableSchemaType | null} [param0.tableSchema]
|
|
|
|
* @param {string[]} [param0.queryValuesArray]
|
|
|
|
* @returns
|
2024-11-06 12:06:51 +00:00
|
|
|
*/ module.exports = async function fullAccessDbHandler({ queryString , database , tableSchema , queryValuesArray , local , }) {
|
2024-11-05 14:18:40 +00:00
|
|
|
/**
|
|
|
|
* Declare variables
|
|
|
|
*
|
|
|
|
* @description Declare "results" variable
|
|
|
|
*/ let results;
|
|
|
|
/**
|
|
|
|
* Fetch from db
|
|
|
|
*
|
|
|
|
* @description Fetch data from db if no cache
|
|
|
|
*/ try {
|
|
|
|
/** ********************* Run Query */ results = await DSQL_USER_DB_HANDLER({
|
|
|
|
paradigm: "Full Access",
|
|
|
|
database,
|
|
|
|
queryString,
|
|
|
|
queryValues: queryValuesArray
|
|
|
|
});
|
|
|
|
////////////////////////////////////////
|
|
|
|
} catch (/** @type {any} */ error) {
|
|
|
|
////////////////////////////////////////
|
|
|
|
serverError({
|
|
|
|
component: "fullAccessDbHandler",
|
|
|
|
message: error.message
|
|
|
|
});
|
|
|
|
/**
|
|
|
|
* Return error
|
|
|
|
*/ return error.message;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Return results
|
|
|
|
*
|
|
|
|
* @description Return results add to cache if "req" param is passed
|
|
|
|
*/ if (results && tableSchema) {
|
|
|
|
const unparsedResults = results;
|
|
|
|
const parsedResults = await parseDbResults({
|
|
|
|
unparsedResults: unparsedResults,
|
|
|
|
tableSchema: tableSchema
|
|
|
|
});
|
|
|
|
return parsedResults;
|
|
|
|
} else if (results) {
|
|
|
|
return results;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/***/ }),
|
|
|
|
|
|
|
|
/***/ 3118:
|
|
|
|
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
|
|
|
|
|
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const fs = __webpack_require__(7147);
|
|
|
|
const serverError = __webpack_require__(3017);
|
|
|
|
const parseDbResults = __webpack_require__(8326);
|
|
|
|
const DSQL_USER_DB_HANDLER = __webpack_require__(3403);
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {object} param0
|
|
|
|
* @param {string} param0.queryString
|
|
|
|
* @param {string} param0.database
|
|
|
|
* @param {string[]} [param0.queryValuesArray]
|
|
|
|
* @param {import("../../types").DSQL_TableSchemaType} [param0.tableSchema]
|
|
|
|
* @returns
|
|
|
|
*/ module.exports = async function varReadOnlyDatabaseDbHandler({ queryString , database , queryValuesArray , tableSchema , }) {
|
|
|
|
/**
|
|
|
|
* Declare variables
|
|
|
|
*
|
|
|
|
* @description Declare "results" variable
|
|
|
|
*/ let results;
|
|
|
|
/**
|
|
|
|
* Fetch from db
|
|
|
|
*
|
|
|
|
* @description Fetch data from db if no cache
|
|
|
|
*/ try {
|
|
|
|
results = await DSQL_USER_DB_HANDLER({
|
|
|
|
paradigm: "Read Only",
|
|
|
|
database,
|
|
|
|
queryString,
|
|
|
|
queryValues: queryValuesArray
|
|
|
|
});
|
|
|
|
////////////////////////////////////////
|
|
|
|
} catch (/** @type {any} */ error) {
|
|
|
|
////////////////////////////////////////
|
|
|
|
serverError({
|
|
|
|
component: "varReadOnlyDatabaseDbHandler",
|
|
|
|
message: error.message,
|
|
|
|
noMail: true
|
|
|
|
});
|
|
|
|
/**
|
|
|
|
* Return error
|
|
|
|
*/ return error.message;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Return results
|
|
|
|
*
|
|
|
|
* @description Return results add to cache if "req" param is passed
|
|
|
|
*/ if (results) {
|
|
|
|
const unparsedResults = results;
|
|
|
|
const parsedResults = await parseDbResults({
|
|
|
|
unparsedResults: unparsedResults,
|
|
|
|
tableSchema: tableSchema
|
|
|
|
});
|
|
|
|
return parsedResults;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2024-11-26 09:31:39 +00:00
|
|
|
/***/ }),
|
|
|
|
|
|
|
|
/***/ 3062:
|
|
|
|
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
|
|
|
|
|
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const mysql = __webpack_require__(2261);
|
|
|
|
const grabDbSSL = __webpack_require__(4824);
|
|
|
|
/**
|
|
|
|
* DSQL user read-only DB handler
|
|
|
|
* @param {object} params
|
|
|
|
* @param {string} params.paradigm
|
|
|
|
* @param {string} params.database
|
|
|
|
* @param {string} params.queryString
|
|
|
|
* @param {string[]} [params.queryValues]
|
|
|
|
*/ async function LOCAL_DB_HANDLER(/** @type {any[]} */ ...args) {
|
|
|
|
const MASTER = mysql({
|
|
|
|
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) : undefined,
|
|
|
|
charset: "utf8mb4",
|
|
|
|
ssl: grabDbSSL()
|
|
|
|
},
|
|
|
|
onConnect: ()=>{
|
|
|
|
console.log("Connection Successful!");
|
|
|
|
},
|
|
|
|
onConnectError: (/** @type {any} */ err)=>{
|
|
|
|
console.log("Connection Error", err.message);
|
|
|
|
},
|
|
|
|
onError: (/** @type {any} */ err)=>{
|
|
|
|
console.log("Client Error", err.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
console.log("Querying ...");
|
|
|
|
try {
|
|
|
|
const results = await MASTER.query(...args);
|
|
|
|
await MASTER.end();
|
|
|
|
return JSON.parse(JSON.stringify(results));
|
|
|
|
} catch (/** @type {any} */ error) {
|
|
|
|
console.log("DB Error =>", error.message);
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
error: error.message
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = LOCAL_DB_HANDLER;
|
|
|
|
|
|
|
|
|
|
|
|
/***/ }),
|
|
|
|
|
|
|
|
/***/ 6888:
|
|
|
|
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
|
|
|
|
|
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const https = __webpack_require__(5687);
|
|
|
|
const http = __webpack_require__(3685);
|
|
|
|
/**
|
|
|
|
* @typedef {object} GrabHostNamesReturn
|
|
|
|
* @property {string} host
|
|
|
|
* @property {number | string} port
|
|
|
|
* @property {typeof http | typeof https} scheme
|
|
|
|
*/ /**
|
|
|
|
* # Trim SQL
|
|
|
|
* @description Remove Returns and miltiple spaces from SQL Query
|
|
|
|
* @param {string} sql
|
|
|
|
* @returns {string}
|
|
|
|
*/ function trimSql(sql) {
|
|
|
|
return sql.replace(/\n|\r|\n\r|\r\n/gm, " ").replace(/ {2,}/g, " ").trim();
|
|
|
|
}
|
|
|
|
module.exports = trimSql;
|
|
|
|
|
|
|
|
|
2024-11-05 14:18:40 +00:00
|
|
|
/***/ })
|
|
|
|
|
|
|
|
};
|
|
|
|
;
|