8
8
RowSet ,
9
9
Forbidden ,
10
10
Conflict ,
11
- Relationship
11
+ Relationship ,
12
+ ObjectField ,
13
+ Type
12
14
} from "@hasura/ndc-sdk-typescript" ;
13
15
import { Configuration , State } from ".." ;
14
16
const SqlString = require ( "sqlstring-sqlite" ) ;
@@ -18,16 +20,27 @@ import { MAX_32_INT } from "../constants";
18
20
const escape_single = ( s : any ) => SqlString . escape ( s ) ;
19
21
const escape_double = ( s : any ) => `"${ SqlString . escape ( s ) . slice ( 1 , - 1 ) } "` ;
20
22
21
- function getColumnExpression ( field_def : any , collection_alias : string , column : string ) : string {
23
+ function getColumnExpression ( field_def : ObjectField , collection_alias : string , column : string ) : string {
22
24
// Helper function to handle the actual type
23
- function handleNamedType ( type : any ) : string {
24
- if ( type . name === "BigInt" ) {
25
- return `CAST(${ escape_double ( collection_alias ) } .${ escape_double ( column ) } AS TEXT)` ;
25
+ function handleNamedType ( type : Type ) : string {
26
+ if ( type . type != "named" ) {
27
+ throw new Forbidden ( "Named type must be named type" , { } ) ;
28
+ }
29
+ switch ( type . name ) {
30
+ case "BigInt" :
31
+ return `CAST(${ escape_double ( collection_alias ) } .${ escape_double ( column ) } AS TEXT)` ;
32
+ case "UBigInt" :
33
+ return `CAST(${ escape_double ( collection_alias ) } .${ escape_double ( column ) } AS TEXT)` ;
34
+ case "HugeInt" :
35
+ return `CAST(${ escape_double ( collection_alias ) } .${ escape_double ( column ) } AS TEXT)` ;
36
+ case "UHugeInt" :
37
+ return `CAST(${ escape_double ( collection_alias ) } .${ escape_double ( column ) } AS TEXT)` ;
38
+ default :
39
+ return `${ escape_double ( collection_alias ) } .${ escape_double ( column ) } ` ;
26
40
}
27
- return `${ escape_double ( collection_alias ) } .${ escape_double ( column ) } ` ;
28
41
}
29
42
// Helper function to traverse the type structure
30
- function processType ( type : any ) : string {
43
+ function processType ( type : Type ) : string {
31
44
if ( type . type === "nullable" ) {
32
45
if ( type . underlying_type . type === "named" ) {
33
46
return handleNamedType ( type . underlying_type ) ;
@@ -56,7 +69,7 @@ function isTimestampType(field_def: any): boolean {
56
69
if ( type . type === "nullable" ) {
57
70
return checkType ( type . underlying_type ) ;
58
71
}
59
- return type . type === "named" && type . name === "Timestamp" ;
72
+ return type . type === "named" && ( type . name === "Timestamp" || type . name === "TimestampTz" ) ;
60
73
}
61
74
62
75
return checkType ( field_def . type ) ;
@@ -82,7 +95,6 @@ function getIntegerType(field_def: any): string | null {
82
95
83
96
return checkType ( field_def . type ) ;
84
97
}
85
-
86
98
function getRhsExpression ( type : string | null ) : string {
87
99
if ( ! type ) return "?" ;
88
100
return `CAST(? AS ${ type } )` ;
@@ -338,7 +350,10 @@ function build_query(
338
350
collect_rows . push ( escape_single ( field_name ) ) ;
339
351
switch ( field_value . type ) {
340
352
case "column" :
341
- const object_type = config . config ?. object_types [ query_request . collection ] ;
353
+ const current_collection = path . length > 1 && relationship_key
354
+ ? query_request . collection_relationships [ relationship_key ] . target_collection
355
+ : query_request . collection ;
356
+ const object_type = config . config ?. object_types [ current_collection ] ;
342
357
let field_def = object_type . fields [ field_value . column ] ;
343
358
collect_rows . push ( getColumnExpression ( field_def , collection_alias , field_value . column ) ) ;
344
359
break ;
@@ -397,7 +412,7 @@ function build_query(
397
412
case "column" :
398
413
if ( elem . target . path . length === 0 ) {
399
414
order_elems . push (
400
- `${ escape_double ( collection_alias ) } .${ escape_double ( elem . target . name ) } ${ elem . order_direction } `
415
+ `LOWER( ${ escape_double ( collection_alias ) } .${ escape_double ( elem . target . name ) } ) ${ elem . order_direction } `
401
416
) ;
402
417
} else {
403
418
let currentAlias = collection_alias ;
0 commit comments