postgrest-js
postgrest-js copied to clipboard
Type error when ordering by a foreign column
Bug report
Describe the bug
A type error is shown if trying to order by a foreign table.
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
let baseQuery = getSupabaseClient()
.from<AuthorTypeWithBooks>("author")
.select("*, books:book!authorId!inner(*)")
.order("title", { foreignTable: "book" });
=> TS2345: Argument of type '"title"' is not assignable to parameter of type 'keyof AuthorType | "books"'.
Relevant type definitions:
export type AuthorTypeWithBooks = AuthorType & {
books?: BookType[];
};
export type AuthorType = {
id?: string;
name?: string;
};
export type BookType = {
id?: string;
authorId?: string;
title?: string;
};
Expected behavior
No type error occurs.
Screenshots
Not applicable.
System information
- OS: Windows
- Browser (if applies): chrome
- Version of supabase-js: 1.35.6
- Version of Node.js: v16.13.0
Additional context
It is possible to get around this with a workaround injecting title into the type:
let baseQuery = getSupabaseClient()
.from<AuthorTypeWithBooks & { title: string }>("author")
.select("*, books:book!authorId!inner(*)")
.order("title", { foreignTable: "book" });
Can you do it with @supabase/supabase-js@rc? We'll be releasing it as @2 in a few weeks. @supabase/supabase-js@1 likely won't be updated anymore.
Thanks for the quick reply. I haven't found the time to test yet and am going on vacation, I'll try to take a look afterwards