Struct SupabaseClient

Source
pub struct SupabaseClient { /* private fields */ }
Expand description

A client structure for interacting with Supabase services.

This structure holds the necessary details to make requests to the Supabase API. It contains the base URL of the Supabase project and the API key for authentication.

§Fields

  • url: The base URL of the Supabase project.
  • api_key: The API key used for authenticating requests to Supabase.

Implementations§

Source§

impl SupabaseClient

Source

pub async fn delete(&self, table_name: &str, id: &str) -> Result<(), String>

Deletes a row in the specified table based on the provided ID.

§Arguments
  • table_name - A string slice that holds the name of the table from which to delete.
  • id - A string slice that holds the ID of the row to delete.
  • body - A JSON value containing the body of the request, typically specifying conditions for deletion.
§Returns

This method returns a Result<(), String>. On success, it returns Ok(()), and on failure, it returns Err(String) with an error message.

§Examples
use serde_json::json;
use supabase_rs::SupabaseClient;

#[tokio::main]
async fn main() {
    let client = SupabaseClient::new(
        "your_supabase_url".to_string(),
        "your_supabase_key".to_string()
    ).unwrap();
    let result = client.delete("your_table_name", "row_id").await;
    match result {
        Ok(_) => println!("Row deleted successfully"),
        Err(e) => println!("Failed to delete row: {}", e),
    }
}
Source§

impl SupabaseClient

Source

pub async fn insert_with_generated_id( &self, table_name: &str, body: Value, ) -> Result<String, String>

Inserts a new row into the specified table with a randomly generated ID for column id.

§Arguments
  • table_name - A string slice that holds the name of the table.
  • body - A JSON value containing the data to be inserted.
§Example
// Initialize the Supabase client
use supabase_rs::SupabaseClient;
let client = SupabaseClient::new("your_supabase_url", "your_supabase_key");

// This will insert a new row into the table
let insert_result = client.insert(
  "your_table_name",
  json!(
    {"column_name": "value"}
  )
).await;
§Returns

This method returns a Result<String, String>. On success, it returns Ok(String) with the new row’s ID, and on failure, it returns Err(String) with an error message.

Source

pub async fn insert( &self, table_name: &str, body: Value, ) -> Result<String, String>

Inserts a new row into the specified table.

§Arguments
  • table_name - A string slice that holds the name of the table.
  • body - A JSON value containing the data to be inserted.
§Example
// Initialize the Supabase client
let client = SupabaseClient::new("your_supabase_url", "your_supabase_key");

// This will insert a new row into the table
let insert_result = client.insert(
  "your_table_name",
  json!(
    {
        "id": "your_id", // Optional
        "column_name": "value"
    }
  )
).await;
§Returns

This method returns a Result<String, String>. On success, it returns Ok(String) with the new row’s ID, and on failure, it returns Err(String) with an error message.

Source

pub async fn insert_without_defined_key( &self, table_name: &str, body: Value, ) -> Result<String, String>

Inserts a new row into the specified table with a user-defined ID or Supabase backend generated ID. This method is identical to the insert method.

§Arguments
  • table_name - A string slice that holds the name of the table.
  • body - A JSON value containing the data to be inserted.
§Example
// Initialize the Supabase client
let client = SupabaseClient::new("your_supabase_url", "your_supabase_key");

// This will insert a new row into the table
let insert_result = client.insert(
  "your_table_name",
  json!(
    {
        "id": "your_id", // Optional
        "column_name": "value"
    }
  )
).await;
§Returns

This method returns a Result<(), String>. On success, it returns Ok(()), and on failure, it returns Err(String) with an error message.

Source

pub async fn insert_if_unique( &self, table_name: &str, body: Value, ) -> Result<String, String>

Inserts a row into the specified table if the value is unique and does not exist in the table already.

§Arguments
  • table_name - A string slice that holds the name of the table.
  • body - A JSON value containing the data to be inserted.
§Example
#[tokio::main]
async fn main() {
    // Initialize the Supabase client
    let client = SupabaseClient::new("your_supabase_url".to_string(), "your_supabase_key".to_string()).unwrap();

    // This will insert a new row into the table if the value is unique
    let unique_insert_result = client.insert_if_unique(
        "your_table_name",
        json!({"unique_column_name": "unique_value"})
    ).await;
}
§Returns

