Skip to content

Commit 8632e84

Browse files
committed
mostly parity
1 parent c4f1d65 commit 8632e84

File tree

6 files changed

+103
-47
lines changed

6 files changed

+103
-47
lines changed

src/PostgrestBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import nodeFetch from '@supabase/node-fetch'
33

44
import type { Fetch, PostgrestResponseSuccess, PostgrestSingleResponse } from './types'
55

6-
export default abstract class PostgrestBuilder<Result, ThrowOnError extends boolean = false>
6+
export default abstract class PostgrestBuilder<Result, ThrowOnError extends boolean>
77
implements PromiseLike<PostgrestSingleResponse<Result>>
88
{
99
protected method: 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE'

src/PostgrestClient.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ export default class PostgrestClient<
2121
: string & keyof Database,
2222
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
2323
? Database[SchemaName]
24-
: any
24+
: any,
25+
ThrowOnError extends boolean = false
2526
> {
2627
url: string
2728
headers: Record<string, string>
2829
schemaName?: SchemaName
2930
fetch?: Fetch
31+
shouldThrowOnError: ThrowOnError
3032

3133
// TODO: Add back shouldThrowOnError once we figure out the typings
3234
/**
@@ -44,37 +46,50 @@ export default class PostgrestClient<
4446
headers = {},
4547
schema,
4648
fetch,
49+
shouldThrowOnError,
4750
}: {
4851
headers?: Record<string, string>
4952
schema?: SchemaName
5053
fetch?: Fetch
54+
shouldThrowOnError?: ThrowOnError
5155
} = {}
5256
) {
5357
this.url = url
5458
this.headers = { ...DEFAULT_HEADERS, ...headers }
5559
this.schemaName = schema
5660
this.fetch = fetch
61+
this.shouldThrowOnError = Boolean(shouldThrowOnError) as ThrowOnError
62+
}
63+
64+
throwOnError(): PostgrestClient<Database, SchemaName, Schema, true> {
65+
return new PostgrestClient(this.url, {
66+
headers: this.headers,
67+
schema: this.schemaName,
68+
fetch: this.fetch,
69+
shouldThrowOnError: true,
70+
}) as PostgrestClient<Database, SchemaName, Schema, true>
5771
}
5872

5973
from<
6074
TableName extends string & keyof Schema['Tables'],
6175
Table extends Schema['Tables'][TableName]
62-
>(relation: TableName): PostgrestQueryBuilder<Schema, Table>
76+
>(relation: TableName): PostgrestQueryBuilder<Schema, Table, ThrowOnError>
6377
from<ViewName extends string & keyof Schema['Views'], View extends Schema['Views'][ViewName]>(
6478
relation: ViewName
65-
): PostgrestQueryBuilder<Schema, View>
66-
from(relation: string): PostgrestQueryBuilder<Schema, any>
79+
): PostgrestQueryBuilder<Schema, View, ThrowOnError>
80+
from(relation: string): PostgrestQueryBuilder<Schema, any, ThrowOnError>
6781
/**
6882
* Perform a query on a table or a view.
6983
*
7084
* @param relation - The table or view name to query
7185
*/
72-
from(relation: string): PostgrestQueryBuilder<Schema, any> {
86+
from(relation: string): PostgrestQueryBuilder<Schema, any, ThrowOnError> {
7387
const url = new URL(`${this.url}/${relation}`)
74-
return new PostgrestQueryBuilder<Schema, any>(url, {
88+
return new PostgrestQueryBuilder<Schema, any, ThrowOnError>(url, {
7589
headers: { ...this.headers },
7690
schema: this.schemaName,
7791
fetch: this.fetch,
92+
shouldThrowOnError: this.shouldThrowOnError,
7893
})
7994
}
8095

@@ -90,16 +105,19 @@ export default class PostgrestClient<
90105
): PostgrestClient<
91106
Database,
92107
DynamicSchema,
93-
Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any
108+
Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any,
109+
ThrowOnError
94110
> {
95111
return new PostgrestClient<
96112
Database,
97113
DynamicSchema,
98-
Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any
114+
Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any,
115+
ThrowOnError
99116
>(this.url, {
100117
headers: this.headers,
101118
schema,
102119
fetch: this.fetch,
120+
shouldThrowOnError: this.shouldThrowOnError,
103121
})
104122
}
105123

@@ -144,7 +162,9 @@ export default class PostgrestClient<
144162
? Function_['Returns'][number]
145163
: never
146164
: never,
147-
Function_['Returns']
165+
Function_['Returns'],
166+
unknown,
167+
ThrowOnError
148168
> {
149169
let method: 'HEAD' | 'POST'
150170
const url = new URL(`${this.url}/rpc/${fn}`)
@@ -172,6 +192,7 @@ export default class PostgrestClient<
172192
body,
173193
fetch: this.fetch,
174194
allowEmpty: false,
175-
} as unknown as PostgrestBuilder<Function_['Returns']>)
195+
shouldThrowOnError: this.shouldThrowOnError,
196+
} as unknown as PostgrestBuilder<Function_['Returns'], ThrowOnError>)
176197
}
177198
}

