Skip to content
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

feat: parse spread operators in select queries #497

Merged
merged 1 commit into from
Feb 24, 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
19 changes: 19 additions & 0 deletions src/select-query-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@ type ConstructFieldDefinition<
Field
> = Field extends { star: true }
? Row
: Field extends { spread: true; original: string; children: unknown[] }
? GetResultHelper<
Schema,
(Schema['Tables'] & Schema['Views'])[Field['original']]['Row'],
Field['original'],
(Schema['Tables'] & Schema['Views'])[Field['original']] extends { Relationships: infer R }
? R
: unknown,
Field['children'],
unknown
>
: Field extends { name: string; original: string; hint: string; children: unknown[] }
? {
[_ in Field['name']]: GetResultHelper<
Expand Down Expand Up @@ -394,12 +405,20 @@ type ParseField<Input extends string> = Input extends ''
* - `*`
* - a field, as defined above
* - a renamed field, `renamed_field:field`
* - a spread field, `...field`
*/
type ParseNode<Input extends string> = Input extends ''
? ParserError<'Empty string'>
: // `*`
Input extends `*${infer Remainder}`
? [{ star: true }, EatWhitespace<Remainder>]
: // `...field`
Input extends `...${infer Remainder}`
? ParseField<EatWhitespace<Remainder>> extends [infer Field, `${infer Remainder}`]
? Field extends { children: unknown[] }
? [Prettify<{ spread: true } & Field>, EatWhitespace<Remainder>]
: ParserError<'Unable to parse spread resource'>
: ParserError<'Unable to parse spread resource'>
: ParseIdentifier<Input> extends [infer Name, `${infer Remainder}`]
? EatWhitespace<Remainder> extends `::${infer _Remainder}`
? // `field::`
Expand Down
26 changes: 26 additions & 0 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expectError, expectType } from 'tsd'
import { PostgrestClient, PostgrestSingleResponse } from '../src/index'
import { SelectQueryError } from '../src/select-query-parser'
import { Prettify } from '../src/types'
import { Database, Json } from './types'

const REST_URL = 'http://localhost:3000'
Expand Down Expand Up @@ -53,6 +54,31 @@ const postgrest = new PostgrestClient<Database>(REST_URL)
expectError(postgrest.from('updatable_view').update({ non_updatable_column: 0 }))
}

// spread resource with single column in select query
{
const { data, error } = await postgrest
.from('messages')
.select('message, ...users(status)')
.single()
if (error) {
throw new Error(error.message)
}
expectType<{ message: string | null; status: Database['public']['Enums']['user_status'] | null }>(
data
)
}

// spread resource with all columns in select query
{
const { data, error } = await postgrest.from('messages').select('message, ...users(*)').single()
if (error) {
throw new Error(error.message)
}
expectType<Prettify<{ message: string | null } & Database['public']['Tables']['users']['Row']>>(
data
)
}

// json accessor in select query
{
const { data, error } = await postgrest
Expand Down
Loading