Re-exports GetResult and Generic helpers to type Postgrest Queries
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 :~)
@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
@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?
Hey all, thanks for the wait. I couldn't come up with a stable helper type in time so I've added an unstable helper type as per @olee's suggestion.
I've omitted GenericTable and GenericSchema since these are also internal types and easily reimplemented - here's an example.
:tada: This PR is included in version 1.17.7 :tada:
The release is available on:
Your semantic-release bot :package::rocket:
@soedirgo thanks a lot for updating this, I just upgraded : ) fwiw for anyone else reading here, make sure to check the master branch of the example linked above as the Generic types have changed (I got stuck on this)