112 lines
3.1 KiB
TypeScript
112 lines
3.1 KiB
TypeScript
import parseDbResults from "./parseDbResults";
|
|
import serverError from "./serverError";
|
|
import DB_HANDLER from "../../utils/backend/global-db/DB_HANDLER";
|
|
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?: any[];
|
|
database?: string;
|
|
tableSchema?: import("../../types").DSQL_TableSchemaType;
|
|
useLocal?: boolean;
|
|
};
|
|
|
|
/**
|
|
* # DB handler for specific database
|
|
*/
|
|
export default async function varDatabaseDbHandler({
|
|
queryString,
|
|
queryValuesArray,
|
|
database,
|
|
tableSchema,
|
|
useLocal,
|
|
}: Param): Promise<any> {
|
|
/**
|
|
* Declare variables
|
|
*
|
|
* @description Declare "results" variable
|
|
*/
|
|
const isMaster = useLocal
|
|
? true
|
|
: database?.match(/^datasquirel$/)
|
|
? true
|
|
: false;
|
|
|
|
/** @type {any} */
|
|
const FINAL_DB_HANDLER: any = useLocal
|
|
? LOCAL_DB_HANDLER
|
|
: isMaster
|
|
? DB_HANDLER
|
|
: DSQL_USER_DB_HANDLER;
|
|
|
|
let results;
|
|
|
|
/**
|
|
* Fetch from db
|
|
*
|
|
* @description Fetch data from db if no cache
|
|
*/
|
|
try {
|
|
if (
|
|
queryString &&
|
|
queryValuesArray &&
|
|
Array.isArray(queryValuesArray) &&
|
|
queryValuesArray[0]
|
|
) {
|
|
results = isMaster
|
|
? await FINAL_DB_HANDLER(queryString, queryValuesArray)
|
|
: await FINAL_DB_HANDLER({
|
|
paradigm: "Full Access",
|
|
database,
|
|
queryString,
|
|
queryValues: queryValuesArray,
|
|
});
|
|
} else {
|
|
results = isMaster
|
|
? await FINAL_DB_HANDLER(queryString)
|
|
: await FINAL_DB_HANDLER({
|
|
paradigm: "Full Access",
|
|
database,
|
|
queryString,
|
|
});
|
|
}
|
|
} catch (error: any) {
|
|
serverError({
|
|
component: "varDatabaseDbHandler/lines-29-32",
|
|
message: error.message,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Return results
|
|
*
|
|
* @description Return results add to cache if "req" param is passed
|
|
*/
|
|
if (results && tableSchema) {
|
|
try {
|
|
const unparsedResults = results;
|
|
const parsedResults = await parseDbResults({
|
|
unparsedResults: unparsedResults,
|
|
tableSchema: tableSchema,
|
|
});
|
|
return parsedResults;
|
|
} catch (error: any) {
|
|
console.log(
|
|
"\x1b[31mvarDatabaseDbHandler ERROR\x1b[0m =>",
|
|
database,
|
|
error
|
|
);
|
|
serverError({
|
|
component: "varDatabaseDbHandler/lines-52-53",
|
|
message: error.message,
|
|
});
|
|
return null;
|
|
}
|
|
} else if (results) {
|
|
return results;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|