From e6037682209b287f33c1bb5fd9502beb06f5fe2e Mon Sep 17 00:00:00 2001 From: Andrew Valleteau Date: Mon, 28 Oct 2024 18:27:12 +0100 Subject: [PATCH] fix(parser): revert error unwrapping (#566) * Revert "fix: unwrap error within arrays" This reverts commit e31b1ff5449cd1bdf252e112a38b1ff5a8f40764. * Revert "fix(result): error plain string results unwrapping" This reverts commit b4e587ab25f5d62b937a8d0c5982d7b78fb66c67. * fix: remove unecessary cast * fix: remove intermediate type for better end result --- src/select-query-parser/result.ts | 13 ++++----- src/select-query-parser/utils.ts | 33 +++++------------------ test/index.test-d.ts | 2 +- test/select-query-parser/select.test-d.ts | 25 ++++++++--------- 4 files changed, 25 insertions(+), 48 deletions(-) diff --git a/src/select-query-parser/result.ts b/src/select-query-parser/result.ts index 4c6e24b8..1742df74 100644 --- a/src/select-query-parser/result.ts +++ b/src/select-query-parser/result.ts @@ -16,7 +16,6 @@ import { IsRelationNullable, ResolveRelationship, SelectQueryError, - UnwrapErrorMessages, } from './utils' /** @@ -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 extends infer ParsedQuery extends Ast.Node[] - ? UnwrapErrorMessages< - RPCCallNodes - > + ? RPCCallNodes : Row : ParseQuery extends infer ParsedQuery ? ParsedQuery extends Ast.Node[] ? RelationName extends string ? Relationships extends GenericRelationship[] - ? UnwrapErrorMessages> - : UnwrapErrorMessages> - : UnwrapErrorMessages> - : UnwrapErrorMessages + ? ProcessNodes + : SelectQueryError<'Invalid Relationships cannot infer result type'> + : SelectQueryError<'Invalid RelationName cannot infer result type'> + : ParsedQuery : never /** diff --git a/src/select-query-parser/utils.ts b/src/select-query-parser/utils.ts index 775f71d8..b869229d 100644 --- a/src/select-query-parser/utils.ts +++ b/src/select-query-parser/utils.ts @@ -12,21 +12,6 @@ import { export type SelectQueryError = { 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}! ?`> - -export type UnwrapErrorMessages = T extends SelectQueryError - ? M - : T extends SelectQueryError[] - ? M[] - : T extends (infer U)[] - ? UnwrapErrorMessages[] - : T extends Record - ? { [K in keyof T]: UnwrapErrorMessages } - : T - export type GetFieldNodeResultName = Field['alias'] extends string ? Field['alias'] : Field['aggregateFunction'] extends AggregateFunctions @@ -161,7 +146,7 @@ type HasMultipleFKeysToFRel = Relationships extends [ type CheckRelationshipError< Schema extends GenericSchema, Relationships extends GenericRelationship[], - CurrentTableOrView extends keyof TablesAndViews, + CurrentTableOrView extends keyof TablesAndViews & string, FoundRelation > = FoundRelation extends SelectQueryError ? FoundRelation @@ -176,10 +161,7 @@ type CheckRelationshipError< ? // We check if there is possible confusion with other relations with this table HasMultipleFKeysToFRel 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}! ?`> : FoundRelation : // Same check for forward relationships, but we must gather the relationships from the found relation FoundRelation extends { @@ -188,13 +170,13 @@ type CheckRelationshipError< name: string } direction: 'forward' - from: infer From extends keyof TablesAndViews + from: infer From extends keyof TablesAndViews & string } ? HasMultipleFKeysToFRel< RelatedRelationName, TablesAndViews[From]['Relationships'] > extends true - ? RequireHintingSelectQueryError + ? SelectQueryError<`Could not embed because more than one relationship was found for '${From}' and '${RelatedRelationName}' you need to hint the column with ${From}! ?`> : FoundRelation : FoundRelation @@ -229,7 +211,7 @@ type ResolveReverseRelationship< Schema extends GenericSchema, Relationships extends GenericRelationship[], Field extends Ast.FieldNode, - CurrentTableOrView extends keyof TablesAndViews + CurrentTableOrView extends keyof TablesAndViews & string > = FindFieldMatchingRelationships extends infer FoundRelation ? FoundRelation extends never ? false @@ -245,10 +227,7 @@ type ResolveReverseRelationship< } : // If the relation was found via implicit relation naming, we must ensure there is no conflicting matches HasMultipleFKeysToFRel 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}! ?`> : { referencedTable: TablesAndViews[RelatedRelationName] relation: FoundRelation diff --git a/test/index.test-d.ts b/test/index.test-d.ts index 516529e9..e64dabcc 100644 --- a/test/index.test-d.ts +++ b/test/index.test-d.ts @@ -99,7 +99,7 @@ const postgrest = new PostgrestClient(REST_URL) } // getting this w/o the cast, not sure why: // Parameter type Json is declared too wide for argument type Json - expectType(data.bar as Json) + expectType(data.bar) expectType(data.baz) } diff --git a/test/select-query-parser/select.test-d.ts b/test/select-query-parser/select.test-d.ts index 1db498e0..af2fd540 100644 --- a/test/select-query-parser/select.test-d.ts +++ b/test/select-query-parser/select.test-d.ts @@ -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' @@ -197,9 +198,9 @@ type Schema = Database['public'] const { data } = await selectQueries.joinOneToOneWithNullablesNoHint.limit(1).single() let result: Exclude 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! ?" - 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! ?" + 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! ?"> } expectType>(true) } @@ -221,9 +222,9 @@ type Schema = Database['public'] const { data } = await selectQueries.joinOneToManyWithNullablesNoHint.limit(1).single() let result: Exclude 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! ?" - 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! ?" + 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! ?"> } expectType>(true) } @@ -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! ?" + 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_friend_of: Array third_wheel_of: Array @@ -439,7 +440,7 @@ type Schema = Database['public'] let result: Exclude let expected: { username: string - messages: "column 'sum' does not exist on 'messages'."[] + messages: SelectQueryError<"column 'sum' does not exist on 'messages'.">[] } expectType>(true) } @@ -629,7 +630,7 @@ type Schema = Database['public'] const { data } = await selectQueries.joinSelectViaColumnHintTwice.limit(1).single() let result: Exclude 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>(true) } @@ -640,7 +641,7 @@ type Schema = Database['public'] let result: Exclude 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>(true) } @@ -683,7 +684,7 @@ type Schema = Database['public'] { const { data } = await selectQueries.typecastingAndAggregate.limit(1).single() let result: Exclude - let expected: `column 'users' does not exist on 'messages'.` + let expected: SelectQueryError<`column 'users' does not exist on 'messages'.`> expectType>(true) } @@ -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>(data) } // many-to-many with join table