Skip to content

Commit fd36306

Browse files
authored
Merge pull request #618 from supabase/avallete/psql-436-feedback-request-postgrest-js-and-postgrest-v13-integration
feat(types): setup postgrest-13 types inference
2 parents e1d9501 + 83923f8 commit fd36306

21 files changed

+1213
-96
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ on:
1212
jobs:
1313
test:
1414
name: Test
15-
runs-on: ubuntu-20.04
15+
runs-on: ubuntu-22.04
1616
steps:
1717
- uses: actions/checkout@v2
1818

.github/workflows/docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99
jobs:
1010
docs:
1111
name: Publish docs
12-
runs-on: ubuntu-20.04
12+
runs-on: ubuntu-22.04
1313
steps:
1414
- uses: actions/checkout@v2
1515

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ on:
1111
jobs:
1212
release:
1313
name: Release
14-
runs-on: ubuntu-20.04
14+
runs-on: ubuntu-22.04
1515
steps:
1616
- uses: actions/checkout@v2
1717

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
22
preset: 'ts-jest',
33
testEnvironment: 'node',
4-
collectCoverageFrom: ['src/**/*'],
4+
collectCoverageFrom: ['src/**/*', '!src/types.ts'],
55
}

src/PostgrestBuilder.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ import type {
88
CheckMatchingArrayTypes,
99
MergePartialResult,
1010
IsValidResultOverride,
11+
ClientServerOptions,
1112
} from './types'
1213
import PostgrestError from './PostgrestError'
1314
import { ContainsNull } from './select-query-parser/types'
1415

15-
export default abstract class PostgrestBuilder<Result, ThrowOnError extends boolean = false>
16-
implements
16+
export default abstract class PostgrestBuilder<
17+
ClientOptions extends ClientServerOptions,
18+
Result,
19+
ThrowOnError extends boolean = false
20+
> implements
1721
PromiseLike<
1822
ThrowOnError extends true ? PostgrestResponseSuccess<Result> : PostgrestSingleResponse<Result>
1923
>
@@ -28,7 +32,7 @@ export default abstract class PostgrestBuilder<Result, ThrowOnError extends bool
2832
protected fetch: Fetch
2933
protected isMaybeSingle: boolean
3034

