import post from "../../actions/post"; import sqlGenerator from "../../functions/dsql/sql/sql-generator"; import { DsqlCrudParam, PostReturn } from "../../types"; // import dsqlCrudBatchGet from "./crud-batch-get"; import dsqlCrudGet from "./crud-get"; export type DsqlCrudReturn = | (PostReturn & { queryObject?: ReturnType>; count?: number; batchPayload?: any[][] | null; }) | null; export default async function dsqlCrud< T extends { [key: string]: any } = { [key: string]: any } >(params: DsqlCrudParam): Promise { const { action, data, table, targetValue, sanitize, targetField, targetId, } = params; const finalData = sanitize ? sanitize(data) : data; switch (action) { case "get": return await dsqlCrudGet(params); // case "batch-get": // return await dsqlCrudBatchGet(params); case "insert": return await post({ query: { action: "insert", table, data: finalData, }, forceLocal: true, }); case "update": delete data?.id; return await post({ query: { action: "update", table, identifierColumnName: targetField || "id", identifierValue: String(targetValue || targetId), data: finalData, }, forceLocal: true, }); case "delete": return await post({ query: { action: "delete", table, identifierColumnName: targetField || "id", identifierValue: String(targetValue || targetId), }, forceLocal: true, }); default: return null; } }