Many people think of the @supabase/supabase-js library as an ORM or a query builder, but in reality, it’s neither. Instead, it provides a clean API layer for many of Supabase’s services. In this discussion, we’ll focus only on the .from portion of the library, as this is where its querying capabilities live.

.from()

There’s a common misconception that this part of the JavaScript library is an object relational mapper (ORM) or a query builder. Under the hood, however, it’s simply constructing a RESTful API URL path. That request is then sent to a server, PostgREST in this case, which handles query construction and executes it against your database.

This means you aren’t writing SQL in your client code, nor are you sending SQL over the wire to the server.

Sending SQL over the wire in itself has some flaws especially when doing such a thing from the client side (in the browser).

Instead we are just constructing a URL that is mapped to a PostgREST endpoint that will interpret it, construct the SQL query, execute it against your database and then return the resulting data to the client library.

Lets take a look at one of such API calls and write a simplified example of what happens on the PostgREST side of things.

The JavaScript call

await supabase.from('profiles').select('*')

The API call the above would make would look like

https://sjpsjonqoxunyrtnxlkj.supabase.co/rest/v1/profiles?select=*

And a simplified version of the resulting query would look like

SELECT * FROM profiles;

Here we can see that the library is just converting our elegrant JavaScript API code to a url path for our server. In the above you can see profiles which is our table name, but in terms of a restful endpoint its our resource name. Following that is a query parameter which contains the action (select) we would like to perform along with the value (*) that action contains.

This is a simplified version of what happens under the hood when you use the @supabase/supabase-js library. PostgREST is a powerful server layer that will transform the restful endpoint into an optimized SQL query.

Supabase has a really nice tool in their documentation to help with translating an SQL query into the PostgREST equivalent. This is good for simple queries but not very complex ones.

SQL to REST API Translator