Skip to content

Type change for columns that do not exist in the schema #436

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

Merged
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
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)
}