Blog post

PostgREST 9

2021-11-274 minute read

PostgREST turns your PostgreSQL database automatically into a RESTful API. Today, PostgREST 9 was released. Let's take a look at some of the new features.

Resource embedding with Inner Joins

PostgREST 9 brings a much-requested feature: the ability to do inner joins when embedding a table.

Here's an example with supabase-js:

1const { data, error } = await supabase
2    .from('messages')
3    .select('*, users!inner(*)')
4    .eq('users.username', 'Jane')
5

With the new !inner keyword, you can now filter rows of the top-level table (messages) based on a filter (eq) of the embedded table (users). This works across all Supabase client libraries and you can use it with any of the available operators (gt, in, etc.)

Read more.

Functions with unnamed parameters

You can now make POST requests to functions with a single unnamed parameter. This is particularly useful for webhooks that send JSON payloads.

For example, imagine you were using Postmark as an email provider and you wanted to save email bounces using their bounce webhook. Previously this wouldn't be possible with PostgREST, as every function required a named parameter.

As of PostgREST 9, this is possible. Simply create a function inside your PostgreSQL database to receive the raw JSON:

1create function store_bounces(json) 
2returns json 
3language sql
4as $$
5  insert into bounces (webhook_id, email)
6  values (
7    ($1->>'ID')::bigint, 
8    ($1->>'Email')::text
9  );
10
11  select '{ "status": 200 }'::json;
12$$;
13

And the webhook can send data directly to your database via an rpc call:

1POST https://<PROJECT_REF>.supabase.co/rest/v1/rpc/store_bounces HTTP/1.1
2Content-Type: application/json
3
4{
5  "RecordType": "Bounce",
6  "MessageStream": "outbound",
7  "ID": 4323372036854775807,
8  "Type": "HardBounce",
9  "MessageID": "883953f4-6105-42a2-a16a-77a8eac79483",
10  "Description": "The server was unable to deliver your message (ex: unknown user, mailbox not found).",
11  "Details": "Test bounce details",
12  "Email": "john@example.com",
13  "From": "sender@example.com",
14  "BouncedAt": "2019-11-05T16:33:54.9070259Z"
15}
16

Read more.

PostgreSQL 14 compatibility

If you've ever done your own custom auth functions using PostgREST HTTP Context, note that a breaking change was necessary for PostgreSQL 14 Compatibility. You'll need to update them:

FromTo
current_setting('request.jwt.claim.custom-claim', true)current_setting('request.jwt.claims', true)::json->>'custom-claim'
current_setting('request.header.custom-header', true)current_setting('request.headers', true)::json->>'custom-header'

If you only use Supabase default auth functions(auth.email(), auth.uid(), auth.role()), then no action is required because we have updated the functions to handle these changes transparently.

Read more.

Release notes

There are a lot more improvements released in PostgREST 9, including support for Partitioned Tables, improved doc, and bug fixes.

You can see the full updates on the PostgREST 9 release notes.

Last post

New in PostgreSQL 14: What every developer should know

28 November 2021
tech
Next post

Supabase Launch Week III: Holiday Special

26 November 2021
launch-week
Related articles
Supabase Beta January 2022
Supabase Beta December 2021
Golden Kitty Awards Ceremony Watch Party with Supabase
Holiday Hackdays Winners 2021
Supabase Beta November 2021: Launch Week Recap
View all posts

Build in a weekend, scale to millions