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

/**
 * # Root DB handler
 */
export default function ROOT_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) {
        global.ERROR_CALLBACK?.(`ROOT_DB_HANDLER Error`, error as Error);
        return {
            success: false,
            error: error.message,
        };
    } finally {
        CONNECTION?.end();
    }
}
