Skip to content

Commit

Permalink
query building write functionality pt 2 (#259)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
fzhao99 and pre-commit-ci[bot] authored Jan 10, 2025
1 parent 2c434d3 commit f87166b
Show file tree
Hide file tree
Showing 19 changed files with 663 additions and 861 deletions.
10 changes: 7 additions & 3 deletions query-connector/src/app/backend/query-building.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
"use server";

import { getDbClient } from "./dbClient";
import { NestedQuery, QueryDetailsResult } from "../queryBuilding/utils";
import {
NestedQuery,
QueryDetailsResult,
QueryUpdateResult,
} from "../queryBuilding/utils";
import { DibbsValueSet } from "../constants";
import { DEFAULT_TIME_WINDOW } from "../utils";
import { randomUUID } from "crypto";
Expand Down Expand Up @@ -56,7 +60,7 @@ export async function saveCustomQuery(
query_data = EXCLUDED.query_data,
author = EXCLUDED.author,
date_last_modified = EXCLUDED.date_last_modified
RETURNING id, query_name;
RETURNING id, query_name, CASE WHEN xmax = 0 THEN 'INSERT' ELSE 'UPDATE' END AS operation;
`;
const { queryDataInsert, conditionInsert } =
formatQueryDataForDatabase(queryInput);
Expand All @@ -76,7 +80,7 @@ export async function saveCustomQuery(
];
const result = await dbClient.query(queryString, dataToWrite);
if (result.rows.length > 0) {
return result.rows as unknown as QueryDetailsResult[];
return result.rows as QueryUpdateResult[];
}
console.error("Query save failed:", dataToWrite);
return [];
Expand Down
23 changes: 11 additions & 12 deletions query-connector/src/app/database-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
CustomUserQuery,
} from "./query-building";
import {
CategoryToConditionToNameMap,
ConditionIdToNameMap,
CategoryToConditionArrayMap,
ConditionsMap,
} from "./queryBuilding/utils";
import {
CategoryStruct,
Expand Down Expand Up @@ -489,25 +489,24 @@ export async function getConditionsData() {
const rows = result.rows;

// 1. Grouped by category with id:name pairs
const categoryToConditionArrayMap: CategoryToConditionToNameMap = rows.reduce(
(acc, row) => {
const categoryToConditionNameArrayMap: CategoryToConditionArrayMap =
rows.reduce((acc, row) => {
const { category, id, name } = row;
if (!acc[category]) {
acc[category] = [];
}
acc[category].push({ [id]: name });
acc[category].push({ id: id, name: name });
return acc;
},
{} as CategoryToConditionToNameMap,
);
}, {} as CategoryToConditionArrayMap);

// 2. ID-Name mapping
const conditionIdToNameMap: ConditionIdToNameMap = rows.reduce((acc, row) => {
acc[row.id] = row.name;
const conditionIdToNameMap: ConditionsMap = rows.reduce((acc, row) => {
acc[row.id] = { name: row.name, category: row.category };
return acc;
}, {} as ConditionIdToNameMap);
}, {} as ConditionsMap);

return {
categoryToConditionArrayMap,
categoryToConditionNameArrayMap,
conditionIdToNameMap,
} as const;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type DrawerProps = {
onSave: () => void;
onClose: () => void;
onSearch?: () => void;
hasChanges: boolean;
};

/**
Expand Down
Loading

0 comments on commit f87166b

Please sign in to comment.