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

46 lines
1.3 KiB
TypeScript
Raw Normal View History

2025-01-10 19:10:28 +00:00
import mysql from "serverless-mysql";
import grabDbSSL from "../grabDbSSL";
2024-11-17 04:36:56 +00:00
/**
2025-01-10 19:10:28 +00:00
* # DSQL user read-only DB handler
2024-11-17 04:36:56 +00:00
*/
2025-01-10 19:10:28 +00:00
export default async function LOCAL_DB_HANDLER(...args: any[]) {
2024-11-18 09:25:28 +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,
port: process.env.DSQL_DB_PORT
? Number(process.env.DSQL_DB_PORT)
: undefined,
charset: "utf8mb4",
ssl: grabDbSSL(),
},
onConnect: () => {
console.log("Connection Successful!");
},
2025-01-10 19:10:28 +00:00
onConnectError: (/** @type {any} */ err: any) => {
2024-11-18 09:25:28 +00:00
console.log("Connection Error", err.message);
},
2025-01-10 19:10:28 +00:00
onError: (/** @type {any} */ err: any) => {
2024-11-18 09:25:28 +00:00
console.log("Client Error", err.message);
},
2024-11-17 04:36:56 +00:00
});
2024-11-18 09:25:28 +00:00
console.log("Querying ...");
2024-11-17 04:36:56 +00:00
try {
const results = await MASTER.query(...args);
await MASTER.end();
return JSON.parse(JSON.stringify(results));
2025-01-10 19:10:28 +00:00
} catch (/** @type {any} */ error: any) {
2024-11-18 09:25:28 +00:00
console.log("DB Error =>", error.message);
2024-11-17 04:36:56 +00:00
return {
success: false,
error: error.message,
};
}
}