postgrest-js
postgrest-js copied to clipboard
No way to retrieve column list for an empty table
Bug report
- [X] I confirm this is a bug with Supabase, not with my own application.
- [X] I confirm I have searched the Docs, GitHub Discussions, and Discord.
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
- Version of supabase-js: 2.13.1
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
I noticed you all added this to the Studio codebase a couple years ago:
https://github.com/supabase/supabase/issues/94
Can I work on this @zachblume ?
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>'
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
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."