pub struct Database { /* private fields */ }
Expand description
Database client for REST API operations
Implementations§
Source§impl Database
impl Database
Sourcepub fn new(
config: Arc<SupabaseConfig>,
http_client: Arc<HttpClient>,
) -> Result<Self>
pub fn new( config: Arc<SupabaseConfig>, http_client: Arc<HttpClient>, ) -> Result<Self>
Create a new Database instance
Sourcepub fn from(&self, table: &str) -> QueryBuilder
pub fn from(&self, table: &str) -> QueryBuilder
Start a query from a table
Sourcepub fn insert(&self, table: &str) -> InsertBuilder
pub fn insert(&self, table: &str) -> InsertBuilder
Insert data into a table
Sourcepub fn upsert(&self, table: &str) -> InsertBuilder
pub fn upsert(&self, table: &str) -> InsertBuilder
Upsert data into a table (insert or update if exists)
§Examples
let client = Client::new("http://localhost:54321", "test-key").unwrap();
// Upsert a user (insert if new, update if exists)
let user: Vec<Value> = client.database()
.upsert("users")
.values(json!({
"id": 1,
"name": "John Doe",
"email": "john@example.com"
}))
.unwrap()
.execute()
.await
.unwrap();
Sourcepub async fn bulk_insert<T>(
&self,
table: &str,
data: Vec<JsonValue>,
) -> Result<Vec<T>>where
T: for<'de> Deserialize<'de>,
pub async fn bulk_insert<T>(
&self,
table: &str,
data: Vec<JsonValue>,
) -> Result<Vec<T>>where
T: for<'de> Deserialize<'de>,
Bulk insert multiple records at once
§Examples
let client = Client::new("http://localhost:54321", "test-key").unwrap();
// Insert multiple users at once
let users: Vec<Value> = client.database()
.bulk_insert("users", vec![
json!({"name": "Alice", "email": "alice@example.com"}),
json!({"name": "Bob", "email": "bob@example.com"}),
json!({"name": "Charlie", "email": "charlie@example.com"})
])
.await
.unwrap();
Sourcepub async fn bulk_upsert<T>(
&self,
table: &str,
data: Vec<JsonValue>,
) -> Result<Vec<T>>where
T: for<'de> Deserialize<'de>,
pub async fn bulk_upsert<T>(
&self,
table: &str,
data: Vec<JsonValue>,
) -> Result<Vec<T>>where
T: for<'de> Deserialize<'de>,
Bulk upsert multiple records at once
§Examples
let client = Client::new("http://localhost:54321", "test-key").unwrap();
// Upsert multiple users (insert new, update existing)
let users: Vec<Value> = client.database()
.bulk_upsert("users", vec![
json!({"id": 1, "name": "Alice Updated", "email": "alice@example.com"}),
json!({"id": 2, "name": "Bob", "email": "bob@example.com"}),
])
.await
.unwrap();
Sourcepub async fn raw_sql<T>(&self, sql: &str, params: JsonValue) -> Result<Vec<T>>where
T: for<'de> Deserialize<'de>,
pub async fn raw_sql<T>(&self, sql: &str, params: JsonValue) -> Result<Vec<T>>where
T: for<'de> Deserialize<'de>,
Execute raw SQL via stored procedure/function
§Examples
let client = Client::new("http://localhost:54321", "test-key").unwrap();
// Execute raw SQL through RPC function
let result: Vec<Value> = client.database()
.raw_sql("SELECT * FROM users WHERE age > $1 AND status = $2", json!([18, "active"]))
.await
.unwrap();
Sourcepub async fn prepared_statement<T>(
&self,
statement_name: &str,
params: JsonValue,
) -> Result<Vec<T>>where
T: for<'de> Deserialize<'de>,
pub async fn prepared_statement<T>(
&self,
statement_name: &str,
params: JsonValue,
) -> Result<Vec<T>>where
T: for<'de> Deserialize<'de>,
Execute a prepared statement with parameters (safe from SQL injection)
§Examples
let client = Client::new("http://localhost:54321", "test-key").unwrap();
// Execute prepared statement
let users: Vec<Value> = client.database()
.prepared_statement("get_active_users_by_age", json!({"min_age": 18}))
.await
.unwrap();
Sourcepub async fn count_query(
&self,
function_name: &str,
params: JsonValue,
) -> Result<u64>
pub async fn count_query( &self, function_name: &str, params: JsonValue, ) -> Result<u64>
Execute a SQL query that returns a count (for analytical queries)
§Examples
let client = Client::new("http://localhost:54321", "test-key").unwrap();
// Get count of active users
let count: u64 = client.database()
.count_query("count_active_users", json!({}))
.await
.unwrap();
Sourcepub async fn transaction<T>(&self, operations: Vec<JsonValue>) -> Result<Vec<T>>where
T: for<'de> Deserialize<'de>,
pub async fn transaction<T>(&self, operations: Vec<JsonValue>) -> Result<Vec<T>>where
T: for<'de> Deserialize<'de>,
Begin a database transaction (via RPC)
§Examples
let client = Client::new("http://localhost:54321", "test-key").unwrap();
// Execute multiple operations in a transaction
let result: Vec<Value> = client.database()
.transaction(vec![
json!({
"operation": "insert",
"table": "users",
"data": {"name": "John", "email": "john@example.com"}
}),
json!({
"operation": "update",
"table": "profiles",
"data": {"user_id": 1, "bio": "Updated bio"},
"where": {"user_id": 1}
})
])
.await
.unwrap();
Sourcepub fn begin_transaction(&self) -> TransactionBuilder
pub fn begin_transaction(&self) -> TransactionBuilder
Create a transaction builder for more complex operations
§Examples
let client = Client::new("http://localhost:54321", "test-key").unwrap();
// Build and execute a transaction
let result: Vec<Value> = client.database()
.begin_transaction()
.insert("users", json!({"name": "Alice", "email": "alice@example.com"}))
.update("profiles", json!({"bio": "New bio"}), "user_id = 1")
.delete("old_data", "created_at < '2023-01-01'")
.commit()
.await
.unwrap();
Sourcepub fn update(&self, table: &str) -> UpdateBuilder
pub fn update(&self, table: &str) -> UpdateBuilder
Update data in a table
Sourcepub fn delete(&self, table: &str) -> DeleteBuilder
pub fn delete(&self, table: &str) -> DeleteBuilder
Delete data from a table
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Database
impl !RefUnwindSafe for Database
impl Send for Database
impl Sync for Database
impl Unpin for Database
impl !UnwindSafe for Database
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more