This commit is contained in:
Benjamin Toby
2025-01-12 18:19:20 +01:00
parent a3561da53d
commit 186cc76ffb
46 changed files with 344 additions and 211 deletions
@@ -11,7 +11,7 @@ const MASTER = mysql({
? Number(process.env.DSQL_DB_PORT)
: undefined,
charset: "utf8mb4",
ssl: grabDbSSL(),
// ssl: grabDbSSL(),
},
});
@@ -120,5 +120,3 @@ export default function DSQL_USER_DB_HANDLER({
};
}
}
module.exports = DSQL_USER_DB_HANDLER;
@@ -0,0 +1,52 @@
import { ServerlessMysql } from "serverless-mysql";
type QueryObject = {
query: string;
values?: (string | number | undefined)[];
};
type Return<ReturnType = any> = ReturnType | null;
/**
* # Run Query From MySQL Connection
* @description Run a query from a pre-existing MySQL/Mariadb Connection
* setup with `serverless-mysql` npm module
*/
export default async function connDbHandler<ReturnType = any>(
/**
* ServerlessMySQL Connection Object
*/
conn: ServerlessMysql,
/**
* String Or `QueryObject` Array
*/
query: QueryObject["query"] | QueryObject[],
/**
* Array of Values to Sanitize and Inject
*/
values?: QueryObject["values"]
): Promise<Return<ReturnType>> {
try {
if (typeof query == "string") {
const res = await conn.query(query, values);
return JSON.parse(JSON.stringify(res));
} else if (typeof query == "object") {
const resArray = [];
for (let i = 0; i < query.length; i++) {
const queryObj = query[i];
const queryObjRes = await conn.query(
queryObj.query,
queryObj.values
);
resArray.push(JSON.parse(JSON.stringify(queryObjRes)));
}
return resArray as any;
} else {
return null;
}
} catch (error) {
return null;
} finally {
conn.end();
}
}
+2 -2
View File
@@ -20,11 +20,11 @@ function parse(
*/
function stringify(
value: any,
replacer?: (this: any, key: string, value: any) => any,
replacer?: ((this: any, key: string, value: any) => any) | null,
space?: string | number
): string | undefined {
try {
return JSON.stringify(value, replacer, space);
return JSON.stringify(value, replacer || undefined, space);
} catch (error) {
return undefined;
}
+1 -1
View File
@@ -11,4 +11,4 @@ function endConnection(connection: mysql.Connection) {
}
}
module.exports = endConnection;
export default endConnection;