datasquirel/package-shared/functions/backend/varReadOnlyDatabaseDbHandler.ts

82 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-11-06 06:26:23 +00:00
// @ts-check
2025-01-10 19:10:28 +00:00
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;
};
2024-11-06 06:26:23 +00:00
/**
2025-01-10 19:10:28 +00:00
* # Read Only Db Handler with Varaibles
2024-11-06 06:26:23 +00:00
* @returns
*/
2025-01-10 19:10:28 +00:00
export default async function varReadOnlyDatabaseDbHandler({
2024-11-06 06:26:23 +00:00
queryString,
database,
queryValuesArray,
tableSchema,
2024-12-09 11:45:39 +00:00
useLocal,
2025-01-10 19:10:28 +00:00
}: Param) {
2024-11-06 06:26:23 +00:00
/**
* Declare variables
*
* @description Declare "results" variable
*/
let results;
/**
* Fetch from db
*
* @description Fetch data from db if no cache
*/
try {
2024-12-09 11:45:39 +00:00
results = useLocal
? await LOCAL_DB_HANDLER(queryString, queryValuesArray)
: await DSQL_USER_DB_HANDLER({
paradigm: "Read Only",
database,
queryString,
queryValues: queryValuesArray,
});
2024-11-06 06:26:23 +00:00
////////////////////////////////////////
2025-01-10 19:10:28 +00:00
} catch (/** @type {any} */ error: any) {
2024-11-06 06:26:23 +00:00
////////////////////////////////////////
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;
}
2025-01-10 19:10:28 +00:00
}