Skip to content

Commit

Permalink
fix(parser): revert error unwrapping (#566)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
avallete authored Oct 28, 2024
1 parent ad1c533 commit e603768
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 48 deletions.
13 changes: 5 additions & 8 deletions src/select-query-parser/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
IsRelationNullable,
ResolveRelationship,
SelectQueryError,
UnwrapErrorMessages,
} from './utils'

/**
Expand All @@ -36,18 +35,16 @@ export type GetResult<
Query extends string
> = Relationships extends null // For .rpc calls the passed relationships will be null in that case, the result will always be the function return type
? ParseQuery<Query> extends infer ParsedQuery extends Ast.Node[]
? UnwrapErrorMessages<
RPCCallNodes<ParsedQuery, RelationName extends string ? RelationName : 'rpc_call', Row>
>
? RPCCallNodes<ParsedQuery, RelationName extends string ? RelationName : 'rpc_call', Row>
: Row
: ParseQuery<Query> extends infer ParsedQuery
? ParsedQuery extends Ast.Node[]
? RelationName extends string
? Relationships extends GenericRelationship[]
? UnwrapErrorMessages<ProcessNodes<Schema, Row, RelationName, Relationships, ParsedQuery>>
: UnwrapErrorMessages<SelectQueryError<'Invalid Relationships cannot infer result type'>>
: UnwrapErrorMessages<SelectQueryError<'Invalid RelationName cannot infer result type'>>
: UnwrapErrorMessages<ParsedQuery>
? ProcessNodes<Schema, Row, RelationName, Relationships, ParsedQuery>
: SelectQueryError<'Invalid Relationships cannot infer result type'>
: SelectQueryError<'Invalid RelationName cannot infer result type'>
: ParsedQuery
: never

/**
Expand Down
33 changes: 6 additions & 27 deletions src/select-query-parser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,6 @@ import {

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

type RequireHintingSelectQueryError<
DistantName extends string,
RelationName extends string
> = SelectQueryError<`Could not embed because more than one relationship was found for '${DistantName}' and '${RelationName}' you need to hint the column with ${DistantName}!<columnName> ?`>

export type UnwrapErrorMessages<T> = T extends SelectQueryError<infer M>
? M
: T extends SelectQueryError<infer M>[]
? M[]
: T extends (infer U)[]
? UnwrapErrorMessages<U>[]
: T extends Record<string, unknown>
? { [K in keyof T]: UnwrapErrorMessages<T[K]> }
: T

export type GetFieldNodeResultName<Field extends Ast.FieldNode> = Field['alias'] extends string
? Field['alias']
: Field['aggregateFunction'] extends AggregateFunctions
Expand Down Expand Up @@ -161,7 +146,7 @@ type HasMultipleFKeysToFRel<FRelName, Relationships> = Relationships extends [
type CheckRelationshipError<
Schema extends GenericSchema,
Relationships extends GenericRelationship[],
CurrentTableOrView extends keyof TablesAndViews<Schema>,
CurrentTableOrView extends keyof TablesAndViews<Schema> & string,
FoundRelation
> = FoundRelation extends SelectQueryError<string>
? FoundRelation
Expand All @@ -176,10 +161,7 @@ type CheckRelationshipError<
? // We check if there is possible confusion with other relations with this table
HasMultipleFKeysToFRel<RelatedRelationName, Relationships> extends true
? // If there is, postgrest will fail at runtime, and require desambiguation via hinting
RequireHintingSelectQueryError<
RelatedRelationName,
CurrentTableOrView extends string ? CurrentTableOrView : 'unknown'
>
SelectQueryError<`Could not embed because more than one relationship was found for '${RelatedRelationName}' and '${CurrentTableOrView}' you need to hint the column with ${RelatedRelationName}!<columnName> ?`>
: FoundRelation
: // Same check for forward relationships, but we must gather the relationships from the found relation
FoundRelation extends {
Expand All @@ -188,13 +170,13 @@ type CheckRelationshipError<
name: string
}
direction: 'forward'
from: infer From extends keyof TablesAndViews<Schema>
from: infer From extends keyof TablesAndViews<Schema> & string
}
? HasMultipleFKeysToFRel<
RelatedRelationName,
TablesAndViews<Schema>[From]['Relationships']
> extends true
? RequireHintingSelectQueryError<From extends string ? From : 'unknown', RelatedRelationName>
? SelectQueryError<`Could not embed because more than one relationship was found for '${From}' and '${RelatedRelationName}' you need to hint the column with ${From}!<columnName> ?`>
: FoundRelation
: FoundRelation

Expand Down Expand Up @@ -229,7 +211,7 @@ type ResolveReverseRelationship<
Schema extends GenericSchema,
Relationships extends GenericRelationship[],
Field extends Ast.FieldNode,
CurrentTableOrView extends keyof TablesAndViews<Schema>
CurrentTableOrView extends keyof TablesAndViews<Schema> & string
> = FindFieldMatchingRelationships<Schema, Relationships, Field> extends infer FoundRelation
? FoundRelation extends never
? false
Expand All @@ -245,10 +227,7 @@ type ResolveReverseRelationship<
}
: // If the relation was found via implicit relation naming, we must ensure there is no conflicting matches
HasMultipleFKeysToFRel<RelatedRelationName, Relationships> extends true
? RequireHintingSelectQueryError<
RelatedRelationName,
CurrentTableOrView extends string ? CurrentTableOrView : 'unknown'
>
? SelectQueryError<`Could not embed because more than one relationship was found for '${RelatedRelationName}' and '${CurrentTableOrView}' you need to hint the column with ${RelatedRelationName}!<columnName> ?`>
: {
referencedTable: TablesAndViews<Schema>[RelatedRelationName]
relation: FoundRelation
Expand Down
2 changes: 1 addition & 1 deletion test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const postgrest = new PostgrestClient<Database>(REST_URL)
}
// getting this w/o the cast, not sure why:
// Parameter type Json is declared too wide for argument type Json
expectType<Json>(data.bar as Json)
expectType<Json>(data.bar)
expectType<string>(data.baz)
}

Expand Down
25 changes: 13 additions & 12 deletions test/select-query-parser/select.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expectType } from 'tsd'
import { TypeEqual } from 'ts-expect'
import { Json } from '../../src/select-query-parser/types'
import { SelectQueryError } from '../../src/select-query-parser/utils'
import { Prettify } from '../../src/types'
import { Database } from '../types'
import { selectQueries } from '../relationships'
Expand Down Expand Up @@ -197,9 +198,9 @@ type Schema = Database['public']
const { data } = await selectQueries.joinOneToOneWithNullablesNoHint.limit(1).single()
let result: Exclude<typeof data, null>
let expected: {
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> ?"
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> ?"
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> ?"
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> ?">
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> ?">
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> ?">
}
expectType<TypeEqual<typeof result, typeof expected>>(true)
}
Expand All @@ -221,9 +222,9 @@ type Schema = Database['public']
const { data } = await selectQueries.joinOneToManyWithNullablesNoHint.limit(1).single()
let result: Exclude<typeof data, null>
let expected: {
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> ?"
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> ?"
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> ?"
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> ?">
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> ?">
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> ?">
}
expectType<TypeEqual<typeof result, typeof expected>>(true)
}
Expand Down Expand Up @@ -270,7 +271,7 @@ type Schema = Database['public']
id: number
second_user: string
third_wheel: string | null
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> ?"
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> ?">
}>
second_friend_of: Array<Database['public']['Tables']['best_friends']['Row']>
third_wheel_of: Array<Database['public']['Tables']['best_friends']['Row']>
Expand Down Expand Up @@ -439,7 +440,7 @@ type Schema = Database['public']
let result: Exclude<typeof data, null>
let expected: {
username: string
messages: "column 'sum' does not exist on 'messages'."[]
messages: SelectQueryError<"column 'sum' does not exist on 'messages'.">[]
}
expectType<TypeEqual<typeof result, typeof expected>>(true)
}
Expand Down Expand Up @@ -629,7 +630,7 @@ type Schema = Database['public']
const { data } = await selectQueries.joinSelectViaColumnHintTwice.limit(1).single()
let result: Exclude<typeof data, null>
let expected: {
users: 'table "best_friends" specified more than once use hinting for desambiguation'
users: SelectQueryError<'table "best_friends" specified more than once use hinting for desambiguation'>
}
expectType<TypeEqual<typeof result, typeof expected>>(true)
}
Expand All @@ -640,7 +641,7 @@ type Schema = Database['public']
let result: Exclude<typeof data, null>
let expected: {
id: number
messages: '"channels" and "messages" do not form a many-to-one or one-to-one relationship spread not possible'
messages: SelectQueryError<'"channels" and "messages" do not form a many-to-one or one-to-one relationship spread not possible'>
}
expectType<TypeEqual<typeof result, typeof expected>>(true)
}
Expand Down Expand Up @@ -683,7 +684,7 @@ type Schema = Database['public']
{
const { data } = await selectQueries.typecastingAndAggregate.limit(1).single()
let result: Exclude<typeof data, null>
let expected: `column 'users' does not exist on 'messages'.`
let expected: SelectQueryError<`column 'users' does not exist on 'messages'.`>
expectType<TypeEqual<typeof result, typeof expected>>(true)
}

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

// many-to-many with join table
Expand Down

0 comments on commit e603768

Please sign in to comment.