This method returns a Result<String, String>. On success, it returns Ok(String) with the new row’s ID, and on failure, it returns Err(String) with an error message indicating a duplicate entry.

Source

pub async fn bulk_insert<T>( &self, table_name: &str, body: Vec<T>, ) -> Result<(), String>
where T: Serialize,

Inserts new rows into the specified table in bulk.

§Arguments
  • table_name - A string slice that holds the name of the table.
  • body - A vector of serializable values to be inserted.
§Example
// Initialize the Supabase client

// A struct that implements the Serialize trait
#[derive(Serialize)]
pub struct User {
  name: String,
}

let client = SupabaseClient::new("your_supabase_url", "your_supabase_key");

// Create the body of the request as a vector of JSON values
let body: Vec<Value> = vec![
    json!({"column_name": "value"}),
    json!({"column_name": "value"}),
    User { name: "Alice".to_string() },
];

// This will insert a new row into the table
let insert_result = client.insert("your_table_name", body).await;
§Returns

This method returns a Result<(), String>. On success, it returns Ok(()), and on failure, it returns Err(String) with an error message.

Source§

impl SupabaseClient

Source

pub async fn get_id( &self, email: String, table_name: String, column_name: String, ) -> Result<String, String>

Retrieves the ID of a row from a specified table based on a matching email address.

§Arguments
  • supabase_client - An instance of SupabaseClient used to interact with the database.
  • email - A String representing the email address to match in the query.
  • table_name - A String specifying the name of the table to query.
  • column_name - A String specifying the name of the column to match against the email.
§Returns

Returns a Result<String, String>:

  • Ok(String) containing the ID of the row if found.
  • Err(String) containing an error message if the query fails or if no matching row is found.
§Examples
#[tokio::main]
async fn main() {
    let supabase_client = SupabaseClient::new(
        "your_supabase_url".to_string(),
        "your_supabase_key".to_string()
    ).unwrap();
    let email = "example@email.com".to_string();
    let table_name = "users".to_string();
    let column_name = "email".to_string();
    match supabase_client.get_id(email, table_name, column_name).await {
        Ok(id) => println!("Found ID: {}", id),
        Err(e) => println!("Error: {}", e),
    }
}
Source§

impl SupabaseClient

Source

pub fn select(&self, table_name: &str) -> QueryBuilder

Initializes a QueryBuilder for a specified table.

§Arguments
  • table_name - A string slice that holds the name of the table to be queried.
§Returns

A QueryBuilder instance configured for the specified table.

Source

pub fn from(&self, table_name: &str) -> QueryBuilder

Source

pub async fn execute( &self, table_name: &str, query_string: &str, ) -> Result<Vec<Value>, String>

Executes a query against a specified table with a given query string.

§Arguments
  • table_name - A string slice that holds the name of the table to be queried.
  • query_string - A string slice that holds the query parameters.
§Returns

A Result which is either a vector of Value representing the records fetched from the database or a String error message in case of failure.

§Errors

This function will return an error if the HTTP request fails or if the server returns a non-success status code.

Source§

impl SupabaseClient

Source

pub async fn update( &self, table_name: &str, id: &str, body: Value, ) -> Result<String, String>

Updates a row in the table, based on the id

Source

pub async fn update_with_column_name( &self, table_name: &str, column_name: &str, id: &str, body: Value, ) -> Result<String, String>

Updates a row in the table, based on the column name

Source

pub async fn upsert( &self, table_name: &str, id: &str, body: Value, ) -> Result<String, String>

Creates a row in the table, or updates if the id already exists

Source

pub async fn upsert_without_defined_key( &self, table_name: &str, body: Value, ) -> Result<(), String>

Creates a row in the table, or updates if the row already exists

This method does not require a defined key in the body unlike the upsert method.

Source§

impl SupabaseClient

Source

pub fn new(supabase_url: String, private_key: String) -> Result<Self>

Creates a new instance of SupabaseClient using the provided Supabase URL and private API key.

This function is crucial for setting up the client with the necessary credentials to interact with Supabase services. The supabase_url should point to your Supabase project URL, and the private_key should be your secret API key.

§Examples
let client = SupabaseClient::new(
    "https://your-project.supabase.co".to_string(),
    "your-secret-key".to_string(),
);

Trait Implementations§

Source§

impl Clone for SupabaseClient

Source§

fn clone(&self) -> SupabaseClient

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SupabaseClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,