Skip to content

Commit

Permalink
fix(result): error plain string results unwrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
avallete authored and soedirgo committed Oct 22, 2024
1 parent 90d9b18 commit b4e587a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
13 changes: 8 additions & 5 deletions src/select-query-parser/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
IsRelationNullable,
ResolveRelationship,
SelectQueryError,
UnwrapErrorMessages,
} from './utils'

/**
Expand All @@ -35,16 +36,18 @@ 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[]
? RPCCallNodes<ParsedQuery, RelationName extends string ? RelationName : 'rpc_call', Row>
? UnwrapErrorMessages<
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[]
? ProcessNodes<Schema, Row, RelationName, Relationships, ParsedQuery>
: SelectQueryError<'Invalid Relationships cannot infer result type'>
: SelectQueryError<'Invalid RelationName cannot infer result type'>
: ParsedQuery
? 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>
: never

/**
Expand Down
6 changes: 6 additions & 0 deletions src/select-query-parser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import {
UnionToArray,
} from './types'

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

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

type RequireHintingSelectQueryError<
Expand Down
20 changes: 10 additions & 10 deletions test/select-query-parser/select.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,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: 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> ?">
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> ?"
}
expectType<TypeEqual<typeof result, typeof expected>>(true)
}
Expand All @@ -222,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: 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> ?">
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> ?"
}
expectType<TypeEqual<typeof result, typeof expected>>(true)
}
Expand Down Expand Up @@ -630,7 +630,7 @@ type Schema = Database['public']
const { data } = await selectQueries.joinSelectViaColumnHintTwice.limit(1).single()
let result: Exclude<typeof data, null>
let expected: {
users: SelectQueryError<'table "best_friends" specified more than once use hinting for desambiguation'>
users: 'table "best_friends" specified more than once use hinting for desambiguation'
}
expectType<TypeEqual<typeof result, typeof expected>>(true)
}
Expand All @@ -641,7 +641,7 @@ type Schema = Database['public']
let result: Exclude<typeof data, null>
let expected: {
id: number
messages: SelectQueryError<'"channels" and "messages" do not form a many-to-one or one-to-one relationship spread not possible'>
messages: '"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 @@ -684,7 +684,7 @@ type Schema = Database['public']
{
const { data } = await selectQueries.typecastingAndAggregate.limit(1).single()
let result: Exclude<typeof data, null>
let expected: SelectQueryError<`column 'users' does not exist on 'messages'.`>
let expected: `column 'users' does not exist on 'messages'.`
expectType<TypeEqual<typeof result, typeof expected>>(true)
}

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

0 comments on commit b4e587a

Please sign in to comment.