Skip to content

Commit

Permalink
Merge branch 'bob/que-29-define-backend-query-creation-spec-and-funct…
Browse files Browse the repository at this point in the history
…ions' of https://github.com/CDCgov/dibbs-query-connector into bob/que-29-define-backend-query-creation-spec-and-functions
  • Loading branch information
fzhao99 committed Oct 30, 2024
2 parents fdb7ce7 + 361db79 commit effdf69
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions query-connector/src/app/database-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,25 @@ function generateValueSetSqlPromise(vs: ValueSet) {
// to update the ID scheme to have something more generically defined that
// don't rely on potentially null external ID values.
const valueSetUniqueId = `${valueSetOid}_${vs.valueSetVersion}`;
const insertValueSetSql =
"INSERT INTO valuesets VALUES($1,$2,$3,$4,$5,$6) RETURNING id;";

// In the event a duplicate value set by OID + Version is entered, simply
// update the existing one to have the new set of information
// ValueSets are already uniquely identified by OID + V so this just allows
// us to proceed with DB creation in the event a duplicate VS from another
// group is pulled and loaded
const insertValueSetSql = `
INSERT INTO valuesets
VALUES($1,$2,$3,$4,$5,$6)
ON CONFLICT(id)
DO UPDATE SET
id = EXCLUDED.id,
oid = EXCLUDED.oid,
version = EXCLUDED.version,
name = EXCLUDED.name,
author = EXCLUDED.author,
type = EXCLUDED.type
RETURNING id;
`;
const valuesArray = [
valueSetUniqueId,
valueSetOid,
Expand All @@ -306,7 +323,22 @@ function generateConceptSqlPromises(vs: ValueSet) {
const insertConceptsSqlArray = vs.concepts.map((concept) => {
const systemPrefix = stripProtocolAndTLDFromSystemUrl(vs.system);
const conceptUniqueId = `${systemPrefix}_${concept.code}`;
const insertConceptSql = `INSERT INTO concepts VALUES($1,$2,$3,$4,$5,$6) RETURNING id;`;

// Duplicate value set insertion is likely to percolate to the concept level
// Apply the same logic of overwriting if unique keys are the same
const insertConceptSql = `
INSERT INTO concepts
VALUES($1,$2,$3,$4,$5,$6)
ON CONFLICT(id)
DO UPDATE SET
id = EXCLUDED.id,
code = EXCLUDED.code,
code_system = EXCLUDED.code_system,
display = EXCLUDED.display,
gem_formatted_code = EXCLUDED.gem_formatted_code,
version = EXCLUDED.version
RETURNING id;
`;
const conceptInsertPromise = dbClient.query(insertConceptSql, [
conceptUniqueId,
concept.code,
Expand All @@ -328,7 +360,20 @@ function generateValuesetConceptJoinSqlPromises(vs: ValueSet) {
const insertConceptsSqlArray = vs.concepts.map((concept) => {
const systemPrefix = stripProtocolAndTLDFromSystemUrl(vs.system);
const conceptUniqueId = `${systemPrefix}_${concept.code}`;
const insertJoinSql = `INSERT INTO valueset_to_concept VALUES($1,$2, $3) RETURNING valueset_id, concept_id;`;

// Last place to make an overwriting upsert adjustment
// Even if the duplicate entries have the same data, PG will attempt to
// insert another row, so just make that upsert the relationship
const insertJoinSql = `
INSERT INTO valueset_to_concept
VALUES($1,$2,$3)
ON CONFLICT(id)
DO UPDATE SET
id = EXCLUDED.id,
valueset_id = EXCLUDED.valueset_id,
concept_id = EXCLUDED.concept_id
RETURNING valueset_id, concept_id;
`;
const conceptInsertPromise = dbClient.query(insertJoinSql, [
`${valueSetUniqueId}_${conceptUniqueId}`,
valueSetUniqueId,
Expand Down

0 comments on commit effdf69

Please sign in to comment.