DEV Community

Cover image for A Rust Client for PostgREST
Bobbie Soedirgo for Supabase

Posted on

A Rust Client for PostgREST

At Supabase, we rely heavily on PostgREST, an open source tool that turns your Postgres database into a RESTful API. We even have our own JavaScript client for it in the form of postgrest-js.

But I use Rust, and it's required by my religion to rewrite everything in Rust, so rewrite it I did. To wit: postgrest-rs 🦀.

🤔 What can I do with it?

postgrest-rs brings an ORM interface to PostgREST. This means you can interact with Postgres (through PostgREST) from within Rust. For example:

use postgrest::Postgrest;

let client = Postgrest::new("https://your.postgrest.endpoint");
let resp = client
    .from("table")
    .select("*")
    .execute()
    .await?;
Enter fullscreen mode Exit fullscreen mode

🤷‍♀️ Why would I want it?

Say I have a table of users, and I want to know the name of the last user that logged on. In PostgREST, you do this by making the following request:

GET https://your.postgrest.endpoint/users?select=username&order=last_seen.desc HTTP/1.1
Accept: application/vnd.pgrst.object+json
Enter fullscreen mode Exit fullscreen mode

This gets cumbersome and error-prone as queries get more complex. Compare this to its equivalent in postgrest-rs, which feels more at home:

let client = Postgrest::new("https://your.postgrest.endpoint");
let resp = client
    .from("users")
    .select("username")
    .order("last_seen.desc")
    .single()
    .execute()
    .await?;
Enter fullscreen mode Exit fullscreen mode

There are many other cool stuff you can do, such as switching schemas, row filtering, calling stored procedures, and much more. You can check out the repo here and play around with it.

And there you have it! 🎉

This project is part of my internship at Supabase. I saw that there was an interest in a Rust client library, and offered to work on that, among other things. Soon after, I'm writing a Rust library, and it was great! So credit where it's due: the Supabase team for all the support, and of course, PostgREST!


We'll announce all our future features with more freebies here on DEV first. Follow us so that you don't miss out.

Sign up for our early alpha!

Follow us on dev.to

Top comments (0)