dsql-admin/dsql-app/package-shared/functions/backend/varDatabaseDbHandler.ts
Benjamin Toby 81cf010cb5 Updates
2025-01-14 16:27:08 +01:00

71 lines
2.0 KiB
TypeScript

import parseDbResults from "./parseDbResults";
import serverError from "./serverError";
import grabDSQLConnection from "../../utils/grab-dsql-connection";
import connDbHandler from "../../utils/db/conn-db-handler";
type Param = {
queryString: string;
queryValuesArray?: any[];
database?: string;
tableSchema?: import("../../types").DSQL_TableSchemaType;
useLocal?: boolean;
debug?: boolean;
};
/**
* # DB handler for specific database
*/
export default async function varDatabaseDbHandler({
queryString,
queryValuesArray,
database,
tableSchema,
useLocal,
debug,
}: Param): Promise<any> {
let CONNECTION = grabDSQLConnection({ fa: true });
if (useLocal) CONNECTION = grabDSQLConnection({ local: true });
if (database?.match(/^datasquirel$/)) CONNECTION = grabDSQLConnection();
if (debug) {
console.log(`varDatabaseDbHandler:query:`, queryString);
console.log(`varDatabaseDbHandler:values:`, queryValuesArray);
}
let results = await connDbHandler(
CONNECTION,
queryString,
queryValuesArray
);
if (debug) {
console.log(`varDatabaseDbHandler:results:`, results);
}
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;
}
}