11import PostgrestTransformBuilder from './PostgrestTransformBuilder'
2+ import { JsonPathToAccessor , JsonPathToType } from './select-query-parser/utils'
23import { GenericSchema } from './types'
34
45type FilterOperator =
@@ -25,6 +26,10 @@ type FilterOperator =
2526 | 'phfts'
2627 | 'wfts'
2728
29+ export type IsStringOperator < Path extends string > = Path extends `${string } ->>${string } `
30+ ? true
31+ : false
32+
2833// Match relationship filters with `table.column` syntax and resolve underlying
2934// column value. If not matched, fallback to generic type.
3035// TODO: Validate the relationship itself ala select-query-parser. Currently we
@@ -40,6 +45,14 @@ type ResolveFilterValue<
4045 : ResolveFilterRelationshipValue < Schema , RelationshipTable , Remainder >
4146 : ColumnName extends keyof Row
4247 ? Row [ ColumnName ]
48+ : // If the column selection is a jsonpath like `data->value` or `data->>value` we attempt to match
49+ // the expected type with the parsed custom json type
50+ IsStringOperator < ColumnName > extends true
51+ ? string
52+ : JsonPathToType < Row , JsonPathToAccessor < ColumnName > > extends infer JsonPathValue
53+ ? JsonPathValue extends never
54+ ? never
55+ : JsonPathValue
4356 : never
4457
4558type ResolveFilterRelationshipValue <
@@ -75,7 +88,12 @@ export default class PostgrestFilterBuilder<
7588 column : ColumnName ,
7689 value : ResolveFilterValue < Schema , Row , ColumnName > extends never
7790 ? NonNullable < unknown >
78- : NonNullable < ResolveFilterValue < Schema , Row , ColumnName > >
91+ : // We want to infer the type before wrapping it into a `NonNullable` to avoid too deep
92+ // type resolution error
93+ ResolveFilterValue < Schema , Row , ColumnName > extends infer ResolvedFilterValue
94+ ? NonNullable < ResolvedFilterValue >
95+ : // We should never enter this case as all the branches are covered above
96+ never
7997 ) : this {
8098 this . url . searchParams . append ( column , `eq.${ value } ` )
8199 return this
@@ -91,7 +109,9 @@ export default class PostgrestFilterBuilder<
91109 column : ColumnName ,
92110 value : ResolveFilterValue < Schema , Row , ColumnName > extends never
93111 ? unknown
94- : ResolveFilterValue < Schema , Row , ColumnName >
112+ : ResolveFilterValue < Schema , Row , ColumnName > extends infer ResolvedFilterValue
113+ ? ResolvedFilterValue
114+ : never
95115 ) : this {
96116 this . url . searchParams . append ( column , `neq.${ value } ` )
97117 return this
@@ -269,9 +289,16 @@ export default class PostgrestFilterBuilder<
269289 */
270290 in < ColumnName extends string > (
271291 column : ColumnName ,
272- values : ResolveFilterValue < Schema , Row , ColumnName > extends never
273- ? unknown [ ]
274- : ReadonlyArray < ResolveFilterValue < Schema , Row , ColumnName > >
292+ values : ReadonlyArray <
293+ ResolveFilterValue < Schema , Row , ColumnName > extends never
294+ ? unknown
295+ : // We want to infer the type before wrapping it into a `NonNullable` to avoid too deep
296+ // type resolution error
297+ ResolveFilterValue < Schema , Row , ColumnName > extends infer ResolvedFilterValue
298+ ? ResolvedFilterValue
299+ : // We should never enter this case as all the branches are covered above
300+ never
301+ >
275302 ) : this {
276303 const cleanedValues = Array . from ( new Set ( values ) )
277304 . map ( ( s ) => {
0 commit comments