Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Commit a1dd318

Browse files
committed
chore: add tests, fix types test
1 parent 5fec334 commit a1dd318

File tree

9 files changed

+327
-11
lines changed

9 files changed

+327
-11
lines changed

src/PostgrestClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ export type GetRpcFunctionFilterBuilderByArgs<
5555
: null
5656
}
5757
: // If we failed to find the function by argument, we still pass with any but also add an overridable
58-
Fn extends never
58+
Fn extends false
5959
? {
6060
Row: any
61-
Result: { error: true } & "Couldn't find function"
61+
Result: { error: true } & "Couldn't infer function definition matching provided arguments"
6262
RelationName: FnName
6363
Relationships: null
6464
}

src/select-query-parser/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,4 +662,4 @@ type FindMatchingFunctionBySetofFrom<
662662
TableName extends string
663663
> = FnUnion extends infer Fn extends GenericFunction
664664
? MatchingFunctionBySetofFrom<Fn, TableName>
665-
: never
665+
: false

test/advanced_rpc.test-d.ts

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ type Schema = Database['public']
99
{
1010
const { data } = await rpcQueries['function returning a setof embeded table']
1111
let result: Exclude<typeof data, null>
12-
let expected: Array<Schema['Tables']['messages']['Row']>
12+
let expected: Array<Omit<Schema['Tables']['messages']['Row'], 'blurb_message'>>
1313
expectType<TypeEqual<typeof result, typeof expected>>(true)
1414
}
1515

1616
{
1717
const { data } = await rpcQueries['function double definition returning a setof embeded table']
1818
let result: Exclude<typeof data, null>
19-
let expected: Array<Schema['Tables']['messages']['Row']>
19+
let expected: Array<Omit<Schema['Tables']['messages']['Row'], 'blurb_message'>>
2020
expectType<TypeEqual<typeof result, typeof expected>>(true)
2121
}
2222

@@ -30,21 +30,21 @@ type Schema = Database['public']
3030
{
3131
const { data } = await rpcQueries['function with scalar input']
3232
let result: Exclude<typeof data, null>
33-
let expected: Array<Schema['Tables']['messages']['Row']>
33+
let expected: Array<Omit<Schema['Tables']['messages']['Row'], 'blurb_message'>>
3434
expectType<TypeEqual<typeof result, typeof expected>>(true)
3535
}
3636

3737
{
3838
const { data } = await rpcQueries['function with table row input']
3939
let result: Exclude<typeof data, null>
40-
let expected: Array<Schema['Tables']['messages']['Row']>
40+
let expected: Array<Omit<Schema['Tables']['messages']['Row'], 'blurb_message'>>
4141
expectType<TypeEqual<typeof result, typeof expected>>(true)
4242
}
4343

4444
{
4545
const { data } = await rpcQueries['function with view row input']
4646
let result: Exclude<typeof data, null>
47-
let expected: Array<Schema['Tables']['messages']['Row']>
47+
let expected: Array<Omit<Schema['Tables']['messages']['Row'], 'blurb_message'>>
4848
expectType<TypeEqual<typeof result, typeof expected>>(true)
4949
}
5050

@@ -146,14 +146,14 @@ type Schema = Database['public']
146146
{
147147
const { data } = await rpcQueries['resolvable function with channel_id and search params']
148148
let result: Exclude<typeof data, null>
149-
let expected: Array<Schema['Tables']['messages']['Row']>
149+
let expected: Array<Omit<Schema['Tables']['messages']['Row'], 'blurb_message'>>
150150
expectType<TypeEqual<typeof result, typeof expected>>(true)
151151
}
152152

153153
{
154154
const { data } = await rpcQueries['resolvable function with user_row param']
155155
let result: Exclude<typeof data, null>
156-
let expected: Array<Schema['Tables']['messages']['Row']>
156+
let expected: Array<Omit<Schema['Tables']['messages']['Row'], 'blurb_message'>>
157157
expectType<TypeEqual<typeof result, typeof expected>>(true)
158158
}
159159

@@ -287,3 +287,36 @@ type Schema = Database['public']
287287
| SelectQueryError<'Could not choose the best candidate function between: polymorphic_function_with_unnamed_default_overload( => int4), polymorphic_function_with_unnamed_default_overload(). Try renaming the parameters or the function itself in the database so function overloading can be resolved'>
288288
expectType<TypeEqual<typeof result, typeof expected>>(true)
289289
}
290+
291+
{
292+
const { data } = await rpcQueries['function with blurb_message']
293+
let result: Exclude<typeof data, null>
294+
let expected: never
295+
expectType<TypeEqual<typeof result, typeof expected>>(true)
296+
}
297+
298+
{
299+
const { data } = await rpcQueries['function returning row']
300+
let result: Exclude<typeof data, null>
301+
let expected: {
302+
age_range: unknown
303+
catchphrase: unknown
304+
data: unknown
305+
status: 'ONLINE' | 'OFFLINE' | null
306+
username: string
307+
}
308+
expectType<TypeEqual<typeof result, typeof expected>>(true)
309+
}
310+
311+
{
312+
const { data } = await rpcQueries['function returning set of rows']
313+
let result: Exclude<typeof data, null>
314+
let expected: Array<{
315+
age_range: unknown
316+
catchphrase: unknown
317+
data: unknown
318+
status: 'ONLINE' | 'OFFLINE' | null
319+
username: string
320+
}>
321+
expectType<TypeEqual<typeof result, typeof expected>>(true)
322+
}

