Skip to content

Make embedded resource non-nullable if using inner join #458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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