dsql-admin/dsql-app/package-shared/functions/backend/fullAccessDbHandler.ts

80 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-11-05 11:12:42 +00:00
// @ts-check
2025-01-13 08:00:21 +00:00
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;
local?: boolean;
tableSchema?: import("../../types").DSQL_TableSchemaType | null;
queryValuesArray?: string[];
};
2024-11-05 11:12:42 +00:00
/**
2025-01-13 08:00:21 +00:00
* # Full Access Db Handler
2024-11-05 11:12:42 +00:00
*/
2025-01-13 08:00:21 +00:00
export default async function fullAccessDbHandler({
2024-11-05 11:12:42 +00:00
queryString,
tableSchema,
queryValuesArray,
2024-11-06 12:06:51 +00:00
local,
2025-01-13 08:00:21 +00:00
}: Param) {
2024-11-05 11:12:42 +00:00
/**
* Declare variables
*
* @description Declare "results" variable
*/
let results;
/**
* Fetch from db
*
* @description Fetch data from db if no cache
*/
try {
/** ********************* Run Query */
2024-12-09 12:27:08 +00:00
results = local
? await LOCAL_DB_HANDLER(queryString, queryValuesArray)
: await DSQL_USER_DB_HANDLER({
paradigm: "Full Access",
queryString,
queryValues: queryValuesArray,
});
2024-11-05 11:12:42 +00:00
////////////////////////////////////////
2025-01-13 08:00:21 +00:00
} catch (/** @type {any} */ error: any) {
2024-11-05 11:12:42 +00:00
////////////////////////////////////////
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;
}
2025-01-13 08:00:21 +00:00
}