Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to import helper types since 1.15.6 #551

Open
olee opened this issue Jul 11, 2024 · 13 comments
Open

Unable to import helper types since 1.15.6 #551

olee opened this issue Jul 11, 2024 · 13 comments
Labels
bug Something isn't working

Comments

@olee
Copy link

olee commented Jul 11, 2024

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.

@olee olee added the bug Something isn't working label Jul 11, 2024
@soedirgo
Copy link
Member

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?

@olee
Copy link
Author

olee commented Aug 8, 2024

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.

@tobico
Copy link

tobico commented Aug 13, 2024

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.

@cco3
Copy link

cco3 commented Aug 23, 2024

If I understand correctly, there were three way to get result types:

  1. Use GetResult, etc.
  2. Run the query and get the type of the result
  3. 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?

@lauri865
Copy link

lauri865 commented Sep 3, 2024

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.

@lauri865
Copy link

lauri865 commented Sep 3, 2024

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

@olee
Copy link
Author

olee commented Sep 3, 2024

@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 🤔

@uncvrd
Copy link

uncvrd commented Sep 8, 2024

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 🙏

@soedirgo
Copy link
Member

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

@olee
Copy link
Author

olee commented Sep 12, 2024

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?

@olee
Copy link
Author

olee commented Sep 26, 2024

Hey, is there any progress on this or is there anything we could help with?

@uncvrd
Copy link

uncvrd commented Oct 12, 2024

Hi all, in an attempt to help move things along I made a simple PR. Let me know if this can be improved :)

#560

@alexgorbatchev
Copy link

alexgorbatchev commented Oct 14, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants