-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
define backend query building insert function (#89)
- Loading branch information
Showing
8 changed files
with
178 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { UUID, randomUUID } from "crypto"; | ||
import { Concept, ValueSet } from "./constants"; | ||
|
||
// TODO: Potentially merge this / infer this from the type created via the | ||
// database creation workstream | ||
export type UserQueryInput = { | ||
queryName: string; | ||
author: string; | ||
valueSets: ValueSet[]; | ||
concepts?: Concept[]; | ||
timeWindowUnit?: string; // TODO: should probably type this more strongly | ||
timeWindowNumber?: Number; | ||
}; | ||
|
||
const DEFAULT_TIME_WINDOW = { | ||
timeWindowNumber: 1, | ||
timeWindowUnit: "day", | ||
}; | ||
|
||
/** | ||
* Function that generates SQL needed for the query building flow | ||
* @param input - Values of the shape UserQueryInput needed for query insertion | ||
* @returns [sql, values] needed for query building insertion | ||
*/ | ||
export function generateQueryInsertionSql(input: UserQueryInput) { | ||
const id = randomUUID(); | ||
const dateCreated = new Date().toISOString(); | ||
const dateLastModified = new Date().toISOString(); | ||
|
||
const sql = | ||
"INSERT INTO query VALUES($1,$2,$3,$4,$5,$6,$7) RETURNING id, query_name;"; | ||
|
||
const values = [ | ||
id, | ||
input.queryName, | ||
input.author, | ||
dateCreated, | ||
dateLastModified, | ||
input?.timeWindowNumber ?? DEFAULT_TIME_WINDOW.timeWindowNumber, | ||
input?.timeWindowUnit ?? DEFAULT_TIME_WINDOW.timeWindowUnit, | ||
]; | ||
|
||
return { sql: sql, values: values } as const; | ||
} | ||
|
||
/** | ||
* Function that generates SQL for the query_to_valueset join table needed for | ||
* query building. | ||
* @param input - Values of the shape UserQueryInput needed for query insertion | ||
* @param queryId - ID of the query that's already been created to associate with | ||
* a given valueset | ||
* @returns An array of {sql, values} to be inserted by the join insertion flow | ||
*/ | ||
export function generateQueryToValueSetInsertionSql( | ||
input: UserQueryInput, | ||
queryId: UUID, | ||
) { | ||
const joinInsertionSqlArray = input.valueSets.map((v) => { | ||
const sql = | ||
"INSERT INTO query_to_valueset VALUES($1,$2,$3,$4) RETURNING query_id, valueset_id;"; | ||
const queryToValueSetId = `${queryId}_${v.valueSetId}`; | ||
const values = [ | ||
queryToValueSetId, | ||
queryId, | ||
v.valueSetId, | ||
v.valueSetExternalId, | ||
]; | ||
|
||
return { sql: sql, values: values } as const; | ||
}); | ||
return joinInsertionSqlArray; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 8 additions & 25 deletions
33
query-connector/src/app/query/components/selectQuery/queryHooks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters