import mysql from "serverless-mysql";
import grabDSQLConnection from "../../grab-dsql-connection";

/**
 * # DSQL user read-only DB handler
 * @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
 */
export default async function DB_HANDLER(...args: any[]) {
    const CONNECTION = grabDSQLConnection();

    try {
        if (!CONNECTION)
            throw new Error("No Connection provided to DB_HANDLER function!");

        const results = await CONNECTION.query(...args);

        return JSON.parse(JSON.stringify(results));
    } catch (error: any) {
        console.log("DB Error =>", error);
        return {
            success: false,
            error: error.message,
        };
    } finally {
        await CONNECTION?.end();
    }
}