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

79 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 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;
queryValuesArray?: string[];
tableSchema?: import("../../types").DSQL_TableSchemaType;
useLocal?: boolean;
};
2024-11-05 11:12:42 +00:00
/**
2025-01-13 08:00:21 +00:00
* # Read Only Db Handler with Varaibles
2024-11-05 11:12:42 +00:00
* @returns
*/
2025-01-13 08:00:21 +00:00
export default async function varReadOnlyDatabaseDbHandler({
2024-11-05 11:12:42 +00:00
queryString,
queryValuesArray,
tableSchema,
2024-12-09 12:27:08 +00:00
useLocal,
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 {
2024-12-09 12:27:08 +00:00
results = useLocal
? await LOCAL_DB_HANDLER(queryString, queryValuesArray)
: await DSQL_USER_DB_HANDLER({
paradigm: "Read Only",
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: "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-13 08:00:21 +00:00
}