Skip to content

Commit

Permalink
fix: return SelectQueryError when referencing missing columns (#436)
Browse files Browse the repository at this point in the history
* Returns 'never' if a column that does not exist in the schema

* fix: return SelectQueryError when referencing missing columns

---------

Co-authored-by: Bobbie Soedirgo <bobbie@soedirgo.dev>
  • Loading branch information
3ru and soedirgo committed Jun 14, 2023
1 parent 56376c0 commit aeb2ce9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
37 changes: 22 additions & 15 deletions src/select-query-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Json = string | number | boolean | null | { [key: string]: Json } | Json[]
*/
type ParserError<Message extends string> = { error: true } & Message
type GenericStringError = ParserError<'Received a generic string'>
export type SelectQueryError<Message extends string> = { error: true } & Message

/**
* Trims whitespace from the left of the input.
Expand Down Expand Up @@ -136,7 +137,9 @@ type ConstructFieldDefinition<
: never
}
: Field extends { name: string; original: string }
? { [K in Field['name']]: Row[Field['original']] }
? Field['original'] extends keyof Row
? { [K in Field['name']]: Row[Field['original']] }
: SelectQueryError<`Referencing missing column \`${Field['original']}\``>
: Field extends { name: string; type: infer T }
? { [K in Field['name']]: T }
: Record<string, unknown>
Expand Down Expand Up @@ -415,21 +418,25 @@ type GetResultHelper<
Fields extends unknown[],
Acc
> = Fields extends [infer R]
? GetResultHelper<
Schema,
Row,
Relationships,
[],
ConstructFieldDefinition<Schema, Row, Relationships, R> & Acc
>
? ConstructFieldDefinition<Schema, Row, Relationships, R> extends SelectQueryError<infer E>
? SelectQueryError<E>
: GetResultHelper<
Schema,
Row,
Relationships,
[],
ConstructFieldDefinition<Schema, Row, Relationships, R> & Acc
>
: Fields extends [infer R, ...infer Rest]
? GetResultHelper<
Schema,
Row,
Relationships,
Rest,
ConstructFieldDefinition<Schema, Row, Relationships, R> & Acc
>
? ConstructFieldDefinition<Schema, Row, Relationships, R> extends SelectQueryError<infer E>
? SelectQueryError<E>
: GetResultHelper<
Schema,
Row,
Relationships,
Rest,
ConstructFieldDefinition<Schema, Row, Relationships, R> & Acc
>
: Prettify<Acc>

/**
Expand Down
9 changes: 8 additions & 1 deletion test/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expectError, expectType } from 'tsd'
import { PostgrestClient } from '../src/index'
import { PostgrestClient, PostgrestSingleResponse } from '../src/index'
import { SelectQueryError } from '../src/select-query-parser'
import { Database, Json } from './types'

const REST_URL = 'http://localhost:3000'
Expand Down Expand Up @@ -81,3 +82,9 @@ const postgrest = new PostgrestClient<Database>(REST_URL)
}
expectType<Database['public']['Tables']['messages']['Row'][]>(user.messages)
}

// referencing missing column
{
const res = await postgrest.from('users').select('username, dat')
expectType<PostgrestSingleResponse<SelectQueryError<`Referencing missing column \`dat\``>[]>>(res)
}

0 comments on commit aeb2ce9

Please sign in to comment.