31-
constructor(builder: PostgrestBuilder<Result>) {
35+
constructor(builder: PostgrestBuilder<ClientOptions, Result>) {
3236
this.method = builder.method
3337
this.url = builder.url
3438
this.headers = builder.headers
@@ -53,9 +57,9 @@ export default abstract class PostgrestBuilder<Result, ThrowOnError extends bool
5357
*
5458
* {@link https://github.com/supabase/supabase-js/issues/92}
5559
*/
56-
throwOnError(): this & PostgrestBuilder<Result, true> {
60+
throwOnError(): this & PostgrestBuilder<ClientOptions, Result, true> {
5761
this.shouldThrowOnError = true
58-
return this as this & PostgrestBuilder<Result, true>
62+
return this as this & PostgrestBuilder<ClientOptions, Result, true>
5963
}
6064

6165
/**
@@ -224,9 +228,14 @@ export default abstract class PostgrestBuilder<Result, ThrowOnError extends bool
224228
* @typeParam NewResult - The new result type to override with
225229
* @deprecated Use overrideTypes<yourType, { merge: false }>() method at the end of your call chain instead
226230
*/
227-
returns<NewResult>(): PostgrestBuilder<CheckMatchingArrayTypes<Result, NewResult>, ThrowOnError> {
231+
returns<NewResult>(): PostgrestBuilder<
232+
ClientOptions,
233+
CheckMatchingArrayTypes<Result, NewResult>,
234+
ThrowOnError
235+
> {
228236
/* istanbul ignore next */
229237
return this as unknown as PostgrestBuilder<
238+
ClientOptions,
230239
CheckMatchingArrayTypes<Result, NewResult>,
231240
ThrowOnError
232241
>
@@ -258,6 +267,7 @@ export default abstract class PostgrestBuilder<Result, ThrowOnError extends bool
258267
NewResult,
259268
Options extends { merge?: boolean } = { merge: true }
260269
>(): PostgrestBuilder<
270+
ClientOptions,
261271
IsValidResultOverride<Result, NewResult, false, false> extends true
262272
? // Preserve the optionality of the result if the overriden type is an object (case of chaining with `maybeSingle`)
263273
ContainsNull<Result> extends true
@@ -267,6 +277,7 @@ export default abstract class PostgrestBuilder<Result, ThrowOnError extends bool
267277
ThrowOnError
268278
> {
269279
return this as unknown as PostgrestBuilder<
280+
ClientOptions,
270281
IsValidResultOverride<Result, NewResult, false, false> extends true
271282
? // Preserve the optionality of the result if the overriden type is an object (case of chaining with `maybeSingle`)
272283
ContainsNull<Result> extends true

src/PostgrestClient.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import PostgrestQueryBuilder from './PostgrestQueryBuilder'
22
import PostgrestFilterBuilder from './PostgrestFilterBuilder'
33
import PostgrestBuilder from './PostgrestBuilder'
44
import { DEFAULT_HEADERS } from './constants'
5-
import { Fetch, GenericSchema } from './types'
5+
import { Fetch, GenericSchema, ClientServerOptions, GetGenericDatabaseWithOptions } from './types'
66

77
/**
88
* PostgREST client.
@@ -16,11 +16,16 @@ import { Fetch, GenericSchema } from './types'
1616
*/
1717
export default class PostgrestClient<
1818
Database = any,
19-
SchemaName extends string & keyof Database = 'public' extends keyof Database
19+
ClientOptions extends ClientServerOptions = GetGenericDatabaseWithOptions<
20+
Database,
21+
{ PostgrestVersion: '12' }
22+
>['options'],
23+
SchemaName extends string &
24+
keyof GetGenericDatabaseWithOptions<Database>['db'] = 'public' extends keyof GetGenericDatabaseWithOptions<Database>['db']
2025
? 'public'
21-
: string & keyof Database,
22-
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
23-
? Database[SchemaName]
26+
: string & keyof GetGenericDatabaseWithOptions<Database>['db'],
27+
Schema extends GenericSchema = GetGenericDatabaseWithOptions<Database>['db'][SchemaName] extends GenericSchema
28+
? GetGenericDatabaseWithOptions<Database>['db'][SchemaName]
2429
: any
2530
> {
2631
url: string
@@ -59,16 +64,16 @@ export default class PostgrestClient<
5964
from<
6065
TableName extends string & keyof Schema['Tables'],
6166
Table extends Schema['Tables'][TableName]
62-
>(relation: TableName): PostgrestQueryBuilder<Schema, Table, TableName>
67+
>(relation: TableName): PostgrestQueryBuilder<ClientOptions, Schema, Table, TableName>
6368
from<ViewName extends string & keyof Schema['Views'], View extends Schema['Views'][ViewName]>(
6469
relation: ViewName
65-
): PostgrestQueryBuilder<Schema, View, ViewName>
70+
): PostgrestQueryBuilder<ClientOptions, Schema, View, ViewName>
6671
/**
6772
* Perform a query on a table or a view.
6873
*
6974
* @param relation - The table or view name to query
7075
*/
71-
from(relation: string): PostgrestQueryBuilder<Schema, any, any> {
76+
from(relation: string): PostgrestQueryBuilder<ClientOptions, Schema, any, any> {
7277
const url = new URL(`${this.url}/${relation}`)
7378
return new PostgrestQueryBuilder(url, {
7479
headers: { ...this.headers },
@@ -84,10 +89,11 @@ export default class PostgrestClient<
8489
*
8590
* @param schema - The schema to query
8691
*/
87-
schema<DynamicSchema extends string & keyof Database>(
92+
schema<DynamicSchema extends string & keyof GetGenericDatabaseWithOptions<Database>['db']>(
8893
schema: DynamicSchema
8994
): PostgrestClient<
9095
Database,
96+
ClientOptions,
9197
DynamicSchema,
9298
Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any
9399
> {
@@ -134,6 +140,7 @@ export default class PostgrestClient<
134140
count?: 'exact' | 'planned' | 'estimated'
135141
} = {}
136142
): PostgrestFilterBuilder<
143+
ClientOptions,
137144
Schema,
138145
Fn['Returns'] extends any[]
139146
? Fn['Returns'][number] extends Record<string, unknown>
@@ -176,6 +183,6 @@ export default class PostgrestClient<
176183
body,
177184
fetch: this.fetch,
178185
allowEmpty: false,
179-
} as unknown as PostgrestBuilder<Fn['Returns']>)
186+
} as unknown as PostgrestBuilder<ClientOptions, Fn['Returns']>)
180187
}
181188
}

src/PostgrestFilterBuilder.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import PostgrestTransformBuilder from './PostgrestTransformBuilder'
22
import { JsonPathToAccessor, JsonPathToType } from './select-query-parser/utils'
3-
import { GenericSchema } from './types'
3+
import { ClientServerOptions, GenericSchema } from './types'
44

55
type FilterOperator =
66
| 'eq'
@@ -69,13 +69,23 @@ type ResolveFilterRelationshipValue<
6969
: unknown
7070
: never
7171

72+
export type InvalidMethodError<S extends string> = { Error: S }
73+
7274
export default class PostgrestFilterBuilder<
75+
ClientOptions extends ClientServerOptions,
7376
Schema extends GenericSchema,
7477
Row extends Record<string, unknown>,
7578
Result,
7679
RelationName = unknown,
7780
Relationships = unknown
78-
> extends PostgrestTransformBuilder<Schema, Row, Result, RelationName, Relationships> {
81+
> extends PostgrestTransformBuilder<
82+
ClientOptions,
83+
Schema,
84+
Row,
85+
Result,
86+
RelationName,
87+
Relationships
88+
> {
7989
/**
8090
* Match only rows where `column` is equal to `value`.
8191
*

0 commit comments

Comments
 (0)