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

82 lines
2.0 KiB
TypeScript

// @ts-check
import fs from "fs";
import serverError from "./serverError";
import parseDbResults from "./parseDbResults";
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";
type Param = {
queryString: string;
database: string;
queryValuesArray?: string[];
tableSchema?: import("../../types").DSQL_TableSchemaType;
useLocal?: boolean;
};
/**
* # Read Only Db Handler with Varaibles
* @returns
*/
export default async function varReadOnlyDatabaseDbHandler({
queryString,
database,
queryValuesArray,
tableSchema,
useLocal,
}: Param) {
/**
* Declare variables
*
* @description Declare "results" variable
*/
let results;
/**
* Fetch from db
*
* @description Fetch data from db if no cache
*/
try {
results = useLocal
? await LOCAL_DB_HANDLER(queryString, queryValuesArray)
: await DSQL_USER_DB_HANDLER({
paradigm: "Read Only",
database,
queryString,
queryValues: queryValuesArray,
});
////////////////////////////////////////
} catch (/** @type {any} */ error: any) {
////////////////////////////////////////
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;
}
}