13 stable releases

1.6.0 Jul 25, 2023
1.5.0 Jan 16, 2023
1.3.2 Nov 22, 2022
1.3.1 Jul 29, 2022
0.1.0 May 24, 2020

#69 in Database interfaces

Download history 789/week @ 2023-12-11 776/week @ 2023-12-18 731/week @ 2023-12-25 627/week @ 2024-01-01 714/week @ 2024-01-08 1268/week @ 2024-01-15 1040/week @ 2024-01-22 1386/week @ 2024-01-29 982/week @ 2024-02-05 697/week @ 2024-02-12 1186/week @ 2024-02-19 1428/week @ 2024-02-26 1228/week @ 2024-03-04 1257/week @ 2024-03-11 878/week @ 2024-03-18 658/week @ 2024-03-25

4,172 downloads per month
Used in http-sense

Apache-2.0 OR MIT

55KB
738 lines

postgrest-rs

Build Crate API License: Apache-2.0 OR MIT

PostgREST client-side library 🦀. This library provides an ORM interface to PostgREST.

Usage

Add this to your Cargo.toml:

[dependencies]
postgrest = "1.0"

Simple example:

use postgrest::Postgrest;

let client = Postgrest::new("https://your.postgrest.endpoint");
let resp = client
    .from("your_table")
    .select("*")
    .execute()
    .await?;
let body = resp
    .text()
    .await?;

Simple example with JWT auth

use postgrest::Postgrest;

let client = Postgrest::new("https://your.postgrest.endpoint");
let resp = client
    .from("your_table")
    .auth("VerySensitiveJWTValueAsStringOrStr")
    .select("*")
    .execute()
    .await?;
let body = resp
    .text()
    .await?;

Simplified example using a custom header (e.g. for API gateway authentication with Supabase).

use postgrest::Postgrest;

let client = Postgrest::new("https://your.supabase.endpoint/rest/v1/")
    .insert_header("apikey", "ExampleAPIKeyValue"); // EXAMPLE ONLY!
// Don't actually hard code this value, that's really bad. Use environment
// variables like with the dotenv(https://crates.io/crates/dotenv) crate to inject
let resp = client
    .from("your_table")
    .select("*")
    .execute()
    .await?;
let body = resp
    .text()
    .await?;

Secure example with authenticated API gateway using the dotenv crate to correctly retrieve sensitive values.

use postgrest::Postgrest;
use dotenv;

dotenv::dotenv().ok(); 

let client = Postgrest::new("https://your.supabase.endpoint/rest/v1/")
    .insert_header(
        "apikey",
        dotenv::var("SUPABASE_PUBLIC_API_KEY").unwrap())
let resp = client
    .from("your_table")
    .select("*")
    .execute()
    .await?;
let body = resp
    .text()
    .await?;

Building Queries

These examples assume you've already initialized the client. The methods .from() and .rpc() initalizes the query builder inside the client.

Using filters:

let resp = client
    .from("your_table")
    .eq("country", "Germany")
    .gte("id", "20")
    .select("*")
    .execute()
    .await?;

Updating a table:

let resp = client
    .from("your_table")
    .eq("username", "soedirgo")
    .update("{\"organization\": \"supabase\"}")
    .execute()
    .await?;

Executing stored procedures:

let resp = client
    .rpc("add", "{\"a\": 1, \"b\": 2}")
    .execute()
    .await?;

Not enough filters:

let resp = client
    .from("countries")
    .eq("name", "New Zealand")                        // You can filter for equality...
    .gt("id", "20")
    .lt("id", "20")
    .gte("id", "20")
    .lte("id", "20")
    .like("name", "%United%")                         // ...do pattern matching...
    .ilike("name", "%United%")
    .is("name", "null")
    .in_("name", vec!["China", "France"])
    .neq("name", "China")
    .fts("phrase", "The Fat Cats", Some("english"))   // ...do full text search...
    .plfts("phrase", "The Fat Cats", None)
    .phfts("phrase", "The Fat Cats", Some("english"))
    .wfts("phrase", "The Fat Cats", None)
    .cs("countries", "(10,20)")
    .cd("countries", "(10,20)")
    .ov("population_range", "(100,500)")
    .sl("population_range", (100, 500))               // ...and range operations!
    .sr("population_range", (100, 500))               // Find out more about the filters at:
    .nxl("population_range", (100, 500))              // https://postgrest.org/en/stable/api.html#operators
    .nxr("population_range", (100, 500))
    .adj("population_range", (100, 500))
    .select("*")
    .execute()
    .await?;

Check out the API docs for more info!

Contributing

Contributions are welcome! There might be some features you want in, or some unclear documentation, or a bug—either way, feel free to create an issue, and we'll work it out!

Boring stuff below.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as below, without any additional terms or conditions.

License

Licensed under either of

at your option.


Dependencies

~3–16MB
~227K SLoC