8 releases

new 0.2.5 Apr 28, 2024
0.2.4 Apr 24, 2024
0.2.2 Feb 4, 2024
0.2.1 Jan 26, 2024
0.1.1 Jan 26, 2024

#1 in #supabase

Download history 46/week @ 2024-01-21 5/week @ 2024-02-11 3/week @ 2024-02-18 7/week @ 2024-02-25 6/week @ 2024-03-03 10/week @ 2024-03-10 6/week @ 2024-03-17 121/week @ 2024-03-24 99/week @ 2024-03-31 41/week @ 2024-04-07 187/week @ 2024-04-14 230/week @ 2024-04-21

635 downloads per month
Used in 3 crates

MIT license

71KB
706 lines

supabase_rs

supabase_rs is an extremely light weight Supabase SDK for interacting with it's database.

I'm actively covering the entire Supabase API including Auth, Realtime, Storage etc

Feature flags

  • storage: Enables the Storage module to interact with Supabase Storage.

Database Features

  • Updating
  • Inserting
  • Inserting if unique
  • Bulk Inserting
  • Upserting
  • Bulk Upserting
  • Delete (only per ID)
  • Select
  • Applying Filters
  • Counting total records

Advanced Filtering over select()

  • Column is equal to a value
  • Column is not equal to a value
  • Column is greater than a value
  • Column is less than a value
  • Column is greater than or equal to a value
  • Column is less than or equal to a value
  • Order the results
  • Limit the number of rows returned
  • Retrieve as a CSV

Storage

  • Downloading a file from a public bucket
  • Saving a file
  • Saving a file to a private bucket
  • Uploading a file
  • Generating a signed url
  • Deleting a file

Auth

// coming soon //

Realtime

// coming soon //

Supabase SDK for Rust

This is an unofficial Rust SDK for Supabase, since there is no official SDK for Rust yet.

Features

  • Insert: Add new rows to a table.
  • Insert if unique: Add a new row only if it does not violate a UNIQUE constraint.
  • Update: Modify existing rows in a table based on a unique identifier.
  • Select: Insert a new row into a table if it does not exist, or update it if it does.
  • Select with count: Select rows from a table and count the number of rows that match the filter criteria.
  • Select with filter: Select rows from a table based on a filter criteria.
  • Select with filter and count: Select rows from a table based on a filter criteria and count the number of rows that match the filter criteria.
  • Delete: Delete a row from a table based on a unique identifier.

Feature flags

  • storage: Enables the Storage module to interact with Supabase Storage.

Cargo.toml

[dependencies]
supabase-rs = "0.2.5"

// With the [storage] feature
supabase-rs = { version = "0.2.5", features = ["storage"] }

//!

Usage

First make sure you have initialized the Supabase Client Initalizing the SupabaseClient //!

Authentication

The Supabase Client is initialized with the Supabase URL and the Supabase Key. Which are environment variables that can be set in a .env file under the following names or any other

SUPABASE_URL=
SUPABASE_KEY=

Examples

Initialize the Supabase Client

use supabase_rs::SupabaseClient;

use dotenv::dotenv;
use std::env::var;

async fn initialize_supabase_client() -> SupabaseClient {
   dotenv().ok(); // Load the .env file

   let supabase_client: SupabaseClient = SupabaseClient::new(
       var("SUPABASE_URL").unwrap(),
       var("SUPABASE_KEY").unwrap()
       );

   supabase_client
}

This will initialize the Supabase Client with the Supabase URL and the Supabase Key, and return the Supabase Client to be passed to other methods.

Insert

This will insert a new row into the test table with the value value_test in the dog column.

// i know the imports are self explanatory but it makes it easier for beginners:)
use serde_json::json;
use supabase_rs::SupabaseClient;

// always pass an initialized SupabaseClient to the method
let client = SupabaseClient::new(
    "your_supabase_url", "your_supabase_key"
);

