Skip to content

Commit aeb2ce9

Browse files
3rusoedirgo
andauthored
fix: return SelectQueryError when referencing missing columns (#436)
* Returns 'never' if a column that does not exist in the schema * fix: return SelectQueryError when referencing missing columns --------- Co-authored-by: Bobbie Soedirgo <[email protected]>
1 parent 56376c0 commit aeb2ce9

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

src/select-query-parser.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type Json = string | number | boolean | null | { [key: string]: Json } | Json[]
5555
*/
5656
type ParserError<Message extends string> = { error: true } & Message
5757
type GenericStringError = ParserError<'Received a generic string'>
58+
export type SelectQueryError<Message extends string> = { error: true } & Message
5859

5960
/**
6061
* Trims whitespace from the left of the input.
@@ -136,7 +137,9 @@ type ConstructFieldDefinition<
136137
: never
137138
}
138139
: Field extends { name: string; original: string }
139-
? { [K in Field['name']]: Row[Field['original']] }
140+
? Field['original'] extends keyof Row
141+
? { [K in Field['name']]: Row[Field['original']] }
142+
: SelectQueryError<`Referencing missing column \`${Field['original']}\``>
140143
: Field extends { name: string; type: infer T }
141144
? { [K in Field['name']]: T }
142145
: Record<string, unknown>
@@ -415,21 +418,25 @@ type GetResultHelper<
415418
Fields extends unknown[],
416419
Acc
417420
> = Fields extends [infer R]
418-
? GetResultHelper<
419-
Schema,
420-
Row,
421-
Relationships,
422-
[],
423-
ConstructFieldDefinition<Schema, Row, Relationships, R> & Acc
424-
>
421+
? ConstructFieldDefinition<Schema, Row, Relationships, R> extends SelectQueryError<infer E>
422+
? SelectQueryError<E>
423+
: GetResultHelper<
424+
Schema,
425+
Row,
426+
Relationships,
427+
[],
428+
ConstructFieldDefinition<Schema, Row, Relationships, R> & Acc
429+
>
425430
: Fields extends [infer R, ...infer Rest]
426-
? GetResultHelper<
427-
Schema,
428-
Row,
429-
Relationships,
430-
Rest,
431-
ConstructFieldDefinition<Schema, Row, Relationships, R> & Acc
432-
>
431+
? ConstructFieldDefinition<Schema, Row, Relationships, R> extends SelectQueryError<infer E>
432+
? SelectQueryError<E>
433+
: GetResultHelper<
434+
Schema,
435+
Row,
436+
Relationships,
437+
Rest,
438+
ConstructFieldDefinition<Schema, Row, Relationships, R> & Acc
439+
>
433440
: Prettify<Acc>
434441

435442
/**

test/index.test-d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expectError, expectType } from 'tsd'
2-
import { PostgrestClient } from '../src/index'
2+
import { PostgrestClient, PostgrestSingleResponse } from '../src/index'
3+
import { SelectQueryError } from '../src/select-query-parser'
34
import { Database, Json } from './types'
45

56
const REST_URL = 'http://localhost:3000'
@@ -81,3 +82,9 @@ const postgrest = new PostgrestClient<Database>(REST_URL)
8182
}
8283
expectType<Database['public']['Tables']['messages']['Row'][]>(user.messages)
8384
}
85+
86+
// referencing missing column
87+
{
88+
const res = await postgrest.from('users').select('username, dat')
89+
expectType<PostgrestSingleResponse<SelectQueryError<`Referencing missing column \`dat\``>[]>>(res)
90+
}

0 commit comments

Comments
 (0)