134 lines
3.8 KiB
TypeScript
134 lines
3.8 KiB
TypeScript
import debugLog from "../logging/debug-log";
|
|
import { DSQLErrorObject } from "../../types";
|
|
import mariadb, { Connection, ConnectionConfig, Pool } from "mariadb";
|
|
import grabDSQLConnection from "../grab-dsql-connection";
|
|
|
|
export type ConnDBHandlerQueryObject = {
|
|
query: string;
|
|
values?: (string | number | undefined)[];
|
|
};
|
|
|
|
type Return<ReturnType = any> =
|
|
| ReturnType
|
|
| ReturnType[]
|
|
| null
|
|
| { error?: string; errors?: DSQLErrorObject[]; config?: ConnectionConfig };
|
|
|
|
/**
|
|
* # 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>(
|
|
/**
|
|
* MariaDB Connection
|
|
*/
|
|
conn?: mariadb.Connection | null,
|
|
/**
|
|
* String Or `ConnDBHandlerQueryObject` Array
|
|
*/
|
|
query?: ConnDBHandlerQueryObject["query"] | ConnDBHandlerQueryObject[],
|
|
/**
|
|
* Array of Values to Sanitize and Inject
|
|
*/
|
|
values?: ConnDBHandlerQueryObject["values"],
|
|
debug?: boolean
|
|
): Promise<Return<ReturnType>> {
|
|
const finalConnection = conn || (await grabDSQLConnection());
|
|
|
|
try {
|
|
if (!finalConnection) throw new Error("No Connection Found!");
|
|
if (!query) throw new Error("Query String Required!");
|
|
|
|
let queryErrorArray: DSQLErrorObject[] = [];
|
|
|
|
if (typeof query == "string") {
|
|
const [results] = await finalConnection.query(
|
|
trimQuery(query),
|
|
values
|
|
);
|
|
|
|
if (debug) {
|
|
debugLog({
|
|
log: results,
|
|
addTime: true,
|
|
label: "res",
|
|
});
|
|
}
|
|
|
|
return results;
|
|
} else if (typeof query == "object") {
|
|
const resArray = [];
|
|
|
|
for (let i = 0; i < query.length; i++) {
|
|
let currentQueryError: DSQLErrorObject = {};
|
|
|
|
try {
|
|
const queryObj = query[i];
|
|
|
|
currentQueryError.sql = queryObj.query;
|
|
currentQueryError.sqlValues = queryObj.values;
|
|
|
|
const queryObjRes = await finalConnection.query(
|
|
trimQuery(queryObj.query),
|
|
queryObj.values
|
|
);
|
|
|
|
const results = queryObjRes[0];
|
|
|
|
if (debug) {
|
|
debugLog({
|
|
log: results,
|
|
addTime: true,
|
|
label: "queryObjRes",
|
|
});
|
|
}
|
|
|
|
resArray.push(results);
|
|
} catch (error: any) {
|
|
resArray.push(null);
|
|
currentQueryError["error"] = error.message;
|
|
queryErrorArray.push(currentQueryError);
|
|
}
|
|
}
|
|
|
|
if (debug) {
|
|
debugLog({
|
|
log: resArray,
|
|
addTime: true,
|
|
label: "resArray",
|
|
});
|
|
}
|
|
|
|
if (queryErrorArray[0]) {
|
|
return {
|
|
errors: queryErrorArray,
|
|
};
|
|
}
|
|
|
|
return resArray;
|
|
} else {
|
|
return null;
|
|
}
|
|
} catch (error: any) {
|
|
if (debug) {
|
|
debugLog({
|
|
log: `Connection DB Handler Error: ${error.message}`,
|
|
addTime: true,
|
|
label: "Error",
|
|
});
|
|
}
|
|
|
|
return {
|
|
error: `Connection DB Handler Error: ${error.message}`,
|
|
// config: conn,
|
|
};
|
|
} finally {
|
|
await finalConnection?.end();
|
|
}
|
|
}
|
|
|
|
function trimQuery(query: string) {
|
|
return query.replace(/\n/gm, " ").replace(/ {2,}/g, " ").trim();
|
|
}
|