@@ -53,40 +53,47 @@ const RESOURCE_TYPES = [
5353
5454const PROJECT_CONCEPTMAP_PREFIX = "hl7v2-" ;
5555
56+ interface PsqlResultSet {
57+ type : string ;
58+ data ?: Array < Record < string , unknown > > ;
59+ }
60+
5661interface PsqlResponse {
57- result ?: Array < Record < string , unknown > > ;
62+ result ?: PsqlResultSet [ ] ;
5863 error ?: string ;
5964 status : "success" | "error" ;
6065 query : string ;
6166 duration ?: number ;
6267}
6368
69+ function rows ( res : PsqlResponse ) : Array < Record < string , unknown > > {
70+ return res . result ?. [ 0 ] ?. data ?? [ ] ;
71+ }
72+
6473async function psql ( query : string ) : Promise < PsqlResponse > {
65- const response = await aidboxFetch < PsqlResponse [ ] > ( "/$psql" , {
74+ const response = await aidboxFetch < PsqlResponse > ( "/$psql" , {
6675 method : "POST" ,
6776 headers : { "Content-Type" : "application/json" } ,
6877 body : JSON . stringify ( { query } ) ,
6978 } ) ;
70- const first = response [ 0 ] ;
71- if ( ! first ) { throw new Error ( `Empty response from $psql: ${ query } ` ) ; }
7279 // Aidbox reports `status: "success"` with an `error` field when a
7380 // non-SELECT statement produced no rows — treat that as OK.
74- if ( first . status !== "success" ) {
75- throw new Error ( `$psql failed: ${ first . error ?? "unknown" } — ${ query } ` ) ;
81+ if ( response . status !== "success" ) {
82+ throw new Error ( `$psql failed: ${ response . error ?? "unknown" } — ${ query } ` ) ;
7683 }
77- return first ;
84+ return response ;
7885}
7986
8087async function tableExists ( table : string ) : Promise < boolean > {
8188 const res = await psql (
8289 `SELECT 1 FROM pg_tables WHERE tablename = '${ table } ' LIMIT 1` ,
8390 ) ;
84- return ( res . result ?. length ?? 0 ) > 0 ;
91+ return rows ( res ) . length > 0 ;
8592}
8693
8794async function countRows ( table : string ) : Promise < number > {
8895 const res = await psql ( `SELECT count(*)::int AS n FROM "${ table } "` ) ;
89- return ( res . result ?. [ 0 ] ?. n as number | undefined ) ?? 0 ;
96+ return ( rows ( res ) [ 0 ] ?. n as number | undefined ) ?? 0 ;
9097}
9198
9299async function confirm ( ) : Promise < boolean > {
@@ -126,7 +133,7 @@ async function schemaTableExists(
126133 const res = await psql (
127134 `SELECT 1 FROM pg_tables WHERE schemaname = '${ schema } ' AND tablename = '${ table } ' LIMIT 1` ,
128135 ) ;
129- return ( res . result ?. length ?? 0 ) > 0 ;
136+ return rows ( res ) . length > 0 ;
130137}
131138
132139async function truncateProjectConceptMaps ( ) : Promise < number > {
@@ -139,11 +146,10 @@ async function truncateProjectConceptMaps(): Promise<number> {
139146 let deleted = 0 ;
140147
141148 if ( await schemaTableExists ( "far" , "conceptmap" ) ) {
142- const n = (
143- await psql (
144- `SELECT count(*)::int AS n FROM far.conceptmap WHERE id LIKE ${ idPattern } ` ,
145- )
146- ) . result ?. [ 0 ] ?. n as number | undefined ;
149+ const res = await psql (
150+ `SELECT count(*)::int AS n FROM far.conceptmap WHERE id LIKE ${ idPattern } ` ,
151+ ) ;
152+ const n = rows ( res ) [ 0 ] ?. n as number | undefined ;
147153 deleted += n ?? 0 ;
148154
149155 if ( await schemaTableExists ( "far" , "conceptmapelement" ) ) {
0 commit comments