supabase / postgrest-js

Isomorphic JavaScript client for PostgREST.
https://supabase.com
MIT License
965 stars 129 forks source link

No way to retrieve column list for an empty table #427

Open zachblume opened 1 year ago

zachblume commented 1 year ago

Bug report

Describe the bug

When querying an empty table, you get an empty array. There is no way to access column names. In a normal table, you can access the keys of the first row object.

To Reproduce

const result = await supabase.from("an_empty_table").select("*");

result.data and result.error work normally but produce no information about columns

Expected behavior

Perhaps behind a flag, like count?

const { data } = await supabase.from("an_empty_table").select("*", { columns: true });
console.assert(data.columns?.length > 0);

System information

Additional context

Why? Because I want to show an empty table view with the columns printed out as a loading state before a user adds a row.

E.g., in my case, they are adding a "person" to a "people" table and when people.length == 0 I still want to show the fields as table headers so visually get an impression of the table, without it directly into the code, and I'm not using the typescript client with generated types, etc., in any case it's a reused component that needs to do so regardless of the model being passed to it.

I noticed you all added this to the Studio codebase a couple years ago:

https://github.com/supabase/supabase/issues/94

itsajay1029 commented 1 year ago

Can I work on this @zachblume ?

soedirgo commented 1 year ago

Hey @zachblume, this is outside the scope of the client library - the response from the query to the REST server doesn't include column information. For this you'd need to query the database directly via SQL (e.g. from information_schema.columns), or you can use the OpenAPI response from the REST server which you can get from:

curl '<your API URL>/rest/v1/' -H 'apikey: <your API key>'
zachblume commented 1 year ago

Thanks for the pointer the that endpoint, didn’t realize there was a programmatic one. Should wrap it in a handler for the clients and document it is my $.02

In the end I wrote an RPC to query info schema as you said, but this is better

Nidroide commented 1 year ago

It would be useful to implement a method in supabase-js to query the /rest/v1/ endpoint without having to extract the API key and bearer token and manually make the fetch call, especially when your app uses custom roles in PostgreSQL. In such cases, you can provide information to your frontend to generate menus in a dashboard based on tables and views visibility, which varies based on user privileges with PostgREST."