datasquirel/package-shared/functions/backend/fullAccessDbHandler.ts
2025-01-10 20:10:28 +01:00

83 lines
2.1 KiB
TypeScript

// @ts-check
import DSQL_USER_DB_HANDLER from "../../utils/backend/global-db/DSQL_USER_DB_HANDLER";
import LOCAL_DB_HANDLER from "../../utils/backend/global-db/LOCAL_DB_HANDLER";
import parseDbResults from "./parseDbResults";
import serverError from "./serverError";
type Param = {
queryString: string;
database: string;
local?: boolean;
tableSchema?: import("../../types").DSQL_TableSchemaType | null;
queryValuesArray?: string[];
};
/**
* # Full Access Db Handler
*/
export default async function fullAccessDbHandler({
queryString,
database,
tableSchema,
queryValuesArray,
local,
}: Param) {
/**
* Declare variables
*
* @description Declare "results" variable
*/
let results;
/**
* Fetch from db
*
* @description Fetch data from db if no cache
*/
try {
/** ********************* Run Query */
results = local
? await LOCAL_DB_HANDLER(queryString, queryValuesArray)
: await DSQL_USER_DB_HANDLER({
paradigm: "Full Access",
database,
queryString,
queryValues: queryValuesArray,
});
////////////////////////////////////////
} catch (/** @type {any} */ error: any) {
////////////////////////////////////////
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;
}
}