Skip to content

Commit 31761f8

Browse files
committed
fix(postgrest): filtering after rpc call
Fixes: #1365
1 parent 18f82f1 commit 31761f8

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

packages/core/postgrest-js/src/PostgrestTransformBuilder.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import PostgrestBuilder from './PostgrestBuilder'
2-
import { InvalidMethodError } from './PostgrestFilterBuilder'
2+
import PostgrestFilterBuilder, { InvalidMethodError } from './PostgrestFilterBuilder'
33
import { GetResult } from './select-query-parser/result'
44
import {
55
GenericSchema,
@@ -31,11 +31,15 @@ export default class PostgrestTransformBuilder<
3131
NewResultOne = GetResult<Schema, Row, RelationName, Relationships, Query, ClientOptions>,
3232
>(
3333
columns?: Query
34-
): PostgrestTransformBuilder<
34+
): PostgrestFilterBuilder<
3535
ClientOptions,
3636
Schema,
3737
Row,
38-
NewResultOne[],
38+
Method extends 'RPC'
39+
? Result extends unknown[]
40+
? NewResultOne[]
41+
: NewResultOne
42+
: NewResultOne[],
3943
RelationName,
4044
Relationships,
4145
Method
@@ -56,11 +60,15 @@ export default class PostgrestTransformBuilder<
5660
.join('')
5761
this.url.searchParams.set('select', cleanedColumns)
5862
this.headers.append('Prefer', 'return=representation')
59-
return this as unknown as PostgrestTransformBuilder<
63+
return this as unknown as PostgrestFilterBuilder<
6064
ClientOptions,
6165
Schema,
6266
Row,
63-
NewResultOne[],
67+
Method extends 'RPC'
68+
? Result extends unknown[]
69+
? NewResultOne[]
70+
: NewResultOne
71+
: NewResultOne[],
6472
RelationName,
6573
Relationships,
6674
Method

packages/core/postgrest-js/test/advanced_rpc.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,3 +1247,65 @@ describe('advanced rpc', () => {
12471247
UserProfileSchema.parse(res.data)
12481248
})
12491249
})
1250+
1251+
test('should be able to filter before and after select rpc', async () => {
1252+
const res = await postgrest
1253+
.rpc('get_user_profile', {
1254+
//@ts-expect-error Type '{ username: string; }' is missing the following properties from type '{ age_range: unknown; catchphrase: unknown; data: unknown; status: "ONLINE" | "OFFLINE" | null; username: string; }': age_range, catchphrase, data, status
1255+
user_row: { username: 'supabot' },
1256+
})
1257+
.select('id, username, users(username, catchphrase)')
1258+
.eq('username', 'nope')
1259+
1260+
expect(res).toMatchInlineSnapshot(`
1261+
Object {
1262+
"count": null,
1263+
"data": null,
1264+
"error": null,
1265+
"status": 200,
1266+
"statusText": "OK",
1267+
}
1268+
`)
1269+
const res2 = await postgrest
1270+
.rpc('get_user_profile', {
1271+
//@ts-expect-error Type '{ username: string; }' is missing the following properties from type
1272+
user_row: { username: 'supabot' },
1273+
})
1274+
// should also be able to fitler before the select
1275+
.eq('username', 'nope')
1276+
.select('id, username, users(username, catchphrase)')
1277+
1278+
expect(res2).toMatchInlineSnapshot(`
1279+
Object {
1280+
"count": null,
1281+
"data": null,
1282+
"error": null,
1283+
"status": 200,
1284+
"statusText": "OK",
1285+
}
1286+
`)
1287+
const res3 = await postgrest
1288+
.rpc('get_user_profile', {
1289+
//@ts-expect-error Type '{ username: string; }' is missing the following properties from type
1290+
user_row: { username: 'supabot' },
1291+
})
1292+
// should also be able to fitler before the select
1293+
.eq('username', 'supabot')
1294+
.select('username, users(username, catchphrase)')
1295+
1296+
expect(res3).toMatchInlineSnapshot(`
1297+
Object {
1298+
"count": null,
1299+
"data": Object {
1300+
"username": "supabot",
1301+
"users": Object {
1302+
"catchphrase": "'cat' 'fat'",
1303+
"username": "supabot",
1304+
},
1305+
},
1306+
"error": null,
1307+
"status": 200,
1308+
"statusText": "OK",
1309+
}
1310+
`)
1311+
})

0 commit comments

Comments
 (0)