Skip to content

Commit

Permalink
Add function to check whether valueset data exists in the DB (#152)
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
m-goggins and pre-commit-ci[bot] authored Nov 13, 2024
1 parent 161104f commit 73d097d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
20 changes: 20 additions & 0 deletions query-connector/src/app/database-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,3 +605,23 @@ export async function getConditionsData() {
conditionIdToNameMap,
} as const;
}

/**
* Checks the database to see if data has been loaded into the valuesets table by
* estmating the number of rows in the table. If the estimated count is greater than
* 0, the function returns true, otherwise false.
* @returns A boolean indicating whether the valuesets table has data.
*/
export async function checkDBForData() {
const query = `
SELECT reltuples AS estimated_count
FROM pg_class
WHERE relname = 'valuesets';
`;
const result = await dbClient.query(query);

// Return true if the estimated count > 0, otherwise false
return (
result.rows.length > 0 && parseFloat(result.rows[0].estimated_count) > 0
);
}
1 change: 0 additions & 1 deletion query-connector/src/app/fhir-servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ class FHIRClient {

async get(path: string): Promise<Response> {
try {
console.log("FHIR Server: ", this.hostname);
return fetch(this.hostname + path, this.init);
} catch (error) {
console.error(error);
Expand Down
16 changes: 12 additions & 4 deletions query-connector/src/db-creation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { ersdToDibbsConceptMap, ErsdConceptType } from "@/app/constants";
import { Bundle, BundleEntry, ValueSet } from "fhir/r4";
import {
checkDBForData,
checkValueSetInsertion,
getERSD,
getVSACValueSet,
Expand Down Expand Up @@ -178,10 +179,17 @@ async function fetchBatchValueSetsFromVsac(oidData: OidData, batchSize = 100) {
* the eRSD, extracting OIDs, then inserting valuesets into the DB.
*/
export async function createDibbsDB() {
const ersdOidData = await getOidsFromErsd();
if (ersdOidData) {
await fetchBatchValueSetsFromVsac(ersdOidData);
// Check if the DB already contains valuesets
const dbHasData = await checkDBForData();

if (!dbHasData) {
const ersdOidData = await getOidsFromErsd();
if (ersdOidData) {
await fetchBatchValueSetsFromVsac(ersdOidData);
} else {
console.error("Could not load eRSD, aborting DIBBs DB creation");
}
} else {
console.error("Could not load eRSD, aborting DIBBs DB creation");
console.log("Database already has data; skipping DIBBs DB creation.");
}
}

0 comments on commit 73d097d

Please sign in to comment.