This commit is contained in:
Benjamin Toby
2025-06-01 12:14:49 +01:00
parent 9b7c98cff6
commit cd96c11817
6 changed files with 60 additions and 22 deletions
+25 -11
View File
@@ -17,6 +17,7 @@ export default async function dsqlCrud<
targetField,
targetId,
count,
countOnly,
}: DsqlCrudParam<T>): Promise<
| (PostReturn & {
queryObject?: ReturnType<Awaited<typeof sqlGenerator>>;
@@ -37,24 +38,34 @@ export default async function dsqlCrud<
const DB_CONN =
global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;
const connQueries: ConnDBHandlerQueryObject[] = [
let connQueries: ConnDBHandlerQueryObject[] = [
{
query: queryObject?.string,
values: queryObject?.values || [],
},
];
if (count) {
const countQueryObject = sqlGenerator({
tableName: table,
genObject: query,
count: true,
});
const countQueryObject =
count || countOnly
? sqlGenerator({
tableName: table,
genObject: query,
count: true,
})
: undefined;
if (count && countQueryObject) {
connQueries.push({
query: countQueryObject.string,
values: countQueryObject.values,
});
} else if (countOnly && countQueryObject) {
connQueries = [
{
query: countQueryObject.string,
values: countQueryObject.values,
},
];
}
const res = await connDbHandler(DB_CONN, connQueries);
@@ -63,13 +74,16 @@ export default async function dsqlCrud<
return {
success: isSuccess,
payload: isSuccess ? res[0] : null,
payload: isSuccess ? (countOnly ? null : res[0]) : null,
error: isSuccess ? undefined : res?.error,
queryObject,
count:
isSuccess && res[1]?.[0]?.["COUNT(*)"]
count: isSuccess
? res[1]?.[0]?.["COUNT(*)"]
? res[1][0]["COUNT(*)"]
: undefined,
: res[0]?.[0]?.["COUNT(*)"]
? res[0][0]["COUNT(*)"]
: undefined
: undefined,
};
case "insert":