Skip to content

Commit

Permalink
chore: finish merge
Browse files Browse the repository at this point in the history
  • Loading branch information
avallete committed Nov 5, 2024
1 parent f910cfa commit 0c2c211
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/select-query-parser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type ResolveRelationships<
match: Relation['relation']['match']
fieldName: GetFieldNodeResultName<Nodes[K]>
}
: never
: Relation
: never
: never
}>[0]
Expand Down Expand Up @@ -381,7 +381,7 @@ type FilterRelationships<R, TName, From> = R extends readonly (infer Rel)[]
: never
: never

type ResolveForwardRelationship<
export type ResolveForwardRelationship<
Schema extends GenericSchema,
Field extends Ast.FieldNode,
CurrentTableOrView extends keyof TablesAndViews<Schema> & string
Expand Down Expand Up @@ -449,7 +449,7 @@ type ResolveForwardRelationship<
* referencedColumns: ["id"]
* }
*/
export type FindJoinTableRelationship<
type ResolveJoinTableRelationship<
Schema extends GenericSchema,
CurrentTableOrView extends keyof TablesAndViews<Schema> & string,
FieldName extends string
Expand All @@ -465,6 +465,15 @@ export type FindJoinTableRelationship<
: never
}[keyof TablesAndViews<Schema>]

export type FindJoinTableRelationship<
Schema extends GenericSchema,
CurrentTableOrView extends keyof TablesAndViews<Schema> & string,
FieldName extends string
> = ResolveJoinTableRelationship<Schema, CurrentTableOrView, FieldName> extends infer Result
? [Result] extends [never]
? false
: Result
: never
/**
* Finds a matching relationship based on the FieldNode's name and optional hint.
*/
Expand Down
26 changes: 26 additions & 0 deletions test/relationships.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ export const selectParams = {
from: 'products',
select: '*, categories(*)',
},
nestedQueryWithSelectiveFieldsAndInnerJoin: {
from: 'users',
select:
'msgs:messages(id, ...message_details(created_at, channel!inner(id, slug, owner:users(*))))',
},
} as const

export const selectQueries = {
Expand Down Expand Up @@ -363,6 +368,9 @@ export const selectQueries = {
manyToManyWithJoinTable: postgrest
.from(selectParams.manyToManyWithJoinTable.from)
.select(selectParams.manyToManyWithJoinTable.select),
nestedQueryWithSelectiveFieldsAndInnerJoin: postgrest
.from(selectParams.nestedQueryWithSelectiveFieldsAndInnerJoin.from)
.select(selectParams.nestedQueryWithSelectiveFieldsAndInnerJoin.select),
} as const

test('nested query with selective fields', async () => {
Expand Down Expand Up @@ -1868,3 +1876,21 @@ test('many-to-many with join table', async () => {
}
`)
})

test('nested query with selective fields and inner join should error on non existing relation', async () => {
const res = await selectQueries.nestedQueryWithSelectiveFieldsAndInnerJoin.limit(1).single()
expect(res).toMatchInlineSnapshot(`
Object {
"count": null,
"data": null,
"error": Object {
"code": "PGRST200",
"details": "Searched for a foreign key relationship between 'messages' and 'message_details' in the schema 'public', but no matches were found.",
"hint": null,
"message": "Could not find a relationship between 'messages' and 'message_details' in the schema cache",
},
"status": 400,
"statusText": "Bad Request",
}
`)
})
4 changes: 2 additions & 2 deletions test/select-query-parser/result.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type SelectQueryFromTableResult<
expectType<TypeEqual<typeof result, typeof expected>>(true)
}

// nested query with selective fields
// nested query with selective fields and inner join should error on non existing relation
{
let result: SelectQueryFromTableResult<
'users',
Expand All @@ -81,7 +81,7 @@ type SelectQueryFromTableResult<
let expected: {
msgs: {
id: number
message_details: SelectQueryError<`Could not embed because more than one relationship was found for 'messages' and '${string}' you need to hint the column with messages!<columnName> ?`>
message_details: SelectQueryError<'could not find the relation between messages and message_details'>
}[]
}
expectType<TypeEqual<typeof result, typeof expected>>(true)
Expand Down

0 comments on commit 0c2c211

Please sign in to comment.