datasquirel/dist/package-shared/functions/api/query/post.js
Benjamin Toby 7e8bb37c09 Updates
2025-07-05 14:59:30 +01:00

81 lines
2.6 KiB
JavaScript

import _ from "lodash";
import serverError from "../../backend/serverError";
import runQuery from "../../backend/db/runQuery";
import debugLog from "../../../utils/logging/debug-log";
/**
* # Post Function For API
*/
export default async function apiPost({ query, dbFullName, queryValues, tableName, dbSchema, dbContext, forceLocal, debug, }) {
var _a, _b;
if (typeof query === "string" && (query === null || query === void 0 ? void 0 : query.match(/^create |^alter |^drop /i))) {
return { success: false, msg: "Wrong Input" };
}
if (typeof query === "object" &&
((_a = query === null || query === void 0 ? void 0 : query.action) === null || _a === void 0 ? void 0 : _a.match(/^create |^alter |^drop /i))) {
return { success: false, msg: "Wrong Input" };
}
let results;
/**
* Create new user folder and file
*
* @description Create new user folder and file
*/
try {
let { result, error } = await runQuery({
dbFullName,
query,
dbSchema,
queryValuesArray: queryValues,
tableName,
dbContext,
forceLocal,
debug,
});
if (debug) {
debugLog({
log: result,
addTime: true,
label: "result",
});
debugLog({
log: query,
addTime: true,
label: "query",
});
}
results = result;
if (error)
throw new Error(error);
let tableSchema;
if (dbSchema) {
const targetTable = dbSchema.tables.find((table) => table.tableName === tableName);
if (targetTable) {
const clonedTargetTable = _.cloneDeep(targetTable);
delete clonedTargetTable.childTable;
delete clonedTargetTable.childrenTables;
delete clonedTargetTable.updateData;
delete clonedTargetTable.indexes;
tableSchema = clonedTargetTable;
}
}
return {
success: true,
payload: results,
error: error,
schema: tableName && tableSchema ? tableSchema : undefined,
};
}
catch (error) {
serverError({
component: "/api/query/post/lines-132-142",
message: error.message,
});
(_b = global.ERROR_CALLBACK) === null || _b === void 0 ? void 0 : _b.call(global, `API Post Error`, error);
return {
success: false,
payload: results,
error: error.message,
};
}
}