Skip to content

Commit

Permalink
add example for erroneous TypeScript types when using non-"count" agg…
Browse files Browse the repository at this point in the history
…regate functions
  • Loading branch information
stefan-girlich committed Apr 10, 2024
1 parent 7c3dd21 commit a91b000
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
45 changes: 45 additions & 0 deletions test/aggregate_function.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { expectType } from 'tsd'
import { PostgrestClient } from '../src/index'
import { Database } from './types'

const REST_URL = 'http://localhost:3000'
const postgrest = new PostgrestClient<Database>(REST_URL)

// query for field of top-level resource => succeeds
{
const { data, error } = await postgrest.from('messages').select('numeric_value').single()
if (error) throw new Error(error.message)
expectType<{ numeric_value: number }>(data)
}

// `sum` on top-level resource => fails
{
// => const data: SelectQueryError<"Referencing missing column `sum`"> | null
const { data, error } = await postgrest.from('messages').select('numeric_value.sum()').single()
if (error) throw new Error(error.message)
expectType<{ sum: number }>(data)
}

// `min` and `max` on top-level resource => fails
{
// => const data: SelectQueryError<"Referencing missing column `max`"> | null
const { data, error } = await postgrest
.from('messages')
.select('numeric_value.min(), numeric_value.max()')
.single()
if (error) throw new Error(error.message)
expectType<{ min: number; max: number }>(data)
}

// `sum` on embedded resource => fails
{
// => users: SelectQueryError<"Referencing missing column `sum`"> | null
const { data, error } = await postgrest
.from('messages')
.select('message, users(numeric_value.sum())')
.single()
if (error) {
throw new Error(error.message)
}
expectType<{ message: string | null; users: { sum: number } | null }>(data)
}
2 changes: 2 additions & 0 deletions test/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export interface Database {
id: number
message: string | null
username: string
numeric_value: number
}
Insert: {
channel_id: number
Expand Down Expand Up @@ -160,6 +161,7 @@ export interface Database {
data: Json | null
status: Database['public']['Enums']['user_status'] | null
username: string
numeric_value: number
}
Insert: {
age_range?: unknown | null
Expand Down

0 comments on commit a91b000

Please sign in to comment.