test/advanced_rpc.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,17 @@ export const rpcQueries = {
214214
'': true,
215215
}
216216
),
217+
// @ts-expect-error the function types doesn't exist and should fail to be retrieved by cache
218+
// for direct rpc call
219+
'function with blurb_message': postgrest.rpc('blurb_messages', {
220+
channel_id: 1,
221+
data: null,
222+
id: 1,
223+
message: 'Hello World 👋',
224+
username: 'supabot',
225+
}),
226+
'function returning row': postgrest.rpc('function_returning_row'),
227+
'function returning set of rows': postgrest.rpc('function_returning_set_of_rows'),
217228
} as const
218229

219230
describe('rpc', () => {
@@ -920,4 +931,97 @@ describe('rpc', () => {
920931
}
921932
`)
922933
})
934+
935+
test('function with blurb_message', async () => {
936+
const res = await rpcQueries['function with blurb_message']
937+
expect(res).toMatchInlineSnapshot(`
938+
Object {
939+
"count": null,
940+
"data": null,
941+
"error": Object {
942+
"code": "PGRST202",
943+
"details": "Searched for the function public.blurb_messages with parameters channel_id, data, id, message, username or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache.",
944+
"hint": "Perhaps you meant to call the function public.get_messages",
945+
"message": "Could not find the function public.blurb_messages(channel_id, data, id, message, username) in the schema cache",
946+
},
947+
"status": 404,
948+
"statusText": "Not Found",
949+
}
950+
`)
951+
})
952+
953+
test('function returning row', async () => {
954+
const res = await rpcQueries['function returning row']
955+
expect(res).toMatchInlineSnapshot(`
956+
Object {
957+
"count": null,
958+
"data": Object {
959+
"age_range": "[1,2)",
960+
"catchphrase": "'cat' 'fat'",
961+
"data": null,
962+
"status": "ONLINE",
963+
"username": "supabot",
964+
},
965+
"error": null,
966+
"status": 200,
967+
"statusText": "OK",
968+
}
969+
`)
970+
})
971+
972+
test('function returning set of rows', async () => {
973+
const res = await rpcQueries['function returning set of rows']
974+
expect(res).toMatchInlineSnapshot(`
975+
Object {
976+
"count": null,
977+
"data": Array [
978+
Object {
979+
"age_range": "[1,2)",
980+
"catchphrase": "'cat' 'fat'",
981+
"data": null,
982+
"status": "ONLINE",
983+
"username": "supabot",
984+
},
985+
Object {
986+
"age_range": "[25,35)",
987+
"catchphrase": "'bat' 'cat'",
988+
"data": null,
989+
"status": "OFFLINE",
990+
"username": "kiwicopple",
991+
},
992+
Object {
993+
"age_range": "[25,35)",
994+
"catchphrase": "'bat' 'rat'",
995+
"data": null,
996+
"status": "ONLINE",
997+
"username": "awailas",
998+
},
999+
Object {
1000+
"age_range": "[20,30)",
1001+
"catchphrase": "'json' 'test'",
1002+
"data": Object {
1003+
"foo": Object {
1004+
"bar": Object {
1005+
"nested": "value",
1006+
},
1007+
"baz": "string value",
1008+
},
1009+
},
1010+
"status": "ONLINE",
1011+
"username": "jsonuser",
1012+
},
1013+
Object {
1014+
"age_range": "[20,30)",
1015+
"catchphrase": "'fat' 'rat'",
1016+
"data": null,
1017+
"status": "ONLINE",
1018+
"username": "dragarcia",
1019+
},
1020+
],
1021+
"error": null,
1022+
"status": 200,
1023+
"statusText": "OK",
1024+
}
1025+
`)
1026+
})
9231027
})

test/db/00-schema.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,24 @@ create or replace function public.polymorphic_function_with_unnamed_default_over
307307
create or replace function public.polymorphic_function_with_unnamed_default_overload(text default 'default') returns text language sql as $$ SELECT 'foo' $$;
308308
create or replace function public.polymorphic_function_with_unnamed_default_overload(bool default true) returns int language sql as 'SELECT 3';
309309

310+
create function public.blurb_message(public.messages) returns character varying as
311+
$$
312+
select substring($1.message, 1, 3);
313+
$$ language sql stable;
314+
315+
316+
create or replace function public.function_returning_row()
317+
returns public.users
318+
language sql
319+
stable
320+
as $$
321+
select * from public.users limit 1;
322+
$$;
323+
324+
create or replace function public.function_returning_set_of_rows()
325+
returns setof public.users
326+
language sql
327+
stable
328+
as $$
329+
select * from public.users;
330+
$$;

test/embeded_functions_join.test-d.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,30 @@ type Schema = Database['public']
130130
}>
131131
expectType<TypeEqual<typeof result, typeof expected>>(true)
132132
}
133+
134+
{
135+
const { data } = await selectQueries.embeded_function_with_blurb_message
136+
let result: Exclude<typeof data, null>
137+
let expected: Array<{
138+
username: string
139+
user_messages: Array<
140+
Pick<Schema['Tables']['messages']['Row'], 'id' | 'message' | 'blurb_message'>
141+
>
142+
}>
143+
expectType<TypeEqual<typeof result, typeof expected>>(true)
144+
}
145+
146+
// Cannot embed an function that is not a setofOptions one
147+
{
148+
const { data } = await selectQueries.embeded_function_returning_row
149+
let result: Exclude<typeof data, null>
150+
let expected: never[]
151+
expectType<TypeEqual<typeof result, typeof expected>>(true)
152+
}
153+
154+
{
155+
const { data } = await selectQueries.embeded_function_returning_set_of_rows
156+
let result: Exclude<typeof data, null>
157+
let expected: never[]
158+
expectType<TypeEqual<typeof result, typeof expected>>(true)
159+
}

0 commit comments

Comments
 (0)