Skip to content

Commit e603768

Browse files
authored
fix(parser): revert error unwrapping (#566)
* Revert "fix: unwrap error within arrays" This reverts commit e31b1ff. * Revert "fix(result): error plain string results unwrapping" This reverts commit b4e587a. * fix: remove unecessary cast * fix: remove intermediate type for better end result
1 parent ad1c533 commit e603768

File tree

4 files changed

+25
-48
lines changed

4 files changed

+25
-48
lines changed

src/select-query-parser/result.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
IsRelationNullable,
1717
ResolveRelationship,
1818
SelectQueryError,
19-
UnwrapErrorMessages,
2019
} from './utils'
2120

2221
/**
@@ -36,18 +35,16 @@ export type GetResult<
3635
Query extends string
3736
> = Relationships extends null // For .rpc calls the passed relationships will be null in that case, the result will always be the function return type
3837
? ParseQuery<Query> extends infer ParsedQuery extends Ast.Node[]
39-
? UnwrapErrorMessages<
40-
RPCCallNodes<ParsedQuery, RelationName extends string ? RelationName : 'rpc_call', Row>
41-
>
38+
? RPCCallNodes<ParsedQuery, RelationName extends string ? RelationName : 'rpc_call', Row>
4239
: Row
4340
: ParseQuery<Query> extends infer ParsedQuery
4441
? ParsedQuery extends Ast.Node[]
4542
? RelationName extends string
4643
? Relationships extends GenericRelationship[]
47-
? UnwrapErrorMessages<ProcessNodes<Schema, Row, RelationName, Relationships, ParsedQuery>>
48-
: UnwrapErrorMessages<SelectQueryError<'Invalid Relationships cannot infer result type'>>
49-
: UnwrapErrorMessages<SelectQueryError<'Invalid RelationName cannot infer result type'>>
50-
: UnwrapErrorMessages<ParsedQuery>
44+
? ProcessNodes<Schema, Row, RelationName, Relationships, ParsedQuery>
45+
: SelectQueryError<'Invalid Relationships cannot infer result type'>
46+
: SelectQueryError<'Invalid RelationName cannot infer result type'>
47+
: ParsedQuery
5148
: never
5249

5350
/**

src/select-query-parser/utils.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,6 @@ import {
1212

1313
export type SelectQueryError<Message extends string> = { error: true } & Message
1414

15-
type RequireHintingSelectQueryError<
16-
DistantName extends string,
17-
RelationName extends string
18-
> = SelectQueryError<`Could not embed because more than one relationship was found for '${DistantName}' and '${RelationName}' you need to hint the column with ${DistantName}!<columnName> ?`>
19-
20-
export type UnwrapErrorMessages<T> = T extends SelectQueryError<infer M>
21-
? M
22-
: T extends SelectQueryError<infer M>[]
23-
? M[]
24-
: T extends (infer U)[]
25-
? UnwrapErrorMessages<U>[]
26-
: T extends Record<string, unknown>
27-
? { [K in keyof T]: UnwrapErrorMessages<T[K]> }
28-
: T
29-
3015
export type GetFieldNodeResultName<Field extends Ast.FieldNode> = Field['alias'] extends string
3116
? Field['alias']
3217
: Field['aggregateFunction'] extends AggregateFunctions
@@ -161,7 +146,7 @@ type HasMultipleFKeysToFRel<FRelName, Relationships> = Relationships extends [
161146
type CheckRelationshipError<
162147
Schema extends GenericSchema,
163148
Relationships extends GenericRelationship[],
164-
CurrentTableOrView extends keyof TablesAndViews<Schema>,
149+
CurrentTableOrView extends keyof TablesAndViews<Schema> & string,
165150
FoundRelation
166151
> = FoundRelation extends SelectQueryError<string>
167152
? FoundRelation
@@ -176,10 +161,7 @@ type CheckRelationshipError<
176161
? // We check if there is possible confusion with other relations with this table
177162
HasMultipleFKeysToFRel<RelatedRelationName, Relationships> extends true
178163
? // If there is, postgrest will fail at runtime, and require desambiguation via hinting
179-
RequireHintingSelectQueryError<
180-
RelatedRelationName,
181-
CurrentTableOrView extends string ? CurrentTableOrView : 'unknown'
182-
>
164+
SelectQueryError<`Could not embed because more than one relationship was found for '${RelatedRelationName}' and '${CurrentTableOrView}' you need to hint the column with ${RelatedRelationName}!<columnName> ?`>
183165
: FoundRelation
184166
: // Same check for forward relationships, but we must gather the relationships from the found relation
185167
FoundRelation extends {
@@ -188,13 +170,13 @@ type CheckRelationshipError<
188170
name: string
189171
}
190172
direction: 'forward'
191-
from: infer From extends keyof TablesAndViews<Schema>
173+
from: infer From extends keyof TablesAndViews<Schema> & string
192174
}
193175
? HasMultipleFKeysToFRel<
194176
RelatedRelationName,
195177
TablesAndViews<Schema>[From]['Relationships']
196178
> extends true
197-
? RequireHintingSelectQueryError<From extends string ? From : 'unknown', RelatedRelationName>
179+
? SelectQueryError<`Could not embed because more than one relationship was found for '${From}' and '${RelatedRelationName}' you need to hint the column with ${From}!<columnName> ?`>
198180
: FoundRelation
199181
: FoundRelation
200182

@@ -229,7 +211,7 @@ type ResolveReverseRelationship<
229211
Schema extends GenericSchema,
230212
Relationships extends GenericRelationship[],
231213
Field extends Ast.FieldNode,
232-
CurrentTableOrView extends keyof TablesAndViews<Schema>
214+
CurrentTableOrView extends keyof TablesAndViews<Schema> & string
233215
> = FindFieldMatchingRelationships<Schema, Relationships, Field> extends infer FoundRelation
234216
? FoundRelation extends never
235217
? false
@@ -245,10 +227,7 @@ type ResolveReverseRelationship<
245227
}
246228
: // If the relation was found via implicit relation naming, we must ensure there is no conflicting matches
247229
HasMultipleFKeysToFRel<RelatedRelationName, Relationships> extends true
248-
? RequireHintingSelectQueryError<
249-
RelatedRelationName,
250-
CurrentTableOrView extends string ? CurrentTableOrView : 'unknown'
251-
>
230+
? SelectQueryError<`Could not embed because more than one relationship was found for '${RelatedRelationName}' and '${CurrentTableOrView}' you need to hint the column with ${RelatedRelationName}!<columnName> ?`>
252231
: {
253232
referencedTable: TablesAndViews<Schema>[RelatedRelationName]
254233
relation: FoundRelation

test/index.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ const postgrest = new PostgrestClient<Database>(REST_URL)
9999
}
100100
// getting this w/o the cast, not sure why:
101101
// Parameter type Json is declared too wide for argument type Json
102-
expectType<Json>(data.bar as Json)
102+
expectType<Json>(data.bar)
103103
expectType<string>(data.baz)
104104
}
105105

test/select-query-parser/select.test-d.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expectType } from 'tsd'
22
import { TypeEqual } from 'ts-expect'
33
import { Json } from '../../src/select-query-parser/types'
4+
import { SelectQueryError } from '../../src/select-query-parser/utils'
45
import { Prettify } from '../../src/types'
56
import { Database } from '../types'
67
import { selectQueries } from '../relationships'
@@ -197,9 +198,9 @@ type Schema = Database['public']
197198
const { data } = await selectQueries.joinOneToOneWithNullablesNoHint.limit(1).single()
198199
let result: Exclude<typeof data, null>
199200
let expected: {
200-
first_user: "Could not embed because more than one relationship was found for 'users' and 'best_friends' you need to hint the column with users!<columnName> ?"
201-
second_user: "Could not embed because more than one relationship was found for 'users' and 'best_friends' you need to hint the column with users!<columnName> ?"
202-
third_wheel: "Could not embed because more than one relationship was found for 'users' and 'best_friends' you need to hint the column with users!<columnName> ?"
201+
first_user: SelectQueryError<"Could not embed because more than one relationship was found for 'users' and 'best_friends' you need to hint the column with users!<columnName> ?">
202+
second_user: SelectQueryError<"Could not embed because more than one relationship was found for 'users' and 'best_friends' you need to hint the column with users!<columnName> ?">
203+
third_wheel: SelectQueryError<"Could not embed because more than one relationship was found for 'users' and 'best_friends' you need to hint the column with users!<columnName> ?">
203204
}
204205
expectType<TypeEqual<typeof result, typeof expected>>(true)
205206
}
@@ -221,9 +222,9 @@ type Schema = Database['public']
221222
const { data } = await selectQueries.joinOneToManyWithNullablesNoHint.limit(1).single()
222223
let result: Exclude<typeof data, null>
223224
let expected: {
224-
first_friend_of: "Could not embed because more than one relationship was found for 'best_friends' and 'users' you need to hint the column with best_friends!<columnName> ?"
225-
second_friend_of: "Could not embed because more than one relationship was found for 'best_friends' and 'users' you need to hint the column with best_friends!<columnName> ?"
226-
third_wheel_of: "Could not embed because more than one relationship was found for 'best_friends' and 'users' you need to hint the column with best_friends!<columnName> ?"
225+
first_friend_of: SelectQueryError<"Could not embed because more than one relationship was found for 'best_friends' and 'users' you need to hint the column with best_friends!<columnName> ?">
226+
second_friend_of: SelectQueryError<"Could not embed because more than one relationship was found for 'best_friends' and 'users' you need to hint the column with best_friends!<columnName> ?">
227+
third_wheel_of: SelectQueryError<"Could not embed because more than one relationship was found for 'best_friends' and 'users' you need to hint the column with best_friends!<columnName> ?">
227228
}
228229
expectType<TypeEqual<typeof result, typeof expected>>(true)
229230
}
@@ -270,7 +271,7 @@ type Schema = Database['public']
270271
id: number
271272
second_user: string
272273
third_wheel: string | null
273-
first_user: "Could not embed because more than one relationship was found for 'users' and 'best_friends' you need to hint the column with users!<columnName> ?"
274+
first_user: SelectQueryError<"Could not embed because more than one relationship was found for 'users' and 'best_friends' you need to hint the column with users!<columnName> ?">
274275
}>
275276
second_friend_of: Array<Database['public']['Tables']['best_friends']['Row']>
276277
third_wheel_of: Array<Database['public']['Tables']['best_friends']['Row']>
@@ -439,7 +440,7 @@ type Schema = Database['public']
439440
let result: Exclude<typeof data, null>
440441
let expected: {
441442
username: string
442-
messages: "column 'sum' does not exist on 'messages'."[]
443+
messages: SelectQueryError<"column 'sum' does not exist on 'messages'.">[]
443444
}
444445
expectType<TypeEqual<typeof result, typeof expected>>(true)
445446
}
@@ -629,7 +630,7 @@ type Schema = Database['public']
629630
const { data } = await selectQueries.joinSelectViaColumnHintTwice.limit(1).single()
630631
let result: Exclude<typeof data, null>
631632
let expected: {
632-
users: 'table "best_friends" specified more than once use hinting for desambiguation'
633+
users: SelectQueryError<'table "best_friends" specified more than once use hinting for desambiguation'>
633634
}
634635
expectType<TypeEqual<typeof result, typeof expected>>(true)
635636
}
@@ -640,7 +641,7 @@ type Schema = Database['public']
640641
let result: Exclude<typeof data, null>
641642
let expected: {
642643
id: number
643-
messages: '"channels" and "messages" do not form a many-to-one or one-to-one relationship spread not possible'
644+
messages: SelectQueryError<'"channels" and "messages" do not form a many-to-one or one-to-one relationship spread not possible'>
644645
}
645646
expectType<TypeEqual<typeof result, typeof expected>>(true)
646647
}
@@ -683,7 +684,7 @@ type Schema = Database['public']
683684
{
684685
const { data } = await selectQueries.typecastingAndAggregate.limit(1).single()
685686
let result: Exclude<typeof data, null>
686-
let expected: `column 'users' does not exist on 'messages'.`
687+
let expected: SelectQueryError<`column 'users' does not exist on 'messages'.`>
687688
expectType<TypeEqual<typeof result, typeof expected>>(true)
688689
}
689690

@@ -740,7 +741,7 @@ type Schema = Database['public']
740741
{
741742
const { data, error } = await selectQueries.aggregateOnMissingColumnWithAlias.limit(1).single()
742743
if (error) throw error
743-
expectType<`column 'missing_column' does not exist on 'users'.`>(data)
744+
expectType<SelectQueryError<`column 'missing_column' does not exist on 'users'.`>>(data)
744745
}
745746

746747
// many-to-many with join table

0 commit comments

Comments
 (0)