Skip to content

Commit

Permalink
non-nullable relationship if inner join (#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyozi committed Mar 13, 2024
1 parent 6f5d570 commit e26266a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/select-query-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ type ConstructFieldDefinition<
? Child | null
: Relationships extends unknown[]
? HasFKey<Field['hint'], Relationships> extends true
? Child | null
? Field extends { inner: true }
? Child
: Child | null
: Child[]
: Child[]
: never
Expand Down Expand Up @@ -260,7 +262,9 @@ type ConstructFieldDefinition<
? Child | null
: Relationships extends unknown[]
? HasFKeyToFRel<Field['original'], Relationships> extends true
? Child | null
? Field extends { inner: true }
? Child
: Child | null
: Child[]
: Child[]
: never
Expand Down Expand Up @@ -351,7 +355,7 @@ type ParseField<Input extends string> = Input extends ''
? EatWhitespace<Remainder> extends `!inner${infer Remainder}`
? ParseEmbeddedResource<EatWhitespace<Remainder>> extends [infer Fields, `${infer Remainder}`]
? // `field!inner(nodes)`
[{ name: Name; original: Name; children: Fields }, EatWhitespace<Remainder>]
[{ name: Name; original: Name; children: Fields; inner: true }, EatWhitespace<Remainder>]
: CreateParserErrorIfRequired<
ParseEmbeddedResource<EatWhitespace<Remainder>>,
'Expected embedded resource after `!inner`'
Expand All @@ -364,7 +368,10 @@ type ParseField<Input extends string> = Input extends ''
`${infer Remainder}`
]
? // `field!hint!inner(nodes)`
[{ name: Name; original: Name; hint: Hint; children: Fields }, EatWhitespace<Remainder>]
[
{ name: Name; original: Name; hint: Hint; children: Fields; inner: true },
EatWhitespace<Remainder>
]
: CreateParserErrorIfRequired<
ParseEmbeddedResource<EatWhitespace<Remainder>>,
'Expected embedded resource after `!inner`'
Expand Down
12 changes: 12 additions & 0 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ const postgrest = new PostgrestClient<Database>(REST_URL)
expectType<Database['public']['Tables']['users']['Row'] | null>(message.user)
}

// !inner relationship
{
const { data: message, error } = await postgrest
.from('messages')
.select('user:users!inner(*)')
.single()
if (error) {
throw new Error(error.message)
}
expectType<Database['public']['Tables']['users']['Row']>(message.user)
}

// one-to-many relationship
{
const { data: user, error } = await postgrest.from('users').select('messages(*)').single()
Expand Down

0 comments on commit e26266a

Please sign in to comment.