import mysql from "serverless-mysql";

export default function setupDb() {
    global.DSQL_DB_CONN = mysql({
        config: {
            host: process.env.DSQL_DB_HOST,
            user: process.env.DSQL_DB_USERNAME,
            password: process.env.DSQL_DB_PASSWORD,
            database: process.env.DSQL_DB_NAME,
            port: process.env.DSQL_DB_PORT
                ? Number(process.env.DSQL_DB_PORT)
                : undefined,
            charset: "utf8mb4",
        },
    });

    global.DSQL_READ_ONLY_DB_CONN = mysql({
        config: {
            host: process.env.DSQL_DB_HOST,
            user: process.env.DSQL_DB_READ_ONLY_USERNAME,
            password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
            port: process.env.DSQL_DB_PORT
                ? Number(process.env.DSQL_DB_PORT)
                : undefined,
            charset: "utf8mb4",
        },
    });

    global.DSQL_FULL_ACCESS_DB_CONN = mysql({
        config: {
            host: process.env.DSQL_DB_HOST,
            user: process.env.DSQL_DB_FULL_ACCESS_USERNAME,
            password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD,
            port: process.env.DSQL_DB_PORT
                ? Number(process.env.DSQL_DB_PORT)
                : undefined,
            charset: "utf8mb4",
        },
    });

    global.DSQL_USE_LOCAL = true;
}
