postgrest-js
postgrest-js copied to clipboard
Unable to import helper types since 1.15.6
Bug report
Describe the bug
Before 1.15.6 it was possible to import helper types from @supabase/postgrest-js to perform type mappings etc. like this:
import type { GetResult } from "@supabase/postgrest-js/dist/module/select-query-parser";
export type TablesSelect<
Schema extends GenericSchema,
TableName extends keyof Schema['Tables'],
Query extends string,
> = GetResult<
Schema,
Schema['Tables'][TableName]['Row'],
TableName,
Schema['Tables'][TableName] extends { Relationships: infer R } ? R : never,
Query
>;
However, starting from version 1.15.6 with the change to the package build (package.json etc), it became impossible to import any files from subdirectories of the build because of the exports configuration.
Expected behavior
All types of the library which are publicly exposed as part of generics, function arguments and whatsoever should be properly exported imho so that it is possible to properly type code depending on postgrest-js.
The easiest solution would probably be to export helper types through some namespace from index.ts.
Hi! These helper types have always been internal and subject to breaking changes - they're not part of the API. Can you share your use case and why you need them?
The use case is exactly as described above: To generate types for table selects.
For example to write a component that accepts some data which has been selected somewhere else:
function MyUser(props: { entity: TablesSelect<'users', 'id,first_name,last_name,avatar'> }) {
return (
<div>...render user here...</div>
);
}
// ... at some other location
await supabaseClient.from('users').select('id,first_name,last_name,avatar')....;
This is extremly useful and currently prevents us from upgrading supabase client as this is not possible anymore now.
It also makes sense, as the other helper types (Tables, TablesInsert, TablesUpdate) are available, but no type is exported to get a subset of a table.
Instead of exposing the internal type like GetResult, I would like even more for a TablesSelect type to be exported for everyone to use as part of the public API because it is extremely useful.
I'm also using GetResult extensively as the data fetched from my queries is passed around various places in my frontend and I want to correctly type all components and functions that process it.
If I understand correctly, there were three way to get result types:
- Use GetResult, etc.
- Run the query and get the type of the result
- Wrap a query in a function and get the return type of the function
1 wasn't supported and was somewhat painful. 2 doesn't give much flexibility for creating helper functions etc. 3 is heavy.
Could we please get a supported way of creating query result types that doesn't require running or wrapping a query?
This is a major breaking change for us, making developer experience much worse. Any kind of higher order abstractions that pass through types become much more difficult to build.
Internal or not, since there's no other good alternative, the community has been forced to use it, as you can see below: https://github.com/search?q=postgrest-js+%22select-query-parser%22&type=code
@lauri865 that's some really nice evidence there - didn't know soo many others had to "hack" their way into the lib to find that GetResult type and figure out how to use it etc. That's why I would really like for there to be a proper, official way of doing this.
If I had some time at hand I would love to make a PR myself on this, but currently that's not possible for me.
Another alternative could be something like using pnpm patch for now to apply the necessary changes for exposing the type locally 🤔
Also would like to +1 this, it makes it extremely easy to be able to type the results from a query. I was inspired by the Supabase Cache Helpers library which uses this method heavily too. Would really appreciate if this was provided externally 🙏
Thanks all, seems like a proper support is needed here. I'll see if this can be accomplished with the already exported QueryResult or, failing that, we'll reexport GetResult
That's wonderful news! The only other option I'd have taken soon would have been completely copying the required types of GetResult into our own codebase which I'd really like to avoid.
btw @soedirgo: Isn't QueryResult pretty much the same as the typescript inbuilt Awaited<T> type?
Hey, is there any progress on this or is there anything we could help with?
Hi all, in an attempt to help move things along I made a simple PR. Let me know if this can be improved :)
#560
As an additional +1 here, I also want to mention that this is used by supabase-cache-helpers which has a full video on the Supabase YouTube channel
Hey, sorry to bother you @soedirgo, but think this might be implemented?
I mean we are not talking about any large feature, just about exporting an already existing type (either directly, with an Unstable_ prefix or through a proper helper) which otherwise blocks quite a few users from upgrading their version of postgrest-js.
I would make a PR, but that wouldn't make sense without a decision on someone from the maintainer side on which solution to opt for yet I think.
Yea this is holding me up on upgrading as well, we have as PR #560 at your convenience
What is the progress on this regarding replacing this with a proper solution?
@Anoian we've added support for this here. No progress on adding the stable version of it.