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

Re-exports GetResult and Generic helpers to type Postgrest Queries #560

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

uncvrd
Copy link

@uncvrd uncvrd commented Oct 12, 2024

What kind of change does this PR introduce?

Hi all! This PR simply re-exports a couple helper types that many of us were using to strongly type our query responses

Please let me know if I can help improve this!

What is the current behavior?

Ever since 1.15.6, these types had been removed, they were internal but recent discussion is that these should be re-exposed

It used to be

import { GenericSchema, GenericTable } from "@supabase/postgrest-js/dist/module/types";
import { GetResult } from "@supabase/postgrest-js/dist/module/select-query-parser";

Please link any relevant issues here.

See #551

What is the new behavior?

It is now:

import { GetResult, GenericSchema, GenericTable } from "@supabase/postgrest-js";

In order to do something like the following:

type Collection = SelectPublic<"Collection", "id,imageObjectId,type,name,workspaceId">

We can build a helper type like this:

export type SchemaKeyType = keyof DB;
export type DatabaseTableKeyType = keyof DB["public"]["Tables"];
type RawSelect<
    SchemaName extends SchemaKeyType,
    TableName extends DatabaseTableKeyType,
    Query extends string = "*",
    Schema extends GenericSchema = DB[SchemaName],
    Table extends GenericTable = Schema["Tables"][TableName],
    Relationships = Table extends { Relationships: infer R } ? R : unknown
> = GetResult<Schema, Table["Row"], TableName, Relationships, Query>;

export type SelectPublic<Table extends DatabaseTableKeyType, Query extends string = "*"> = RawSelect<"public", Table, Query>;

In order to do this we need access to: GetResult, GenericSchema, and GenericTable

So I've simply exported those from their respective files.

Now we get our get our types back :~)

image

@alexgorbatchev
Copy link

alexgorbatchev commented Oct 14, 2024

@olee @soedirgo please merge this

as a side note, this is used by supabase-cache-helpers which has a full video on the Supabase YouTube channel

@olee
Copy link

olee commented Nov 5, 2024

@alexgorbatchev afaik the GetResult type should still be considered internal and there was no intention to export it. (see this comment).
Instead, a proper helper type should be exported publicly, like eg. this:

// Exported from postgrest-js
export type TablesSelect<
    Schema extends GenericSchema,
    TableName extends ???,
    Query extends string = '*'
> = ...;

// usage:
type MyEntitySelected = TablesSelect<Database['public'], 'my_table', 'id,title'>;

However, as the current state of the library causes quite some issues with various users and even libraries depending on the exported GetResult I'd suggest exporting it with an unstable prefix eg. Unstable_GetResult<...>.
What do you think @soedirgo?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants