Updates
This commit is contained in:
@@ -1,19 +1,25 @@
|
||||
import sqlGenerator from "../../functions/dsql/sql/sql-generator";
|
||||
import { DsqlCrudParam } from "../../types";
|
||||
import { APIResponseObject, DsqlCrudParam } from "../../types";
|
||||
import connDbHandler, { ConnDBHandlerQueryObject } from "../db/conn-db-handler";
|
||||
import { DsqlCrudReturn } from "./crud";
|
||||
|
||||
export default async function dsqlCrudGet({
|
||||
export default async function <
|
||||
T extends { [key: string]: any } = { [key: string]: any }
|
||||
>({
|
||||
table,
|
||||
query,
|
||||
count,
|
||||
countOnly,
|
||||
}: DsqlCrudParam<any>): Promise<DsqlCrudReturn> {
|
||||
dbFullName,
|
||||
}: Omit<
|
||||
DsqlCrudParam<T>,
|
||||
"action" | "data" | "sanitize"
|
||||
>): Promise<APIResponseObject> {
|
||||
let queryObject: ReturnType<Awaited<typeof sqlGenerator>> | undefined;
|
||||
|
||||
queryObject = sqlGenerator({
|
||||
tableName: table,
|
||||
genObject: query,
|
||||
dbFullName,
|
||||
});
|
||||
|
||||
const DB_CONN = global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;
|
||||
@@ -31,6 +37,7 @@ export default async function dsqlCrudGet({
|
||||
tableName: table,
|
||||
genObject: query,
|
||||
count: true,
|
||||
dbFullName,
|
||||
})
|
||||
: undefined;
|
||||
|
||||
@@ -55,8 +62,13 @@ export default async function dsqlCrudGet({
|
||||
return {
|
||||
success: isSuccess,
|
||||
payload: isSuccess ? (countOnly ? null : res[0]) : null,
|
||||
batchPayload: isSuccess ? (countOnly ? null : res) : null,
|
||||
error: isSuccess ? undefined : res?.error,
|
||||
queryObject,
|
||||
errors: res?.errors,
|
||||
queryObject: {
|
||||
sql: queryObject?.string,
|
||||
params: queryObject?.values,
|
||||
},
|
||||
count: isSuccess
|
||||
? res[1]?.[0]?.["COUNT(*)"]
|
||||
? res[1][0]["COUNT(*)"]
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
import post from "../../actions/post";
|
||||
import sqlDeleteGenerator from "../../functions/dsql/sql/sql-delete-generator";
|
||||
import sqlGenerator from "../../functions/dsql/sql/sql-generator";
|
||||
import { DsqlCrudParam, PostReturn } from "../../types";
|
||||
// import dsqlCrudBatchGet from "./crud-batch-get";
|
||||
import {
|
||||
APIResponseObject,
|
||||
DsqlCrudParam,
|
||||
DSQLErrorObject,
|
||||
PostInsertReturn,
|
||||
PostReturn,
|
||||
} from "../../types";
|
||||
import dsqlCrudGet from "./crud-get";
|
||||
|
||||
export type DsqlCrudReturn =
|
||||
| (PostReturn & {
|
||||
queryObject?: ReturnType<Awaited<typeof sqlGenerator>>;
|
||||
count?: number;
|
||||
batchPayload?: any[][] | null;
|
||||
})
|
||||
| null;
|
||||
import connDbHandler from "../db/conn-db-handler";
|
||||
import addDbEntry from "../../functions/backend/db/addDbEntry";
|
||||
import updateDbEntry from "../../functions/backend/db/updateDbEntry";
|
||||
|
||||
export default async function dsqlCrud<
|
||||
T extends { [key: string]: any } = { [key: string]: any }
|
||||
>(params: DsqlCrudParam<T>): Promise<DsqlCrudReturn> {
|
||||
T extends { [key: string]: any } = { [key: string]: any },
|
||||
K extends string = string
|
||||
>(params: DsqlCrudParam<T, K>): Promise<APIResponseObject> {
|
||||
const {
|
||||
action,
|
||||
data,
|
||||
@@ -23,8 +24,17 @@ export default async function dsqlCrud<
|
||||
sanitize,
|
||||
targetField,
|
||||
targetId,
|
||||
dbFullName,
|
||||
deleteData,
|
||||
batchData,
|
||||
deleteKeyValues,
|
||||
} = params;
|
||||
const finalData = sanitize ? sanitize(data) : data;
|
||||
const finalData = (sanitize ? sanitize({ data }) : data) as T;
|
||||
const finalBatchData = (
|
||||
sanitize ? sanitize({ batchData }) : batchData
|
||||
) as T[];
|
||||
|
||||
const DB_CONN = global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;
|
||||
|
||||
switch (action) {
|
||||
case "get":
|
||||
@@ -34,41 +44,59 @@ export default async function dsqlCrud<
|
||||
// return await dsqlCrudBatchGet(params);
|
||||
|
||||
case "insert":
|
||||
return await post({
|
||||
query: {
|
||||
action: "insert",
|
||||
table,
|
||||
data: finalData,
|
||||
},
|
||||
forceLocal: true,
|
||||
const INSERT_RESULT = await addDbEntry({
|
||||
data: finalData,
|
||||
batchData: finalBatchData,
|
||||
tableName: table,
|
||||
dbFullName,
|
||||
});
|
||||
return INSERT_RESULT;
|
||||
|
||||
case "update":
|
||||
delete data?.id;
|
||||
|
||||
return await post({
|
||||
query: {
|
||||
action: "update",
|
||||
table,
|
||||
identifierColumnName: targetField || "id",
|
||||
identifierValue: String(targetValue || targetId),
|
||||
data: finalData,
|
||||
},
|
||||
forceLocal: true,
|
||||
const UPDATE_RESULT = await updateDbEntry({
|
||||
data: finalData,
|
||||
tableName: table,
|
||||
dbFullName,
|
||||
identifierColumnName: (targetField || "id") as string,
|
||||
identifierValue: String(targetValue || targetId),
|
||||
});
|
||||
|
||||
return UPDATE_RESULT;
|
||||
|
||||
case "delete":
|
||||
return await post({
|
||||
query: {
|
||||
action: "delete",
|
||||
table,
|
||||
identifierColumnName: targetField || "id",
|
||||
identifierValue: String(targetValue || targetId),
|
||||
},
|
||||
forceLocal: true,
|
||||
const deleteQuery = sqlDeleteGenerator({
|
||||
data: targetId
|
||||
? { id: targetId }
|
||||
: targetField && targetValue
|
||||
? { [targetField]: targetValue }
|
||||
: deleteData,
|
||||
tableName: table,
|
||||
dbFullName,
|
||||
deleteKeyValues,
|
||||
});
|
||||
|
||||
const res = (await connDbHandler(
|
||||
DB_CONN,
|
||||
deleteQuery?.query,
|
||||
deleteQuery?.values
|
||||
)) as PostInsertReturn;
|
||||
|
||||
return {
|
||||
success: Boolean(res.affectedRows),
|
||||
payload: res,
|
||||
queryObject: {
|
||||
sql: deleteQuery?.query || "",
|
||||
params: deleteQuery?.values || [],
|
||||
},
|
||||
};
|
||||
|
||||
default:
|
||||
return null;
|
||||
return {
|
||||
success: false,
|
||||
payload: undefined,
|
||||
msg: "Invalid action",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,7 +150,10 @@ export default async function dsqlMethodCrud<
|
||||
payload: GET_RESULT?.payload,
|
||||
msg: GET_RESULT?.msg,
|
||||
error: GET_RESULT?.error,
|
||||
queryObject: GET_RESULT?.queryObject,
|
||||
queryObject: {
|
||||
string: GET_RESULT?.queryObject?.sql || "",
|
||||
values: GET_RESULT?.queryObject?.params || [],
|
||||
},
|
||||
};
|
||||
break;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user