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

/**
 * # DSQL user read-only DB handler
 */
export default function NO_DB_HANDLER(...args: any[]) {
    const CONNECTION = grabDSQLConnection();

    try {
        return new Promise((resolve, reject) => {
            CONNECTION.query(...args)
                .then((results) => {
                    CONNECTION.end();
                    resolve(JSON.parse(JSON.stringify(results)));
                })
                .catch((err) => {
                    CONNECTION.end();
                    resolve({
                        error: err.message,
                        sql: err.sql,
                    });
                });
        });
    } catch (error: any) {
        return {
            success: false,
            error: error.message,
        };
    } finally {
        CONNECTION?.end();
    }
}
