postgrest-js icon indicating copy to clipboard operation
postgrest-js copied to clipboard

Unable to import helper types since 1.15.6

Open olee opened this issue 1 year ago • 13 comments

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 avatar Jul 11 '24 14:07 olee

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?

soedirgo avatar Jul 15 '24 09:07 soedirgo

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.

olee avatar Aug 08 '24 13:08 olee

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.

tobico avatar Aug 13 '24 06:08 tobico

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?

cco3 avatar Aug 23 '24 14:08 cco3

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 avatar Sep 03 '24 08:09 lauri865

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 avatar Sep 03 '24 09:09 lauri865

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

olee avatar Sep 03 '24 21:09 olee

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 🙏

uncvrd avatar Sep 08 '24 06:09 uncvrd

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

soedirgo avatar Sep 11 '24 23:09 soedirgo

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 avatar Sep 12 '24 23:09 olee

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

olee avatar Sep 26 '24 09:09 olee

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

#560

uncvrd avatar Oct 12 '24 21:10 uncvrd

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

alexgorbatchev avatar Oct 14 '24 22:10 alexgorbatchev

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.

olee avatar Nov 26 '24 08:11 olee

Yea this is holding me up on upgrading as well, we have as PR #560 at your convenience

uncvrd avatar Nov 27 '24 16:11 uncvrd

What is the progress on this regarding replacing this with a proper solution?

Anoian avatar Jun 18 '25 10:06 Anoian

@Anoian we've added support for this here. No progress on adding the stable version of it.

soedirgo avatar Jun 18 '25 13:06 soedirgo