2025-01-28 18:43:16 +00:00
|
|
|
import connDbHandler from "../../utils/db/conn-db-handler";
|
2025-01-13 08:00:21 +00:00
|
|
|
import parseDbResults from "./parseDbResults";
|
|
|
|
import serverError from "./serverError";
|
|
|
|
|
|
|
|
type Param = {
|
|
|
|
queryString: string;
|
|
|
|
tableSchema?: import("../../types").DSQL_TableSchemaType | null;
|
|
|
|
queryValuesArray?: string[];
|
2025-02-12 16:56:44 +00:00
|
|
|
forceLocal?: boolean;
|
2025-01-13 08:00:21 +00:00
|
|
|
};
|
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,
|
2025-02-12 16:56:44 +00:00
|
|
|
forceLocal,
|
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;
|
|
|
|
|
2025-02-12 16:56:44 +00:00
|
|
|
const DB_CONN = forceLocal
|
|
|
|
? global.DSQL_DB_CONN
|
|
|
|
: global.DSQL_FULL_ACCESS_DB_CONN || global.DSQL_DB_CONN;
|
2025-01-28 18:43:16 +00:00
|
|
|
|
2024-11-05 11:12:42 +00:00
|
|
|
/**
|
|
|
|
* Fetch from db
|
|
|
|
*
|
|
|
|
* @description Fetch data from db if no cache
|
|
|
|
*/
|
|
|
|
try {
|
2025-01-28 18:43:16 +00:00
|
|
|
results = await connDbHandler(DB_CONN, queryString, queryValuesArray);
|
2024-11-05 11:12:42 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////
|
2025-01-28 18:43:16 +00:00
|
|
|
} catch (error: any) {
|
2024-11-05 11:12:42 +00:00
|
|
|
////////////////////////////////////////
|
|
|
|
|
2025-02-19 19:38:56 +00:00
|
|
|
global.ERROR_CALLBACK?.(`Full Access DB Handler Error`, error as Error);
|
|
|
|
|
2024-11-05 11:12:42 +00:00
|
|
|
serverError({
|
|
|
|
component: "fullAccessDbHandler",
|
|
|
|
message: error.message,
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return error
|
|
|
|
*/
|
|
|
|
return error.message;
|
2025-01-28 18:43:16 +00:00
|
|
|
} finally {
|
|
|
|
DB_CONN?.end();
|
2024-11-05 11:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|