datasquirel/package-shared/functions/backend/db/runQuery.ts

183 lines
5.5 KiB
TypeScript
Raw Normal View History

2025-01-10 19:10:28 +00:00
import fullAccessDbHandler from "../fullAccessDbHandler";
import varReadOnlyDatabaseDbHandler from "../varReadOnlyDatabaseDbHandler";
import serverError from "../serverError";
import addDbEntry from "./addDbEntry";
import updateDbEntry from "./updateDbEntry";
import deleteDbEntry from "./deleteDbEntry";
import trimSql from "../../../utils/trim-sql";
import { DSQL_TableSchemaType } from "../../../types";
type Param = {
dbFullName: string;
query: string | any;
readOnly?: boolean;
local?: boolean;
dbSchema?: import("../../../types").DSQL_DatabaseSchemaType;
queryValuesArray?: (string | number)[];
tableName?: string;
};
2023-09-21 14:00:04 +00:00
/**
2025-01-10 19:10:28 +00:00
* # Run DSQL users queries
2023-09-21 14:00:04 +00:00
*/
2025-01-10 19:10:28 +00:00
export default async function runQuery({
2024-10-14 06:49:01 +00:00
dbFullName,
query,
readOnly,
dbSchema,
queryValuesArray,
tableName,
2024-11-06 06:26:23 +00:00
local,
2025-01-10 19:10:28 +00:00
}: Param): Promise<any> {
2023-09-21 14:00:04 +00:00
/**
* Declare variables
*
* @description Declare "results" variable
*/
2025-01-10 19:10:28 +00:00
let result: any;
let error: any;
let tableSchema: DSQL_TableSchemaType | undefined;
2023-09-21 14:00:04 +00:00
if (dbSchema) {
try {
2024-10-14 06:49:01 +00:00
const table = tableName
? tableName
: typeof query == "string"
? null
: query
? query?.table
: null;
2023-09-21 14:00:04 +00:00
if (!table) throw new Error("No table name provided");
2024-10-14 06:49:01 +00:00
tableSchema = dbSchema.tables.filter(
(tb) => tb?.tableName === table
)[0];
2024-11-06 06:26:23 +00:00
} catch (_err) {
// console.log("ERROR getting tableSchema: ", _err.message);
}
2023-09-21 14:00:04 +00:00
}
/**
* Declare variables
*
* @description Declare "results" variable
*/
try {
if (typeof query === "string") {
2024-11-18 17:14:15 +00:00
const formattedQuery = trimSql(query);
2024-11-18 09:47:14 +00:00
2024-11-18 09:59:12 +00:00
/**
* Input Validation
*
* @description Input Validation
*/
if (
readOnly &&
2024-11-18 17:14:15 +00:00
formattedQuery.match(
2024-12-09 11:45:39 +00:00
/^alter|^delete|information_schema|^create/i
2024-11-18 17:14:15 +00:00
)
2024-11-18 09:59:12 +00:00
) {
throw new Error("Wrong Input!");
}
2024-12-09 11:45:39 +00:00
if (readOnly) {
2024-11-06 06:26:23 +00:00
result = await varReadOnlyDatabaseDbHandler({
2024-11-18 09:47:14 +00:00
queryString: formattedQuery,
2024-12-06 10:31:24 +00:00
queryValuesArray: queryValuesArray?.map((vl) => String(vl)),
2024-11-06 06:26:23 +00:00
database: dbFullName,
tableSchema,
2024-12-09 11:45:39 +00:00
useLocal: local,
2024-11-06 06:26:23 +00:00
});
} else {
result = await fullAccessDbHandler({
2024-11-18 09:47:14 +00:00
queryString: formattedQuery,
2024-12-06 10:31:24 +00:00
queryValuesArray: queryValuesArray?.map((vl) => String(vl)),
2024-11-06 06:26:23 +00:00
database: dbFullName,
tableSchema,
2024-12-09 11:45:39 +00:00
local,
2024-11-06 06:26:23 +00:00
});
}
2023-09-21 14:00:04 +00:00
} else if (typeof query === "object") {
/**
* Declare variables
*
* @description Declare "results" variable
*/
2024-10-14 06:49:01 +00:00
const {
data,
action,
table,
identifierColumnName,
identifierValue,
update,
duplicateColumnName,
duplicateColumnValue,
} = query;
2023-09-21 14:00:04 +00:00
switch (action.toLowerCase()) {
case "insert":
result = await addDbEntry({
2024-11-06 06:26:23 +00:00
dbContext: local ? "Master" : "Dsql User",
paradigm: "Full Access",
2023-09-21 14:00:04 +00:00
dbFullName: dbFullName,
tableName: table,
data: data,
update,
duplicateColumnName,
duplicateColumnValue,
tableSchema,
2024-12-09 11:45:39 +00:00
useLocal: local,
2023-09-21 14:00:04 +00:00
});
if (!result?.insertId) {
error = new Error("Couldn't insert data");
}
break;
case "update":
result = await updateDbEntry({
2024-11-06 06:26:23 +00:00
dbContext: local ? "Master" : "Dsql User",
2023-09-21 14:00:04 +00:00
paradigm: "Full Access",
dbFullName: dbFullName,
tableName: table,
data: data,
identifierColumnName,
identifierValue,
tableSchema,
2024-12-09 11:45:39 +00:00
useLocal: local,
2023-09-21 14:00:04 +00:00
});
break;
case "delete":
result = await deleteDbEntry({
2024-11-06 06:26:23 +00:00
dbContext: local ? "Master" : "Dsql User",
2023-09-21 14:00:04 +00:00
paradigm: "Full Access",
dbFullName: dbFullName,
tableName: table,
identifierColumnName,
identifierValue,
tableSchema,
2024-12-09 11:45:39 +00:00
useLocal: local,
2023-09-21 14:00:04 +00:00
});
break;
default:
2024-11-06 06:26:23 +00:00
result = null;
2023-09-21 14:00:04 +00:00
break;
}
}
2025-01-10 19:10:28 +00:00
} catch (/** @type {any} */ error: any) {
2024-11-06 06:26:23 +00:00
serverError({
component: "functions/backend/runQuery",
message: error.message,
});
result = null;
2023-09-21 14:00:04 +00:00
error = error.message;
}
return { result, error };
}