Skip to content

Commit

Permalink
Add support for ... spread operator
Browse files Browse the repository at this point in the history
  • Loading branch information
bnjmnt4n committed Nov 4, 2023
1 parent 9b38343 commit bcbd0f4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/select-query-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ 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'],
(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 @@ -278,12 +288,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}`
? // `renamed_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

0 comments on commit bcbd0f4

Please sign in to comment.