55 lines
1.5 KiB
JavaScript
55 lines
1.5 KiB
JavaScript
|
// @ts-check
|
||
|
|
||
|
const mysql = require("serverless-mysql");
|
||
|
const grabDbSSL = require("../grabDbSSL");
|
||
|
|
||
|
/**
|
||
|
* DSQL user read-only DB handler
|
||
|
* @param {object} params
|
||
|
* @param {string} params.paradigm
|
||
|
* @param {string} params.database
|
||
|
* @param {string} params.queryString
|
||
|
* @param {string[]} [params.queryValues]
|
||
|
*/
|
||
|
async function LOCAL_DB_HANDLER(/** @type {any[]} */ ...args) {
|
||
|
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!");
|
||
|
},
|
||
|
onConnectError: (/** @type {any} */ err) => {
|
||
|
console.log("Connection Error", err.message);
|
||
|
},
|
||
|
onError: (/** @type {any} */ err) => {
|
||
|
console.log("Client Error", err.message);
|
||
|
},
|
||
|
});
|
||
|
|
||
|
console.log("Querying ...");
|
||
|
|
||
|
try {
|
||
|
const results = await MASTER.query(...args);
|
||
|
await MASTER.end();
|
||
|
|
||
|
return JSON.parse(JSON.stringify(results));
|
||
|
} catch (/** @type {any} */ error) {
|
||
|
console.log("DB Error =>", error.message);
|
||
|
return {
|
||
|
success: false,
|
||
|
error: error.message,
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = LOCAL_DB_HANDLER;
|