datasquirel/dist/package-shared/utils/data-fetching/method-crud.js
Benjamin Toby 7e8bb37c09 Updates
2025-07-05 14:59:30 +01:00

151 lines
6.9 KiB
JavaScript

import _ from "lodash";
import deserializeQuery from "../deserialize-query";
import EJSON from "../ejson";
import numberfy from "../numberfy";
import dsqlCrud from "./crud";
export default async function dsqlMethodCrud({ method, tableName, addUser, user, extraData, transformData, existingData, body, query, targetId, sanitize, transformQuery, debug, }) {
var _a, _b, _c, _d, _e;
let result = {
success: false,
};
try {
let finalBody = body;
let finalQuery = deserializeQuery(query);
let LIMIT = 10;
let PAGE = 1;
let OFFSET = (PAGE - 1) * LIMIT;
if (method == "GET") {
const newFinalQuery = _.cloneDeep(finalQuery || {});
Object.keys(newFinalQuery).forEach((key) => {
const value = newFinalQuery[key];
if (typeof value == "string" && value.match(/^\{|^\[/)) {
newFinalQuery[key] = EJSON.stringify(value);
}
if (value == "true") {
newFinalQuery[key] = true;
}
if (value == "false") {
newFinalQuery[key] = false;
}
});
if (newFinalQuery.limit)
LIMIT = numberfy(newFinalQuery.limit);
if (newFinalQuery.page)
PAGE = numberfy(newFinalQuery.page);
OFFSET = (PAGE - 1) * LIMIT;
finalQuery = newFinalQuery;
}
let finalData = finalBody
? Object.assign(Object.assign({}, finalBody), extraData)
: {};
if ((user === null || user === void 0 ? void 0 : user.id) && addUser) {
finalData = Object.assign(Object.assign({}, finalData), { [addUser.field]: String(user.id) });
}
if (transformData) {
if (debug) {
console.log("DEBUG:::transforming Data ...");
}
finalData = (await transformData({
data: finalData,
existingData: existingData,
user,
reqMethod: method,
}));
}
if (transformQuery) {
if (debug) {
console.log("DEBUG:::transforming Query ...");
}
finalQuery = await transformQuery({
query: finalQuery || {},
user,
reqMethod: method,
});
}
if (debug) {
console.log("DEBUG:::finalQuery", finalQuery);
console.log("DEBUG:::finalData", finalData);
}
switch (method) {
case "GET":
const GET_RESULT = await dsqlCrud({
action: "get",
table: tableName,
query: Object.assign(Object.assign({}, finalQuery), { query: Object.assign(Object.assign({}, finalQuery === null || finalQuery === void 0 ? void 0 : finalQuery.query), ((user === null || user === void 0 ? void 0 : user.id) && addUser
? {
[addUser.field]: {
value: String(user.id),
},
}
: undefined)), limit: LIMIT, offset: OFFSET }),
sanitize,
});
result = {
success: Boolean(GET_RESULT === null || GET_RESULT === void 0 ? void 0 : GET_RESULT.success),
payload: GET_RESULT === null || GET_RESULT === void 0 ? void 0 : GET_RESULT.payload,
msg: GET_RESULT === null || GET_RESULT === void 0 ? void 0 : GET_RESULT.msg,
error: GET_RESULT === null || GET_RESULT === void 0 ? void 0 : GET_RESULT.error,
queryObject: {
string: ((_a = GET_RESULT === null || GET_RESULT === void 0 ? void 0 : GET_RESULT.queryObject) === null || _a === void 0 ? void 0 : _a.sql) || "",
values: ((_b = GET_RESULT === null || GET_RESULT === void 0 ? void 0 : GET_RESULT.queryObject) === null || _b === void 0 ? void 0 : _b.params) || [],
},
};
break;
case "POST":
const POST_RESULT = await dsqlCrud({
action: "insert",
table: tableName,
data: finalData && ((_c = Object.keys(finalData)) === null || _c === void 0 ? void 0 : _c[0])
? finalData
: undefined,
sanitize,
});
result = {
success: Boolean(POST_RESULT === null || POST_RESULT === void 0 ? void 0 : POST_RESULT.success),
payload: POST_RESULT === null || POST_RESULT === void 0 ? void 0 : POST_RESULT.payload,
msg: POST_RESULT === null || POST_RESULT === void 0 ? void 0 : POST_RESULT.msg,
error: POST_RESULT === null || POST_RESULT === void 0 ? void 0 : POST_RESULT.error,
};
break;
case "PUT":
const PUT_RESULT = await dsqlCrud({
action: "update",
table: tableName,
data: finalData && ((_d = Object.keys(finalData)) === null || _d === void 0 ? void 0 : _d[0])
? finalData
: undefined,
targetId,
sanitize,
});
result = {
success: Boolean(PUT_RESULT === null || PUT_RESULT === void 0 ? void 0 : PUT_RESULT.success),
payload: PUT_RESULT === null || PUT_RESULT === void 0 ? void 0 : PUT_RESULT.payload,
msg: PUT_RESULT === null || PUT_RESULT === void 0 ? void 0 : PUT_RESULT.msg,
error: PUT_RESULT === null || PUT_RESULT === void 0 ? void 0 : PUT_RESULT.error,
};
break;
case "DELETE":
const DELETE_RESULT = await dsqlCrud({
action: "delete",
table: tableName,
targetId,
sanitize,
});
result = {
success: Boolean(DELETE_RESULT === null || DELETE_RESULT === void 0 ? void 0 : DELETE_RESULT.success),
payload: DELETE_RESULT === null || DELETE_RESULT === void 0 ? void 0 : DELETE_RESULT.payload,
msg: DELETE_RESULT === null || DELETE_RESULT === void 0 ? void 0 : DELETE_RESULT.msg,
error: DELETE_RESULT === null || DELETE_RESULT === void 0 ? void 0 : DELETE_RESULT.error,
};
break;
default:
break;
}
return result;
}
catch (error) {
(_e = global.ERROR_CALLBACK) === null || _e === void 0 ? void 0 : _e.call(global, `Method Crud Error`, error);
return result;
}
}