diff --git a/src/select-query-parser/result.ts b/src/select-query-parser/result.ts index 07802c70..391f175c 100644 --- a/src/select-query-parser/result.ts +++ b/src/select-query-parser/result.ts @@ -16,6 +16,7 @@ import { IsRelationNullable, ResolveRelationship, SelectQueryError, + UnwrapErrorMessages, } from './utils' /** @@ -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 extends infer ParsedQuery extends Ast.Node[] - ? RPCCallNodes + ? UnwrapErrorMessages< + RPCCallNodes + > : Row : ParseQuery extends infer ParsedQuery ? ParsedQuery extends Ast.Node[] ? RelationName extends string ? Relationships extends GenericRelationship[] - ? ProcessNodes - : SelectQueryError<'Invalid Relationships cannot infer result type'> - : SelectQueryError<'Invalid RelationName cannot infer result type'> - : ParsedQuery + ? UnwrapErrorMessages> + : UnwrapErrorMessages> + : UnwrapErrorMessages> + : UnwrapErrorMessages : never /** diff --git a/src/select-query-parser/utils.ts b/src/select-query-parser/utils.ts index eff2b1fc..5a7c0abf 100644 --- a/src/select-query-parser/utils.ts +++ b/src/select-query-parser/utils.ts @@ -10,6 +10,12 @@ import { UnionToArray, } from './types' +export type UnwrapErrorMessages = T extends SelectQueryError + ? M + : T extends Record + ? { [K in keyof T]: UnwrapErrorMessages } + : T + export type SelectQueryError = { error: true } & Message type RequireHintingSelectQueryError< diff --git a/test/select-query-parser/select.test-d.ts b/test/select-query-parser/select.test-d.ts index 7849ea1f..0001e6db 100644 --- a/test/select-query-parser/select.test-d.ts +++ b/test/select-query-parser/select.test-d.ts @@ -198,9 +198,9 @@ type Schema = Database['public'] const { data } = await selectQueries.joinOneToOneWithNullablesNoHint.limit(1).single() let result: Exclude 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! ?"> - 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! ?"> - 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! ?"> + 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! ?" + 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! ?" + 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! ?" } expectType>(true) } @@ -222,9 +222,9 @@ type Schema = Database['public'] const { data } = await selectQueries.joinOneToManyWithNullablesNoHint.limit(1).single() let result: Exclude 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! ?"> - 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! ?"> - 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! ?"> + 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! ?" + 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! ?" + 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! ?" } expectType>(true) } @@ -630,7 +630,7 @@ type Schema = Database['public'] const { data } = await selectQueries.joinSelectViaColumnHintTwice.limit(1).single() let result: Exclude 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>(true) } @@ -641,7 +641,7 @@ type Schema = Database['public'] let result: Exclude 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>(true) } @@ -684,7 +684,7 @@ type Schema = Database['public'] { const { data } = await selectQueries.typecastingAndAggregate.limit(1).single() let result: Exclude - let expected: SelectQueryError<`column 'users' does not exist on 'messages'.`> + let expected: `column 'users' does not exist on 'messages'.` expectType>(true) } @@ -741,5 +741,5 @@ type Schema = Database['public'] { const { data, error } = await selectQueries.aggregateOnMissingColumnWithAlias.limit(1).single() if (error) throw error - expectType>(data) + expectType<`column 'missing_column' does not exist on 'users'.`>(data) }