Skip to content

Commit

Permalink
fix: jsonpath filter string operator accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
avallete committed Jan 23, 2025
1 parent b49c660 commit c2112d4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/PostgrestFilterBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ type FilterOperator =
| 'phfts'
| 'wfts'

export type IsStringOperator<Path extends string> = Path extends `${string}->>${string}`
? true
: false

// Match relationship filters with `table.column` syntax and resolve underlying
// column value. If not matched, fallback to generic type.
// TODO: Validate the relationship itself ala select-query-parser. Currently we
Expand All @@ -41,9 +45,11 @@ type ResolveFilterValue<
: ResolveFilterRelationshipValue<Schema, RelationshipTable, Remainder>
: ColumnName extends keyof Row
? Row[ColumnName]
: // If the column selection is a jsonpath like `data->value` we attempt to match
: // If the column selection is a jsonpath like `data->value` or `data->>value` we attempt to match
// the expected type with the parsed custom json type
JsonPathToType<Row, JsonPathToAccessor<ColumnName>> extends infer JsonPathValue
IsStringOperator<ColumnName> extends true
? string
: JsonPathToType<Row, JsonPathToAccessor<ColumnName>> extends infer JsonPathValue
? JsonPathValue extends never
? never
: JsonPathValue
Expand Down
8 changes: 7 additions & 1 deletion test/issue-1354-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,13 @@ const postgrestOverrideTypes = new PostgrestClient<DatabaseOverride>('http://loc
const resIn = await postgrestOverrideTypes
.from('foo')
.select('id, bar, baz')
.in('bar->version', [1, 32])
.in('bar->version', [31])
.single()
await postgrestOverrideTypes
.from('foo')
.select('id, bar, baz')
// the type become a string when using the string json accessor operator
.in('bar->>version', ['something'])
.single()

if (resIn.error) {
Expand Down

0 comments on commit c2112d4

Please sign in to comment.