src/PostgrestFilterBuilder.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ export default class PostgrestFilterBuilder<
2929
Schema extends GenericSchema,
3030
Row extends Record<string, unknown>,
3131
Result,
32-
Relationships = unknown
33-
> extends PostgrestTransformBuilder<Schema, Row, Result, Relationships> {
32+
Relationships, // = unknown,
33+
ThrowOnError extends boolean // = false
34+
> extends PostgrestTransformBuilder<Schema, Row, Result, Relationships, ThrowOnError> {
3435
eq<ColumnName extends string & keyof Row>(
3536
column: ColumnName,
3637
value: NonNullable<Row[ColumnName]>

src/PostgrestQueryBuilder.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,35 @@ import { Fetch, GenericSchema, GenericTable, GenericView } from './types'
66
export default class PostgrestQueryBuilder<
77
Schema extends GenericSchema,
88
Relation extends GenericTable | GenericView,
9+
ThrowOnError extends boolean,
910
Relationships = Relation extends { Relationships: infer R } ? R : unknown
1011
> {
1112
url: URL
1213
headers: Record<string, string>
1314
schema?: string
1415
signal?: AbortSignal
1516
fetch?: Fetch
17+
shouldThrowOnError: ThrowOnError
1618

1719
constructor(
1820
url: URL,
1921
{
2022
headers = {},
2123
schema,
2224
fetch,
25+
shouldThrowOnError,
2326
}: {
2427
headers?: Record<string, string>
2528
schema?: string
2629
fetch?: Fetch
30+
shouldThrowOnError?: ThrowOnError
2731
}
2832
) {
2933
this.url = url
3034
this.headers = headers
3135
this.schema = schema
3236
this.fetch = fetch
37+
this.shouldThrowOnError = Boolean(shouldThrowOnError) as ThrowOnError
3338
}
3439

3540
/**
@@ -65,7 +70,7 @@ export default class PostgrestQueryBuilder<
6570
head?: boolean
6671
count?: 'exact' | 'planned' | 'estimated'
6772
} = {}
68-
): PostgrestFilterBuilder<Schema, Relation['Row'], ResultOne[], Relationships> {
73+
): PostgrestFilterBuilder<Schema, Relation['Row'], ResultOne[], Relationships, ThrowOnError> {
6974
const method = head ? 'HEAD' : 'GET'
7075
// Remove whitespaces except when quoted
7176
let quoted = false
@@ -93,7 +98,8 @@ export default class PostgrestQueryBuilder<
9398
schema: this.schema,
9499
fetch: this.fetch,
95100
allowEmpty: false,
96-
} as unknown as PostgrestBuilder<ResultOne[]>)
101+
shouldThrowOnError: this.shouldThrowOnError,
102+
} as unknown as PostgrestBuilder<ResultOne[], ThrowOnError>)
97103
}
98104

99105
// TODO(v3): Make `defaultToNull` consistent for both single & bulk inserts.
@@ -102,14 +108,14 @@ export default class PostgrestQueryBuilder<
102108
options?: {
103109
count?: 'exact' | 'planned' | 'estimated'
104110
}
105-
): PostgrestFilterBuilder<Schema, Relation['Row'], null, Relationships>
111+
): PostgrestFilterBuilder<Schema, Relation['Row'], null, Relationships, ThrowOnError>
106112
insert<Row extends Relation extends { Insert: unknown } ? Relation['Insert'] : never>(
107113
values: Row[],
108114
options?: {
109115
count?: 'exact' | 'planned' | 'estimated'
110116
defaultToNull?: boolean
111117
}
112-
): PostgrestFilterBuilder<Schema, Relation['Row'], null, Relationships>
118+
): PostgrestFilterBuilder<Schema, Relation['Row'], null, Relationships, ThrowOnError>
113119
/**
114120
* Perform an INSERT into the table or view.
115121
*
@@ -145,7 +151,7 @@ export default class PostgrestQueryBuilder<
145151
count?: 'exact' | 'planned' | 'estimated'
146152
defaultToNull?: boolean
147153
} = {}
148-
): PostgrestFilterBuilder<Schema, Relation['Row'], null, Relationships> {
154+
): PostgrestFilterBuilder<Schema, Relation['Row'], null, Relationships, ThrowOnError> {
149155
const method = 'POST'
150156

151157
const prefersHeaders = []
@@ -176,7 +182,8 @@ export default class PostgrestQueryBuilder<
176182
body: values,
177183
fetch: this.fetch,
178184
allowEmpty: false,
179-
} as unknown as PostgrestBuilder<null>)
185+
shouldThrowOnError: this.shouldThrowOnError,
186+
} as unknown as PostgrestBuilder<null, ThrowOnError>)
180187
}
181188

182189
// TODO(v3): Make `defaultToNull` consistent for both single & bulk upserts.
@@ -187,7 +194,7 @@ export default class PostgrestQueryBuilder<
187194
ignoreDuplicates?: boolean
188195
count?: 'exact' | 'planned' | 'estimated'
189196
}
190-
): PostgrestFilterBuilder<Schema, Relation['Row'], null, Relationships>
197+
): PostgrestFilterBuilder<Schema, Relation['Row'], null, Relationships, ThrowOnError>
191198
upsert<Row extends Relation extends { Insert: unknown } ? Relation['Insert'] : never>(
192199
values: Row[],
193200
options?: {
@@ -196,7 +203,7 @@ export default class PostgrestQueryBuilder<
196203
count?: 'exact' | 'planned' | 'estimated'
197204
defaultToNull?: boolean
198205
}
199-
): PostgrestFilterBuilder<Schema, Relation['Row'], null, Relationships>
206+
): PostgrestFilterBuilder<Schema, Relation['Row'], null, Relationships, ThrowOnError>
200207
/**
201208
* Perform an UPSERT on the table or view. Depending on the column(s) passed
202209
* to `onConflict`, `.upsert()` allows you to perform the equivalent of
@@ -248,7 +255,7 @@ export default class PostgrestQueryBuilder<
248255
count?: 'exact' | 'planned' | 'estimated'
249256
defaultToNull?: boolean
250257
} = {}
251-
): PostgrestFilterBuilder<Schema, Relation['Row'], null, Relationships> {
258+
): PostgrestFilterBuilder<Schema, Relation['Row'], null, Relationships, ThrowOnError> {
252259
const method = 'POST'
253260

254261
const prefersHeaders = [`resolution=${ignoreDuplicates ? 'ignore' : 'merge'}-duplicates`]
@@ -281,7 +288,8 @@ export default class PostgrestQueryBuilder<
281288
body: values,
282289
fetch: this.fetch,
283290
allowEmpty: false,
284-
} as unknown as PostgrestBuilder<null>)
291+
shouldThrowOnError: this.shouldThrowOnError,
292+
} as unknown as PostgrestBuilder<null, ThrowOnError>)
285293
}
286294

287295
/**
@@ -312,7 +320,7 @@ export default class PostgrestQueryBuilder<
312320
}: {
313321
count?: 'exact' | 'planned' | 'estimated'
314322
} = {}
315-
): PostgrestFilterBuilder<Schema, Relation['Row'], null, Relationships> {
323+
): PostgrestFilterBuilder<Schema, Relation['Row'], null, Relationships, ThrowOnError> {
316324
const method = 'PATCH'
317325
const prefersHeaders = []
318326
if (this.headers['Prefer']) {
@@ -331,7 +339,8 @@ export default class PostgrestQueryBuilder<
331339
body: values,
332340
fetch: this.fetch,
333341
allowEmpty: false,
334-
} as unknown as PostgrestBuilder<null>)
342+
shouldThrowOnError: this.shouldThrowOnError,
343+
} as unknown as PostgrestBuilder<null, ThrowOnError>)
335344
}
336345

337346
/**
@@ -357,7 +366,7 @@ export default class PostgrestQueryBuilder<
357366
count,
358367
}: {
359368
count?: 'exact' | 'planned' | 'estimated'
360-
} = {}): PostgrestFilterBuilder<Schema, Relation['Row'], null, Relationships> {
369+
} = {}): PostgrestFilterBuilder<Schema, Relation['Row'], null, Relationships, ThrowOnError> {
361370
const method = 'DELETE'
362371
const prefersHeaders = []
363372
if (count) {
@@ -375,6 +384,7 @@ export default class PostgrestQueryBuilder<
375384
schema: this.schema,
376385
fetch: this.fetch,
377386
allowEmpty: false,
378-
} as unknown as PostgrestBuilder<null>)
387+
shouldThrowOnError: this.shouldThrowOnError,
388+
} as unknown as PostgrestBuilder<null, ThrowOnError>)
379389
}
380390
}

0 commit comments

Comments
 (0)