datasquirel/package-shared/utils/backend/global-db/DB_HANDLER.ts
2025-01-10 20:10:28 +01:00

37 lines
929 B
TypeScript

import mysql from "serverless-mysql";
import grabDbSSL from "../grabDbSSL";
const MASTER = 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",
ssl: grabDbSSL(),
},
});
/**
* # DSQL user read-only DB handler
*/
export default async function DB_HANDLER(...args: any[]) {
try {
const results = await MASTER.query(...args);
/** ********************* Clean up */
await MASTER.end();
return JSON.parse(JSON.stringify(results));
} catch (error: any) {
console.log("DB Error =>", error);
return {
success: false,
error: error.message,
};
}
}