dsql-admin/dsql-app/package-shared/functions/backend/db/runQuery.js

206 lines
6.7 KiB
JavaScript
Raw Normal View History

2024-11-05 11:12:42 +00:00
// @ts-check
const fs = require("fs");
2024-11-26 09:31:39 +00:00
const LOCAL_DB_HANDLER = require("../../../utils/backend/global-db/LOCAL_DB_HANDLER");
2024-11-05 11:12:42 +00:00
const fullAccessDbHandler = require("../fullAccessDbHandler");
const varReadOnlyDatabaseDbHandler = require("../varReadOnlyDatabaseDbHandler");
const serverError = require("../serverError");
const addDbEntry = require("./addDbEntry");
const updateDbEntry = require("./updateDbEntry");
const deleteDbEntry = require("./deleteDbEntry");
2024-11-06 12:06:51 +00:00
const parseDbResults = require("../parseDbResults");
2024-11-26 09:31:39 +00:00
const trimSql = require("../../../utils/trim-sql");
2024-11-05 11:12:42 +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 11:12:42 +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 11:12:42 +00:00
* @param {import("../../../types").DSQL_DatabaseSchemaType} [params.dbSchema] - Database schema
2024-12-06 13:24:26 +00:00
* @param {(string | number)[]} [params.queryValuesArray] - An optional array of query values if "?" is used in the query string
2024-11-05 11:12:42 +00:00
* @param {string} [params.tableName] - Table Name
*
* @return {Promise<any>}
*/
async function runQuery({
dbFullName,
query,
readOnly,
dbSchema,
queryValuesArray,
tableName,
2024-11-06 12:06:51 +00:00
local,
2024-11-05 11:12:42 +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(
2024-12-09 12:27:08 +00:00
/^alter|^delete|information_schema|^create/i
2024-11-26 09:31:39 +00:00
)
) {
throw new Error("Wrong Input!");
}
2024-12-09 12:27:08 +00:00
if (readOnly) {
2024-11-05 11:12:42 +00:00
result = await varReadOnlyDatabaseDbHandler({
2024-11-26 09:31:39 +00:00
queryString: formattedQuery,
2024-12-06 13:24:26 +00:00
queryValuesArray: queryValuesArray?.map((vl) => String(vl)),
2024-11-05 11:12:42 +00:00
database: dbFullName,
tableSchema,
2024-12-09 12:27:08 +00:00
useLocal: local,
2024-11-05 11:12:42 +00:00
});
} else {
result = await fullAccessDbHandler({
2024-11-26 09:31:39 +00:00
queryString: formattedQuery,
2024-12-06 13:24:26 +00:00
queryValuesArray: queryValuesArray?.map((vl) => String(vl)),
2024-11-05 11:12:42 +00:00
database: dbFullName,
tableSchema,
2024-12-09 12:27:08 +00:00
local,
2024-11-05 11:12:42 +00:00
});
}
} else if (typeof query === "object") {
/**
* Declare variables
*
* @description Declare "results" variable
*/
const {
data,
action,
table,
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 11:12:42 +00:00
paradigm: "Full Access",
dbFullName: dbFullName,
tableName: table,
data: data,
update,
duplicateColumnName,
duplicateColumnValue,
tableSchema,
2024-12-09 12:27:08 +00:00
useLocal: local,
2024-11-05 11:12:42 +00:00
});
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 11:12:42 +00:00
paradigm: "Full Access",
dbFullName: dbFullName,
tableName: table,
data: data,
identifierColumnName,
identifierValue,
tableSchema,
2024-12-09 12:27:08 +00:00
useLocal: local,
2024-11-05 11:12:42 +00:00
});
break;
case "delete":
result = await deleteDbEntry({
2024-11-06 12:06:51 +00:00
dbContext: local ? "Master" : "Dsql User",
2024-11-05 11:12:42 +00:00
paradigm: "Full Access",
dbFullName: dbFullName,
tableName: table,
identifierColumnName,
identifierValue,
tableSchema,
2024-12-09 12:27:08 +00:00
useLocal: local,
2024-11-05 11:12:42 +00:00
});
break;
default:
result = null;
break;
}
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} catch (/** @type {any} */ error) {
serverError({
component: "functions/backend/runQuery",
message: error.message,
});
result = null;
error = error.message;
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
return { result, error };
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
}
module.exports = runQuery;