datasquirel/package-shared/utils/backend/global-db/DB_HANDLER.ts

37 lines
929 B
TypeScript
Raw Normal View History

2025-01-10 19:10:28 +00:00
import mysql from "serverless-mysql";
import grabDbSSL from "../grabDbSSL";
2024-11-06 06:26:23 +00:00
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,
2024-11-06 09:30:00 +00:00
port: process.env.DSQL_DB_PORT
? Number(process.env.DSQL_DB_PORT)
: undefined,
2024-11-06 06:26:23 +00:00
charset: "utf8mb4",
2024-11-07 14:28:28 +00:00
ssl: grabDbSSL(),
2024-11-06 06:26:23 +00:00
},
});
/**
2025-01-10 19:10:28 +00:00
* # DSQL user read-only DB handler
*/
export default async function DB_HANDLER(...args: any[]) {
2024-11-06 06:26:23 +00:00
try {
const results = await MASTER.query(...args);
/** ********************* Clean up */
await MASTER.end();
return JSON.parse(JSON.stringify(results));
2025-01-10 19:10:28 +00:00
} catch (error: any) {
2024-11-06 06:26:23 +00:00
console.log("DB Error =>", error);
return {
success: false,
error: error.message,
};
}
}