pub struct SurrealClient { /* private fields */ }Implementations§
Source§impl SurrealClient
impl SurrealClient
Sourcepub fn new(
engine: Box<dyn Engine>,
namespace: Option<String>,
database: Option<String>,
) -> Self
pub fn new( engine: Box<dyn Engine>, namespace: Option<String>, database: Option<String>, ) -> Self
Create a new SurrealDB instance with the given engine and optional namespace/database
Sourcepub fn with_debug(self, enabled: bool) -> Self
pub fn with_debug(self, enabled: bool) -> Self
Enable debug mode to log queries
Sourcepub async fn let_var(&mut self, key: &str, value: Value) -> Result<()>
pub async fn let_var(&mut self, key: &str, value: Value) -> Result<()>
Set a parameter for the session
Sourcepub async fn create(&self, resource: &str, data: Option<Value>) -> Result<Value>
pub async fn create(&self, resource: &str, data: Option<Value>) -> Result<Value>
Create a record in the database
Sourcepub async fn select_all(&self, table: Table) -> Result<Value>
pub async fn select_all(&self, table: Table) -> Result<Value>
Select all records from a table
Sourcepub async fn select_record(&self, record: RecordId) -> Result<Value>
pub async fn select_record(&self, record: RecordId) -> Result<Value>
Select a specific record by ID
Sourcepub async fn select_range(&self, range: RecordRange) -> Result<Value>
pub async fn select_range(&self, range: RecordRange) -> Result<Value>
Select a range of records
Sourcepub async fn update(&self, resource: &str, data: Option<Value>) -> Result<Value>
pub async fn update(&self, resource: &str, data: Option<Value>) -> Result<Value>
Update records in the database
Sourcepub async fn update_record(
&self,
record: RecordId,
data: Value,
) -> Result<Value>
pub async fn update_record( &self, record: RecordId, data: Value, ) -> Result<Value>
Update a specific record by ID
Sourcepub async fn update_all(&self, table: Table, data: Value) -> Result<Value>
pub async fn update_all(&self, table: Table, data: Value) -> Result<Value>
Update all records in a table
Sourcepub async fn upsert(&self, resource: &str, data: Option<Value>) -> Result<Value>
pub async fn upsert(&self, resource: &str, data: Option<Value>) -> Result<Value>
Upsert (insert or update) records in the database
Sourcepub async fn upsert_record(
&self,
record: RecordId,
data: Value,
) -> Result<Value>
pub async fn upsert_record( &self, record: RecordId, data: Value, ) -> Result<Value>
Upsert a specific record by ID
Sourcepub async fn merge(&self, resource: &str, data: Value) -> Result<Value>
pub async fn merge(&self, resource: &str, data: Value) -> Result<Value>
Merge data into records in the database
Sourcepub async fn merge_record(&self, record: RecordId, data: Value) -> Result<Value>
pub async fn merge_record(&self, record: RecordId, data: Value) -> Result<Value>
Merge data into a specific record by ID
Sourcepub async fn merge_all(&self, table: Table, data: Value) -> Result<Value>
pub async fn merge_all(&self, table: Table, data: Value) -> Result<Value>
Merge data into all records in a table
Sourcepub async fn patch(&self, resource: &str, patches: Vec<Value>) -> Result<Value>
pub async fn patch(&self, resource: &str, patches: Vec<Value>) -> Result<Value>
Apply JSON patches to records Apply patches to records in the database
Sourcepub async fn delete_record(&self, record: RecordId) -> Result<Value>
pub async fn delete_record(&self, record: RecordId) -> Result<Value>
Delete a specific record by ID
Sourcepub async fn delete_all(&self, table: Table) -> Result<Value>
pub async fn delete_all(&self, table: Table) -> Result<Value>
Delete all records from a table
Sourcepub async fn insert(&self, table: &str, data: Value) -> Result<Value>
pub async fn insert(&self, table: &str, data: Value) -> Result<Value>
Insert records into the database Insert data into a table
Sourcepub async fn insert_many(&self, table: Table, data: Vec<Value>) -> Result<Value>
pub async fn insert_many(&self, table: Table, data: Vec<Value>) -> Result<Value>
Insert multiple records
Sourcepub async fn relate(
&self,
from: &str,
relation: &str,
to: &str,
data: Option<Value>,
) -> Result<Value>
pub async fn relate( &self, from: &str, relation: &str, to: &str, data: Option<Value>, ) -> Result<Value>
Create a relation between records
Sourcepub async fn relate_records(
&self,
from: RecordId,
relation: Table,
to: RecordId,
data: Option<Value>,
) -> Result<Value>
pub async fn relate_records( &self, from: RecordId, relation: Table, to: RecordId, data: Option<Value>, ) -> Result<Value>
Create a relation between specific records
Sourcepub async fn run(&self, func: &str, args: Option<Value>) -> Result<Value>
pub async fn run(&self, func: &str, args: Option<Value>) -> Result<Value>
Run a stored function
Sourcepub async fn query(&self, sql: &str, variables: Option<Value>) -> Result<Value>
pub async fn query(&self, sql: &str, variables: Option<Value>) -> Result<Value>
Execute a custom SurrealQL query
Examples found in repository?
10async fn main() -> Result<(), Box<dyn std::error::Error>> {
11 let token = std::env::var("SURREAL_TOKEN").expect("set SURREAL_TOKEN");
12 let host = std::env::var("SURREAL_HOST").unwrap_or_else(|_| {
13 "wss://close-wasp-06fmkfm5uhq2f77ih987dcdac4.aws-euw1.surreal.cloud".into()
14 });
15 let query = std::env::var("SURREAL_QUERY")
16 .unwrap_or_else(|_| "USE NS main DB `vantage-leads`; INFO FOR DB;".into());
17
18 println!("connecting to {host} ...");
19 let client = SurrealConnection::dsn(&host)?
20 .auth_token(token)
21 .version_check(false)
22 .connect()
23 .await?;
24 println!(
25 "connected. server version = {:?}\n",
26 client.version().await?
27 );
28
29 let result = client.query(&query, None).await?;
30 println!("{}", serde_json::to_string_pretty(&result)?);
31
32 Ok(())
33}Sourcepub async fn version(&self) -> Result<String>
pub async fn version(&self) -> Result<String>
Get the version of the SurrealDB instance
Examples found in repository?
10async fn main() -> Result<(), Box<dyn std::error::Error>> {
11 let token = std::env::var("SURREAL_TOKEN").expect("set SURREAL_TOKEN");
12 let host = std::env::var("SURREAL_HOST").unwrap_or_else(|_| {
13 "wss://close-wasp-06fmkfm5uhq2f77ih987dcdac4.aws-euw1.surreal.cloud".into()
14 });
15 let query = std::env::var("SURREAL_QUERY")
16 .unwrap_or_else(|_| "USE NS main DB `vantage-leads`; INFO FOR DB;".into());
17
18 println!("connecting to {host} ...");
19 let client = SurrealConnection::dsn(&host)?
20 .auth_token(token)
21 .version_check(false)
22 .connect()
23 .await?;
24 println!(
25 "connected. server version = {:?}\n",
26 client.version().await?
27 );
28
29 let result = client.query(&query, None).await?;
30 println!("{}", serde_json::to_string_pretty(&result)?);
31
32 Ok(())
33}Sourcepub async fn import(
&self,
_content: &str,
_username: &str,
_password: &str,
) -> Result<Value>
pub async fn import( &self, _content: &str, _username: &str, _password: &str, ) -> Result<Value>
Import database content (HTTP only)
Sourcepub async fn export(&self, _username: &str, _password: &str) -> Result<String>
pub async fn export(&self, _username: &str, _password: &str) -> Result<String>
Export database content (HTTP only)
Sourcepub async fn import_ml(
&self,
_content: &str,
_username: Option<&str>,
_password: Option<&str>,
) -> Result<Value>
pub async fn import_ml( &self, _content: &str, _username: Option<&str>, _password: Option<&str>, ) -> Result<Value>
Import ML model (HTTP only)
Sourcepub async fn export_ml(
&self,
_name: &str,
_version: Option<&str>,
_username: Option<&str>,
_password: Option<&str>,
) -> Result<String>
pub async fn export_ml( &self, _name: &str, _version: Option<&str>, _username: Option<&str>, _password: Option<&str>, ) -> Result<String>
Export ML model (HTTP only)
Sourcepub async fn live(&self, resource: &str) -> Result<LiveStream>
pub async fn live(&self, resource: &str) -> Result<LiveStream>
Start a live query on a table (or record) and stream change notifications.
Issues the live RPC, then registers the returned live-query id with
the engine so its read loop forwards every matching CREATE/UPDATE/
DELETE frame onto the returned LiveStream. Requires a WebSocket
connection — request/response-only engines return an error from
Engine::register_live.
The stream stays open until the connection closes; call kill
with the stream’s query_id to release the
server-side query early.