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
impl SupabaseClient
Sourcepub async fn delete(&self, table_name: &str, id: &str) -> Result<(), String>
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
impl SupabaseClient
Sourcepub async fn insert_with_generated_id(
&self,
table_name: &str,
body: Value,
) -> Result<String, String>
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.
Sourcepub async fn insert(
&self,
table_name: &str,
body: Value,
) -> Result<String, String>
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.
Sourcepub async fn insert_without_defined_key(
&self,
table_name: &str,
body: Value,
) -> Result<String, String>
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.
Sourcepub async fn insert_if_unique(
&self,
table_name: &str,
body: Value,
) -> Result<String, String>
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.
Sourcepub async fn bulk_insert<T>(
&self,
table_name: &str,
body: Vec<T>,
) -> Result<(), String>where
T: Serialize,
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
impl SupabaseClient
Sourcepub async fn get_id(
&self,
email: String,
table_name: String,
column_name: String,
) -> Result<String, String>
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 ofSupabaseClient
used to interact with the database.email
- AString
representing the email address to match in the query.table_name
- AString
specifying the name of the table to query.column_name
- AString
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
impl SupabaseClient
Sourcepub fn select(&self, table_name: &str) -> QueryBuilder
pub fn select(&self, table_name: &str) -> QueryBuilder
pub fn from(&self, table_name: &str) -> QueryBuilder
Sourcepub async fn execute(
&self,
table_name: &str,
query_string: &str,
) -> Result<Vec<Value>, String>
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
impl SupabaseClient
Sourcepub async fn update(
&self,
table_name: &str,
id: &str,
body: Value,
) -> Result<String, String>
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
Sourcepub async fn update_with_column_name(
&self,
table_name: &str,
column_name: &str,
id: &str,
body: Value,
) -> Result<String, String>
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§impl SupabaseClient
impl SupabaseClient
Sourcepub fn new(supabase_url: String, private_key: String) -> Result<Self>
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
impl Clone for SupabaseClient
Source§fn clone(&self) -> SupabaseClient
fn clone(&self) -> SupabaseClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more