File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -3,19 +3,26 @@ import { QueryExecResult } from "sql.js";
33import safeJsonParse from "./safeJsonParse" ;
44
55function parseQueryResult < T > ( queryResponse : QueryExecResult [ ] ) : T [ ] {
6- const [ { columns, values } ] = queryResponse ;
7- if ( ! values ) {
8- throw new Error ( "No data found" ) ;
6+ const result = queryResponse ?. [ 0 ] ;
7+
8+ if ( ! result || ! result . columns || ! result . values ) {
9+ return [ ] ;
910 }
1011
11- return values . map ( ( entry ) => {
12- return columns . reduce ( ( acc , column , idx ) => {
13- const value = safeJsonParse ( entry [ idx ] ) ;
12+ const { columns, values } = result ;
13+
14+ return values . map ( ( entry ) =>
15+ columns . reduce ( ( acc , column , idx ) => {
16+ const raw = entry [ idx ] ;
17+ const value = safeJsonParse ( raw ) ;
18+
1419 const isStringToNumber =
15- typeof entry [ idx ] === "string" && typeof value === "number" ; // JSON.parse converts strings to numbers
16- return { ...acc , [ column ] : isStringToNumber ? `${ value } ` : value } ;
17- } , { } as T ) ;
18- } , [ ] ) ;
20+ typeof raw === "string" && typeof value === "number" ;
21+
22+ acc [ column as keyof T ] = ( isStringToNumber ? `${ value } ` : value ) as T [ keyof T ] ;
23+ return acc ;
24+ } , { } as T )
25+ ) ;
1926}
2027
2128export default parseQueryResult ;
You can’t perform that action at this time.
0 commit comments