datasquirel/dist/package-shared/utils/db/conn-db-handler.js
Benjamin Toby 186cc76ffb Updates
2025-01-12 18:19:20 +01:00

58 lines
2.0 KiB
JavaScript

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = connDbHandler;
/**
* # Run Query From MySQL Connection
* @description Run a query from a pre-existing MySQL/Mariadb Connection
* setup with `serverless-mysql` npm module
*/
function connDbHandler(
/**
* ServerlessMySQL Connection Object
*/
conn,
/**
* String Or `QueryObject` Array
*/
query,
/**
* Array of Values to Sanitize and Inject
*/
values) {
return __awaiter(this, void 0, void 0, function* () {
try {
if (typeof query == "string") {
const res = yield 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 = yield conn.query(queryObj.query, queryObj.values);
resArray.push(JSON.parse(JSON.stringify(queryObjRes)));
}
return resArray;
}
else {
return null;
}
}
catch (error) {
return null;
}
finally {
conn.end();
}
});
}