async fn insert_example(
   client: SupabaseClient
) -> Result<(), String> {
    let insert_result = client
        .insert(
            "test", 
            json!({
                "dog": "value_test"
            }),
       ).await;

Insert if unique

This will insert a new row into the test table with the value value_test in the dog column if the value is unique. It's a drop-in replacement for insert without relying on Supabase's unique constraints on the database.

use serde_json::json;
use supabase_rs::SupabaseClient;

// always pass an initialized SupabaseClient to the method
let client = SupabaseClient::new(
    "your_supabase_url", "your_supabase_key"
);

async fn insert_example(
   client: SupabaseClient
) -> Result<(), String> {
    let insert_result = client
        .insert_if_unique(
            "test", 
            json!({
                "dog": "value_test"
            }),
       ).await;

Update

This will update the row in the test table with the value value_test in the dog column where the id is 1.

// i know the imports are self explanatory but it makes it easier for beginners:)
use serde_json::json;
use supabase_rs::SupabaseClient;

// always pass an initialized SupabaseClient to the method
let client = SupabaseClient::new(
   "your_supabase_url", "your_supabase_key"
);

async fn update_example(
  client: SupabaseClient
) -> Result<(), String> {
   let update_result = client
      .update(
         "test", // the table name
         "1",    // the id of the row to update
         json!({
           "dog": "value_test"  // the new value
         }),
     ).await;

Select

This will return all dog rows where the value is scooby in the animals table

use supabase_rs::SupabaseClient;

// always pass an initialized SupabaseClient to the method
let client = SupabaseClient::new(
   "your_supabase_url", "your_supabase_key"
);

async fn select_scooby(
   supabase_client: SupabaseClient
) -> Result<(), String> {

let data: Result<Vec<Value>, String> = supabase_client
   .select("animals")
   .eq("dog", "scooby")
   .execute()
   .await; 

Select with Count

This will return all dog rows where the value is scooby in the animals table and count the number of rows that match the filter criteria.

use supabase_rs::SupabaseClient;

// always pass an initialized SupabaseClient to the method
let client = SupabaseClient::new(
  "your_supabase_url", "your_supabase_key"
);

async fn select_scooby_with_count(
  supabase_client: SupabaseClient
) -> Result<(), String> {
 let data: Result<Vec<Value>, String> = supabase_client
   .select("animals")
   .count()
   .execute()
   .await;

Select with Filter

This will return all dog rows where the value is scooby in the animals table

use supabase_rs::SupabaseClient;

// always pass an initialized SupabaseClient to the method
let client = SupabaseClient::new(
  "your_supabase_url", "your_supabase_key"
);

async fn select_scooby_with_filter(
 supabase_client: SupabaseClient
) -> Result<(), String> {
let data: Result<Vec<Value>, String> = supabase_client
    .select("animals")
    .eq("dog", "scooby")
    .execute()
    .await;

Selecting with Filter and Count

This will return all dog rows where the value is scooby in the animals table and count the number of rows that match the filter criteria.

use supabase_rs::SupabaseClient;

// always pass an initialized SupabaseClient to the method
let client = SupabaseClient::new(
 "your_supabase_url", "your_supabase_key"
);

async fn select_scooby_with_filter_and_count(
supabase_client: SupabaseClient
) -> Result<(), String> {
let data: Result<Vec<Value>, String> = supabase_client
    .select("animals")
    .eq("dog", "scooby")
    .count()
    .execute()
    .await;

Delete

This will delete the row in the test table where the id is 1.

// i know the imports are self explanatory but it makes it easier for beginners:)
use supabase_rs::SupabaseClient;

// always pass an initialized SupabaseClient to the method
let client = SupabaseClient::new(
  "your_supabase_url", "your_supabase_key"
);

async fn delete_example(
 client: SupabaseClient
) -> Result<(), String> {
let delete_result = client
    .delete("test", "1

Dependencies

~6–18MB
~253K SLoC