diff --git a/src/content/doc-sdk-rust/methods/connect.mdx b/src/content/doc-sdk-rust/methods/connect.mdx index fc1248555..9527f682e 100644 --- a/src/content/doc-sdk-rust/methods/connect.mdx +++ b/src/content/doc-sdk-rust/methods/connect.mdx @@ -42,24 +42,23 @@ The `.connect()` method will usually take a `String` or a type that implements ` where T: Into`. ```rust +use std::sync::LazyLock; use std::time::Duration; use surrealdb::engine::remote::ws::{Client, Ws, Wss}; use surrealdb::opt::Config; use surrealdb::Surreal; +static DB: LazyLock> = LazyLock::new(Surreal::init); + #[tokio::main] async fn main() -> surrealdb::Result<()> { - let db: Surreal = Surreal::init(); // Connect to a local endpoint - db.connect::("127.0.0.1:8000").await?; + DB.connect::("127.0.0.1:8000").await?; // Connect to a remote endpoint - db.connect::("cloud.surrealdb.com").await?; + DB.connect::("cloud.surrealdb.com").await?; // A tuple with a Config struct can also be passed in for fine tuning of the connection - db.connect::(( - "127.0.0.1:8000", - Config::default().query_timeout(Duration::from_millis(1500)), - )) - .await?; + let config = Config::default().query_timeout(Duration::from_millis(1500)); + DB.connect::(("127.0.0.1:8000", config)).await?; Ok(()) } -``` \ No newline at end of file +``` diff --git a/src/content/doc-sdk-rust/methods/create.mdx b/src/content/doc-sdk-rust/methods/create.mdx index 3768374fc..91b91dcc2 100644 --- a/src/content/doc-sdk-rust/methods/create.mdx +++ b/src/content/doc-sdk-rust/methods/create.mdx @@ -46,6 +46,11 @@ db.create(resource).content(data) ### Example usage ```rust +use serde::{Deserialize, Serialize}; +use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; +use surrealdb::RecordId; + #[derive(Debug, Serialize, Deserialize)] struct Person { name: Option, @@ -54,16 +59,17 @@ struct Person { #[derive(Debug, Deserialize)] struct Record { - id: Thing, + id: RecordId, } -use serde::{Deserialize, Serialize}; -use surrealdb::engine::any::connect; -use surrealdb::sql::Thing; - #[tokio::main] async fn main() -> surrealdb::Result<()> { let db = connect("ws://localhost:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; db.use_ns("ns").use_db("db").await?; // Create a record with a random ID diff --git a/src/content/doc-sdk-rust/methods/delete.mdx b/src/content/doc-sdk-rust/methods/delete.mdx index 958a93eec..9d3e1436c 100644 --- a/src/content/doc-sdk-rust/methods/delete.mdx +++ b/src/content/doc-sdk-rust/methods/delete.mdx @@ -36,28 +36,35 @@ db.delete(resource) ### Example usage ```rust +use serde::{Deserialize, Serialize}; +use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; +use surrealdb::opt::Resource; +use surrealdb::RecordId; + #[derive(Debug, Serialize, Deserialize)] struct Person { - id: Thing, + id: RecordId, } -use serde::{Deserialize, Serialize}; -use surrealdb::engine::any::connect; -use surrealdb::sql::Thing; - #[tokio::main] async fn main() -> surrealdb::Result<()> { let db = connect("ws://localhost:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; db.use_ns("ns").use_db("db").await?; // Create three `person` records - let _: Option = db.create(("person", "one")).await?; - let _: Option = db.create(("person", "two")).await?; - let _: Option = db.create(("person", "three")).await?; + db.create(Resource::from(("person", "one"))).await?; + db.create(Resource::from(("person", "two"))).await?; + db.create(Resource::from(("person", "three"))).await?; let deleted_one: Option = db.delete(("person", "one")).await?; - let deleted_rest: Vec = db.delete("person").await?; dbg!(deleted_one); + let deleted_rest: Vec = db.delete("person").await?; dbg!(deleted_rest); Ok(()) } diff --git a/src/content/doc-sdk-rust/methods/export.mdx b/src/content/doc-sdk-rust/methods/export.mdx index b9d55169c..fe259c53e 100644 --- a/src/content/doc-sdk-rust/methods/export.mdx +++ b/src/content/doc-sdk-rust/methods/export.mdx @@ -42,49 +42,48 @@ db.export(target) The `.export()` method can be used to save the contents of a database to a file. ```rust -#[derive(Debug, Serialize, Deserialize)] -struct Person { - id: Thing, -} - -use serde::{Deserialize, Serialize}; use surrealdb::engine::any::connect; -use surrealdb::sql::Thing; +use surrealdb::opt::auth::Root; +use surrealdb::opt::Resource; #[tokio::main] async fn main() -> surrealdb::Result<()> { let db = connect("http://localhost:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; db.use_ns("ns").use_db("db").await?; // Create a `person` record - let _: Option = db.create("person").await?; + db.create(Resource::from("person")).await?; db.export("backup.surql").await?; Ok(()) } - ``` If an empty tuple is passed in for the file name, the `.export()` method will instead return an async stream of bytes. ```rust -#[derive(Debug, Serialize, Deserialize)] -struct Person { - id: Thing, -} - use futures::StreamExt; -use serde::{Deserialize, Serialize}; use surrealdb::engine::any::connect; -use surrealdb::sql::Thing; +use surrealdb::opt::auth::Root; +use surrealdb::opt::Resource; #[tokio::main] async fn main() -> surrealdb::Result<()> { let db = connect("http://localhost:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; db.use_ns("ns").use_db("db").await?; // Create a `person` record - let _: Option = db.create("person").await?; + db.create(Resource::from("person")).await?; let mut stream = db.export(()).await?; diff --git a/src/content/doc-sdk-rust/methods/import.mdx b/src/content/doc-sdk-rust/methods/import.mdx index 55a5bd950..372378371 100644 --- a/src/content/doc-sdk-rust/methods/import.mdx +++ b/src/content/doc-sdk-rust/methods/import.mdx @@ -13,17 +13,23 @@ Restores the database from a file. > WebSocket connections currently do not currently support exports and imports. Be sure to use an HTTP endpoint when using this method. ```rust title="Method Syntax" -db.authenticate(token) +db.import(source) ``` ### Example usage ```rust use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; #[tokio::main] async fn main() -> surrealdb::Result<()> { let db = connect("http://localhost:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; db.use_ns("ns").use_db("db").await?; db.import("backup.surql").await?; Ok(()) diff --git a/src/content/doc-sdk-rust/methods/index.mdx b/src/content/doc-sdk-rust/methods/index.mdx index ee034ea6f..d0933a027 100644 --- a/src/content/doc-sdk-rust/methods/index.mdx +++ b/src/content/doc-sdk-rust/methods/index.mdx @@ -36,7 +36,7 @@ Most methods in the SurrealDB SDK involve either working with or creating an ins Assigns a value as a parameter for this connection - db.use() + db.use_ns().use_db() Switch to a specific namespace and database @@ -172,9 +172,5 @@ Most methods in the SurrealDB SDK involve either working with or creating an ins db.version() Returns the current database version - - db.import() - Waits for a certain event to occur before proceeding - - \ No newline at end of file + diff --git a/src/content/doc-sdk-rust/methods/init.mdx b/src/content/doc-sdk-rust/methods/init.mdx index 982d7f55e..4b48f611d 100644 --- a/src/content/doc-sdk-rust/methods/init.mdx +++ b/src/content/doc-sdk-rust/methods/init.mdx @@ -8,6 +8,7 @@ description: The .init() method initializes a new unconnected instance. # `init()` The .init() method initializes a new unconnected instance of the client. +This is typically used to create a global, static instance of the client. ```rust title="Method Syntax" Surreal::init() @@ -16,35 +17,36 @@ Surreal::init() ### Example usage ```rust +use std::sync::LazyLock; use surrealdb::engine::remote::ws::{Client, Ws}; use surrealdb::Surreal; +static DB: LazyLock> = LazyLock::new(Surreal::init); + #[tokio::main] async fn main() -> surrealdb::Result<()> { - let db: Surreal = Surreal::init(); // Connect to the database - db.connect::("127.0.0.1:8000").await?; + DB.connect::("127.0.0.1:8000").await?; Ok(()) } ``` -`Surreal::init()` by default will create an instance of `Surreal`, allowing you to choose at runtime which way to connect. +`Surreal::init()` can also be used to create an instance of `Surreal`, allowing you to choose at runtime which way to connect. ```rust use std::env; - +use std::sync::LazyLock; use surrealdb::engine::any::Any; use surrealdb::Surreal; +static DB: LazyLock> = LazyLock::new(Surreal::init); + #[tokio::main] async fn main() -> surrealdb::Result<()> { - let db: Surreal = Surreal::init(); - - if let Ok(path) = env::var("DB_ENDPOINT") { - db.connect(path).await?; - } else { - db.connect("mem://").await?; - }; + // Choose an endpoint at runtime using the `DB_ENDPOINT` environment variable + // or fallback to the memory engine. + let endpoint = env::var("DB_ENDPOINT").unwrap_or_else(|_| "mem://".to_owned()); + DB.connect(endpoint).await?; Ok(()) } ``` \ No newline at end of file diff --git a/src/content/doc-sdk-rust/methods/insert.mdx b/src/content/doc-sdk-rust/methods/insert.mdx index ed4073247..95d1a1d01 100644 --- a/src/content/doc-sdk-rust/methods/insert.mdx +++ b/src/content/doc-sdk-rust/methods/insert.mdx @@ -54,8 +54,9 @@ db.insert(resource).relation(data); Inserting a record with a specific ID: ```rust -se serde::{Deserialize, Serialize}; -use surrealdb::{engine::any::connect, sql::Thing}; +use serde::{Deserialize, Serialize}; +use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; #[derive(Debug, Serialize, Deserialize)] struct Settings { @@ -73,13 +74,18 @@ struct Data<'a> { struct Person { name: String, settings: Settings, - id: Thing, } #[tokio::main] async fn main() -> surrealdb::Result<()> { let db = connect("ws://localhost:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; + db.use_ns("ns").use_db("db").await?; let person: Option = db @@ -101,7 +107,8 @@ Inserting multiple records into a table: ```rust use serde::{Deserialize, Serialize}; -use surrealdb::{engine::any::connect, sql::Thing}; +use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; #[derive(Debug, Serialize, Deserialize)] struct Settings { @@ -119,13 +126,18 @@ struct Data<'a> { struct Person { name: String, settings: Settings, - id: Thing, } #[tokio::main] async fn main() -> surrealdb::Result<()> { let db = connect("ws://localhost:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; + db.use_ns("ns").use_db("db").await?; let people: Vec = db @@ -152,11 +164,13 @@ async fn main() -> surrealdb::Result<()> { } ``` -The `.insert()` method can take an empty tuple instead of a table ID if the following method contains [a record ID](https://docs.rs/surrealdb-core/latest/surrealdb_core/sql/struct.Thing.html) (a struct `Thing`). The [`sql::thing()`](https://docs.rs/surrealdb-core/latest/surrealdb_core/sql/fn.thing.html) method is a quick and easy way to do this. +The `.insert()` method can take an empty tuple instead of a table ID if the following method contains [a record ID](https://docs.rs/surrealdb/latest/surrealdb/struct.RecordId.html). ```rust use serde::{Deserialize, Serialize}; -use surrealdb::{engine::any::connect, sql, sql::Thing}; +use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; +use surrealdb::RecordId; #[derive(Debug, Serialize, Deserialize)] struct Settings { @@ -166,31 +180,37 @@ struct Settings { #[derive(Serialize)] struct Data<'a> { - id: Thing, + id: RecordId, name: &'a str, } #[derive(Debug, Deserialize)] struct Person { name: String, - id: Thing, + id: RecordId, } #[tokio::main] async fn main() -> surrealdb::Result<()> { let db = connect("ws://localhost:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; + db.use_ns("ns").use_db("db").await?; let people: Vec = db .insert(()) .content(vec![ Data { - id: sql::thing("person:tobie")?, + id: RecordId::from(("person", "tobie")), name: "Tobie", }, Data { - id: sql::thing("person:jaime")?, + id: RecordId::from(("person", "jaime")), name: "Jaime", }, ]) @@ -204,7 +224,9 @@ An example of two `person` records and one `company` record, followed by `.inser ```rust use serde::{Deserialize, Serialize}; -use surrealdb::{engine::any::connect, sql, sql::Thing}; +use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; +use surrealdb::RecordId; #[derive(Debug, Serialize, Deserialize)] struct Settings { @@ -214,43 +236,49 @@ struct Settings { #[derive(Serialize)] struct Data<'a> { - id: Thing, + id: RecordId, name: &'a str, } #[derive(Debug, Deserialize)] struct Record { name: String, - id: Thing, + id: RecordId, } #[derive(Debug, Serialize, Deserialize)] struct Founded { #[serde(rename = "in")] - founder: Thing, + founder: RecordId, #[serde(rename = "out")] - company: Thing, + company: RecordId, } #[tokio::main] async fn main() -> surrealdb::Result<()> { let db = connect("ws://localhost:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; + db.use_ns("ns").use_db("db").await?; let records: Vec = db .insert(()) .content(vec![ Data { - id: sql::thing("person:tobie")?, + id: RecordId::from(("person", "tobie")), name: "Tobie", }, Data { - id: sql::thing("person:jaime")?, + id: RecordId::from(("person", "jaime")), name: "Jaime", }, Data { - id: sql::thing("company:surrealdb")?, + id: RecordId::from(("company", "surrealdb")), name: "SurrealDB", }, ]) @@ -261,12 +289,12 @@ async fn main() -> surrealdb::Result<()> { .insert("founded") .relation(vec![ Founded { - founder: sql::thing("person:tobie")?, - company: sql::thing("company:surrealdb")?, + founder: RecordId::from(("person", "tobie")), + company: RecordId::from(("company", "surrealdb")), }, Founded { - founder: sql::thing("person:jaime")?, - company: sql::thing("company:surrealdb")?, + founder: RecordId::from(("person", "jaime")), + company: RecordId::from(("company", "surrealdb")), }, ]) .await?; diff --git a/src/content/doc-sdk-rust/methods/query.mdx b/src/content/doc-sdk-rust/methods/query.mdx index bb3710fca..1f092c3e2 100644 --- a/src/content/doc-sdk-rust/methods/query.mdx +++ b/src/content/doc-sdk-rust/methods/query.mdx @@ -43,23 +43,31 @@ The `.query()` method serves as a default way to pass queries into the Rust SDK. ```rust use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; #[tokio::main] async fn main() -> surrealdb::Result<()> { let db = connect("ws://localhost:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; + db.use_ns("ns").use_db("db").await?; let query = r#" - LET $data = ["J. Jonah Jameson", "James Earl Jones"]; - RETURN $data.map(|$name| { - LET $names = $name.split(' '); - { - first_name: $names[0], - middle_name: $names[1], - last_name: $names[2] - } - });"#; + LET $data = ["J. Jonah Jameson", "James Earl Jones"]; + RETURN $data.map(|$name| { + LET $names = $name.split(' '); + { + first_name: $names[0], + middle_name: $names[1], + last_name: $names[2] + } + }); + "#; let result = db.query(query).await?; println!("Number of statements: {}", result.num_statements()); @@ -73,6 +81,7 @@ The `.take()` method can be used to pull out one of the responses into a deseria ```rust use serde::Deserialize; use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; #[derive(Debug, Deserialize)] struct Person { @@ -85,6 +94,12 @@ struct Person { async fn main() -> surrealdb::Result<()> { let db = connect("ws://localhost:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; + db.use_ns("ns").use_db("db").await?; let query = r#" @@ -134,23 +149,31 @@ And then apply the `.bind()` method to pass the parameter in. ```rust use serde::Deserialize; -use surrealdb::{engine::any::connect, sql::Thing}; +use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; +use surrealdb::RecordId; #[derive(Debug, Deserialize)] struct Person { - id: Thing + id: RecordId, } #[tokio::main] async fn main() -> surrealdb::Result<()> { let db = connect("ws://localhost:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; + db.use_ns("ns").use_db("db").await?; let sql = " - CREATE person; - SELECT * FROM type::table($table); -"; + CREATE person; + SELECT * FROM type::table($table); + "; let mut result = db.query(sql).bind(("table", "person")).await?; // Get the first result from the first query let created: Option = result.take(0)?; diff --git a/src/content/doc-sdk-rust/methods/run.mdx b/src/content/doc-sdk-rust/methods/run.mdx index 757943210..8cd87858d 100644 --- a/src/content/doc-sdk-rust/methods/run.mdx +++ b/src/content/doc-sdk-rust/methods/run.mdx @@ -41,11 +41,18 @@ Calling an existing SurrealQL function: ```rust use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; #[tokio::main] async fn main() -> surrealdb::Result<()> { let db = connect("ws://localhost:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; + let res: f32 = db.run("rand::float").await?; dbg!(res); Ok(()) @@ -56,10 +63,16 @@ User-defined functions can be called as well. ```rust use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; #[tokio::main] async fn main() -> surrealdb::Result<()> { let db = connect("ws://localhost:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; db.use_ns("ns").use_db("db").await?; db.query("DEFINE FUNCTION fn::return_one() -> int { RETURN 1 };") @@ -76,6 +89,7 @@ The return value of the `.run()` function can be deserialized in the same way as ```rust use serde::Deserialize; use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; #[derive(Debug, Deserialize)] struct Person { @@ -87,6 +101,11 @@ struct Person { #[tokio::main] async fn main() -> surrealdb::Result<()> { let db = connect("ws://localhost:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; db.use_ns("ns").use_db("db").await?; db.query( diff --git a/src/content/doc-sdk-rust/methods/select.mdx b/src/content/doc-sdk-rust/methods/select.mdx index 74dff4fc1..0d5df76d1 100644 --- a/src/content/doc-sdk-rust/methods/select.mdx +++ b/src/content/doc-sdk-rust/methods/select.mdx @@ -49,18 +49,12 @@ let person: Option = db.select(("person", "h5wxrf2ewk8xjxosxtyc")).await use serde::Deserialize; use surrealdb::engine::remote::ws::Ws; use surrealdb::opt::auth::Root; -use surrealdb::sql::Id; +use surrealdb::RecordId; use surrealdb::Surreal; -// defining your own custom Thing struct and using that in place of `Thing` in the `Id` struct -#[derive(Debug, Deserialize)] -struct CustomThing { - id: Id, -} - #[derive(Debug, Deserialize)] struct Person { - id: CustomThing, + id: RecordId, name: String, age: u8, } @@ -89,7 +83,6 @@ async fn main() -> surrealdb::Result<()> { Ok(()) } - ``` ### Translated query diff --git a/src/content/doc-sdk-rust/methods/select_live.mdx b/src/content/doc-sdk-rust/methods/select_live.mdx index f9c31dd56..6219b25c3 100644 --- a/src/content/doc-sdk-rust/methods/select_live.mdx +++ b/src/content/doc-sdk-rust/methods/select_live.mdx @@ -41,12 +41,13 @@ The following example requires adding the `futures` crate with `cargo add future use futures::StreamExt; use serde::Deserialize; use surrealdb::engine::remote::ws::Ws; -use surrealdb::sql::Thing; +use surrealdb::opt::auth::Root; use surrealdb::{Notification, Surreal}; +use surrealdb::RecordId; #[derive(Debug, Deserialize)] struct Person { - id: Thing, + id: RecordId, } // Handle the result of the live query notification @@ -58,6 +59,12 @@ fn handle(result: Result, surrealdb::Error>) { async fn main() -> surrealdb::Result<()> { let db = Surreal::new::("127.0.0.1:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; + db.use_ns("ns").use_db("db").await?; // Select the "person" table and listen for live updates. @@ -72,16 +79,10 @@ async fn main() -> surrealdb::Result<()> { } ``` -To experiment with the output, first start a server using the following command: - -``` -surreal start --unauthenticated -``` - Then connect to it using Surrealist or open a new terminal window with the following command. ``` -surreal sql --namespace ns --database db --pretty +surreal sql --namespace ns --database db --user root --pass root --pretty ``` You can then use queries like the following to work with some `person` records. diff --git a/src/content/doc-sdk-rust/methods/set.mdx b/src/content/doc-sdk-rust/methods/set.mdx index 919ca911d..ae388adf8 100644 --- a/src/content/doc-sdk-rust/methods/set.mdx +++ b/src/content/doc-sdk-rust/methods/set.mdx @@ -58,6 +58,7 @@ LET $name = { ```rust use serde::Serialize; use surrealdb::engine::remote::ws::Ws; +use surrealdb::opt::auth::Root; use surrealdb::Surreal; #[derive(Debug, Serialize)] @@ -70,6 +71,12 @@ struct Name<'a> { async fn main() -> surrealdb::Result<()> { let db = Surreal::new::("127.0.0.1:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; + db.use_ns("ns").use_db("db").await?; // Assign the variable on the connection diff --git a/src/content/doc-sdk-rust/methods/unset.mdx b/src/content/doc-sdk-rust/methods/unset.mdx index 2da45cf35..8b76216ea 100644 --- a/src/content/doc-sdk-rust/methods/unset.mdx +++ b/src/content/doc-sdk-rust/methods/unset.mdx @@ -18,6 +18,7 @@ db.unset(key) ```rust use serde::Serialize; use surrealdb::engine::remote::ws::Ws; +use surrealdb::opt::auth::Root; use surrealdb::Surreal; #[derive(Debug, Serialize)] @@ -30,6 +31,12 @@ struct Name<'a> { async fn main() -> surrealdb::Result<()> { let db = Surreal::new::("127.0.0.1:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; + db.use_ns("ns").use_db("db").await?; // Assign the variable on the connection diff --git a/src/content/doc-sdk-rust/methods/update.mdx b/src/content/doc-sdk-rust/methods/update.mdx index fdc775817..e4207016e 100644 --- a/src/content/doc-sdk-rust/methods/update.mdx +++ b/src/content/doc-sdk-rust/methods/update.mdx @@ -60,6 +60,7 @@ db.update(resource).content(data) ```rust use serde::{Deserialize, Serialize}; use surrealdb::engine::remote::ws::Ws; +use surrealdb::opt::auth::Root; use surrealdb::Surreal; #[derive(Debug, Serialize, Deserialize, Default)] @@ -84,6 +85,12 @@ struct Company { async fn main() -> surrealdb::Result<()> { let db = Surreal::new::("127.0.0.1:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; + db.use_ns("ns").use_db("db").await?; db.query("CREATE person:tobie, person:jaime").await?; @@ -153,6 +160,7 @@ db.update(resource).merge(data) ```rust use serde::{Deserialize, Serialize}; use surrealdb::engine::remote::ws::Ws; +use surrealdb::opt::auth::Root; use surrealdb::Surreal; #[derive(Debug, Serialize, Deserialize, Default)] @@ -178,6 +186,12 @@ struct Company { async fn main() -> surrealdb::Result<()> { let db = Surreal::new::("127.0.0.1:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; + db.use_ns("ns").use_db("db").await?; db.query("CREATE person:tobie SET name = 'Tobie'; CREATE person:jaime SET name = 'jaime';") @@ -258,6 +272,7 @@ The `.patch()` method uses a struct called a `PatchOp` that contains the four me ```rust use serde::{Deserialize, Serialize}; use surrealdb::engine::remote::ws::Ws; +use surrealdb::opt::auth::Root; use surrealdb::opt::PatchOp; use surrealdb::sql::Datetime; use surrealdb::Surreal; @@ -280,6 +295,12 @@ struct Settings { async fn main() -> surrealdb::Result<()> { let db = Surreal::new::("127.0.0.1:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; + db.use_ns("ns").use_db("db").await?; db.query( diff --git a/src/content/doc-sdk-rust/methods/upsert.mdx b/src/content/doc-sdk-rust/methods/upsert.mdx index 57862b2d1..b76c8cb98 100644 --- a/src/content/doc-sdk-rust/methods/upsert.mdx +++ b/src/content/doc-sdk-rust/methods/upsert.mdx @@ -13,7 +13,7 @@ Upserts all records in a table, or a specific record. db.upsert(resource) ``` -he `.upsert()` method is followed by second method that refers to the type of upsert to use: an upsert with `.content()`, `.merge()`, or `.patch()`. +The `.upsert()` method is followed by second method that refers to the type of upsert to use: an upsert with `.content()`, `.merge()`, or `.patch()`. ## `.upsert().content()` @@ -60,12 +60,13 @@ db.upsert(resource).content(data) ```rust use serde::{Deserialize, Serialize}; use surrealdb::engine::remote::ws::Ws; -use surrealdb::sql::Thing; +use surrealdb::opt::auth::Root; +use surrealdb::RecordId; use surrealdb::Surreal; #[derive(Debug, Serialize, Deserialize)] struct Person { - id: Thing, + id: RecordId, name: Option, company: Option, settings: Option, @@ -86,6 +87,12 @@ struct Company { async fn main() -> surrealdb::Result<()> { let db = Surreal::new::("127.0.0.1:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; + db.use_ns("ns").use_db("db").await?; // upsert one record in a table @@ -154,6 +161,7 @@ db.upsert(resource).merge(data) ```rust use serde::{Deserialize, Serialize}; use surrealdb::engine::remote::ws::Ws; +use surrealdb::opt::auth::Root; use surrealdb::Surreal; #[derive(Debug, Serialize, Deserialize, Default)] @@ -174,6 +182,12 @@ struct Settings { async fn main() -> surrealdb::Result<()> { let db = Surreal::new::("127.0.0.1:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; + db.use_ns("ns").use_db("db").await?; // upsert a single record @@ -243,6 +257,7 @@ The `.patch()` method uses a struct called a `PatchOp` that contains the four me ```rust use serde::{Deserialize, Serialize}; use surrealdb::engine::remote::ws::Ws; +use surrealdb::opt::auth::Root; use surrealdb::opt::PatchOp; use surrealdb::sql::Datetime; use surrealdb::Surreal; @@ -265,6 +280,12 @@ struct Settings { async fn main() -> surrealdb::Result<()> { let db = Surreal::new::("127.0.0.1:8000").await?; + db.signin(Root { + username: "root", + password: "root", + }) + .await?; + db.use_ns("ns").use_db("db").await?; // upsert a record with a specific ID diff --git a/src/content/doc-sdk-rust/methods/use.mdx b/src/content/doc-sdk-rust/methods/use.mdx index 4a57a8e25..63d1202ae 100644 --- a/src/content/doc-sdk-rust/methods/use.mdx +++ b/src/content/doc-sdk-rust/methods/use.mdx @@ -5,7 +5,7 @@ title: Rust | SDKs | Integration description: The .use_ns() and .use_db() methods switch to a specific namespace and database. --- -# `db.use_ns()`, `db.use_db()` +# `use_ns()`, `use_db()` Switch to a specific namespace and database. diff --git a/src/content/doc-sdk-rust/setup.mdx b/src/content/doc-sdk-rust/setup.mdx index c3171948f..a1f813574 100644 --- a/src/content/doc-sdk-rust/setup.mdx +++ b/src/content/doc-sdk-rust/setup.mdx @@ -152,8 +152,10 @@ Now that we have the basics down, it is time to try out some other methods like use serde::{Deserialize, Serialize}; use surrealdb::engine::remote::ws::Ws; use surrealdb::opt::auth::Root; -use surrealdb::sql::Thing; +use surrealdb::opt::Resource; +use surrealdb::RecordId; use surrealdb::Surreal; +use surrealdb::Value; #[derive(Debug, Serialize)] struct Name<'a> { @@ -175,7 +177,7 @@ struct Responsibility { #[derive(Debug, Deserialize)] struct Record { - id: Thing, + id: RecordId, } #[tokio::main] @@ -201,12 +203,14 @@ async fn main() -> surrealdb::Result<()> { }, marketing: true, }) - .await?; + .await?; dbg!(created); // Update a person record with a specific id - let _: Option = db - .update(("person", "jaime")) + // We don't care about the response in this case + // so we are just going to use `Resource::from` + // to let the compiler return `surrealdb::Value` + db.update(Resource::from(("person", "jaime"))) .merge(Responsibility { marketing: true }) .await?; @@ -236,12 +240,13 @@ A static singleton can be used to ensure that a single database instance is avai use serde::{Deserialize, Serialize}; use std::sync::LazyLock; use surrealdb::engine::remote::ws::{Client, Ws}; -use surrealdb::sql::Thing; +use surrealdb::opt::auth::Root; +use surrealdb::RecordId; use surrealdb::Surreal; #[derive(Debug, Deserialize)] struct Record { - id: Thing, + id: RecordId, } #[derive(Debug, Serialize)] @@ -269,6 +274,12 @@ async fn upsert_tobie() -> surrealdb::Result<()> { async fn main() -> surrealdb::Result<()> { // Connect to the database DB.connect::("localhost:8000").await?; + // Sign in to the server + DB.signin(Root { + username: "root", + password: "root", + }) + .await?; // Select a namespace + database DB.use_ns("test").use_db("test").await?; upsert_tobie().await?; diff --git a/src/content/doc-surrealdb/cli/env.mdx b/src/content/doc-surrealdb/cli/env.mdx index 098aaf6cf..f10dfeba2 100644 --- a/src/content/doc-surrealdb/cli/env.mdx +++ b/src/content/doc-surrealdb/cli/env.mdx @@ -445,6 +445,8 @@ surreal start --user root --pass root ## Storage backend environment variables +These environment variables are used to configure the storage backend for SurrealDB. + ### FoundationDB diff --git a/src/content/doc-surrealdb/cli/export.mdx b/src/content/doc-surrealdb/cli/export.mdx index ee1364904..64ad4e3fa 100644 --- a/src/content/doc-surrealdb/cli/export.mdx +++ b/src/content/doc-surrealdb/cli/export.mdx @@ -26,7 +26,7 @@ The export command exports a SurrealQL script file from a local or remote Surrea diff --git a/src/content/doc-surrealdb/cli/upgrade.mdx b/src/content/doc-surrealdb/cli/upgrade.mdx index be8438dc2..6a7e2ea45 100644 --- a/src/content/doc-surrealdb/cli/upgrade.mdx +++ b/src/content/doc-surrealdb/cli/upgrade.mdx @@ -28,7 +28,7 @@ The upgrade command upgrades SurrealDB to the latest version, nightly or a speci
- -e / --endpoint / --conn + `-e` / `--endpoint` / `--conn` @@ -35,7 +35,7 @@ The export command exports a SurrealQL script file from a local or remote Surrea
- -u / --user + `-u` / `--user` @@ -44,7 +44,7 @@ The export command exports a SurrealQL script file from a local or remote Surrea
- -p / --pass + `-p` / `--pass` @@ -53,7 +53,7 @@ The export command exports a SurrealQL script file from a local or remote Surrea
- -t / --token + `-t` / `--token` @@ -62,7 +62,7 @@ The export command exports a SurrealQL script file from a local or remote Surrea
- --ns + `--ns` @@ -71,7 +71,7 @@ The export command exports a SurrealQL script file from a local or remote Surrea
- --db + `--db` @@ -93,7 +93,7 @@ The export command exports a SurrealQL script file from a local or remote Surrea
- file + `file` diff --git a/src/content/doc-surrealdb/cli/fix.mdx b/src/content/doc-surrealdb/cli/fix.mdx index f4af46ac5..3a7c9c8f4 100644 --- a/src/content/doc-surrealdb/cli/fix.mdx +++ b/src/content/doc-surrealdb/cli/fix.mdx @@ -26,7 +26,7 @@ The fix command converts SurrealDB version 1.x data into a format that can be us
- -e / --log + `-e` / `--log` @@ -48,7 +48,7 @@ The fix command converts SurrealDB version 1.x data into a format that can be us
- file + `file` Sets the the path to the existing data to convert to 2.x storage format diff --git a/src/content/doc-surrealdb/cli/import.mdx b/src/content/doc-surrealdb/cli/import.mdx index 1a39681d6..12f342bed 100644 --- a/src/content/doc-surrealdb/cli/import.mdx +++ b/src/content/doc-surrealdb/cli/import.mdx @@ -26,7 +26,7 @@ The import command imports a SurrealQL script file into a local or remote Surrea
- -e / --endpoint / --conn + `-e` / `--endpoint` / `--conn` @@ -35,7 +35,7 @@ The import command imports a SurrealQL script file into a local or remote Surrea
- -u / --user + `-u` / `--user` @@ -44,7 +44,7 @@ The import command imports a SurrealQL script file into a local or remote Surrea
- -p / --pass + `-p` / `--pass` @@ -53,7 +53,7 @@ The import command imports a SurrealQL script file into a local or remote Surrea
- -t / --token + `-t` / `--token` @@ -62,7 +62,7 @@ The import command imports a SurrealQL script file into a local or remote Surrea
- --ns + `--ns` @@ -71,7 +71,7 @@ The import command imports a SurrealQL script file into a local or remote Surrea
- --db + `--db` @@ -93,7 +93,7 @@ The import command imports a SurrealQL script file into a local or remote Surrea
- file + `file` diff --git a/src/content/doc-surrealdb/cli/isready.mdx b/src/content/doc-surrealdb/cli/isready.mdx index c02dac489..440e9dc04 100644 --- a/src/content/doc-surrealdb/cli/isready.mdx +++ b/src/content/doc-surrealdb/cli/isready.mdx @@ -25,7 +25,7 @@ The isready command attempts to connect to a remote SurrealDB server to detect i
- -e / --endpoint / --conn + `-e` / `--endpoint` / `--conn ` diff --git a/src/content/doc-surrealdb/cli/ml/export.mdx b/src/content/doc-surrealdb/cli/ml/export.mdx index 6c113df65..ad16141a2 100644 --- a/src/content/doc-surrealdb/cli/ml/export.mdx +++ b/src/content/doc-surrealdb/cli/ml/export.mdx @@ -26,7 +26,7 @@ The ML export command is used to export an existing machine learning model from
- -e, --endpoint / --conn + `-e,--endpoint/--conn` @@ -35,7 +35,7 @@ The ML export command is used to export an existing machine learning model from
- -u / --user + `-u / --user` @@ -44,7 +44,7 @@ The ML export command is used to export an existing machine learning model from
- -p / --pass + `-p / --pass` @@ -53,7 +53,7 @@ The ML export command is used to export an existing machine learning model from
- -t / --token + `-t` / `--token` @@ -62,7 +62,7 @@ The ML export command is used to export an existing machine learning model from
- --ns + `--ns` @@ -71,7 +71,7 @@ The ML export command is used to export an existing machine learning model from
- --db + `--db` @@ -80,7 +80,7 @@ The ML export command is used to export an existing machine learning model from
- --name + `--name` @@ -89,7 +89,7 @@ The ML export command is used to export an existing machine learning model from
- --version + `--version` @@ -111,7 +111,7 @@ The ML export command is used to export an existing machine learning model from
- file + `file` diff --git a/src/content/doc-surrealdb/cli/ml/import.mdx b/src/content/doc-surrealdb/cli/ml/import.mdx index 10c5ed539..33ef007e9 100644 --- a/src/content/doc-surrealdb/cli/ml/import.mdx +++ b/src/content/doc-surrealdb/cli/ml/import.mdx @@ -27,7 +27,7 @@ The ML import command is used to import a new machine learning model into Surrea
- -e, --endpoint / --conn + `-e, --endpoint / --conn` @@ -36,7 +36,7 @@ The ML import command is used to import a new machine learning model into Surrea
- -u / --user + `-u / --user` @@ -45,7 +45,7 @@ The ML import command is used to import a new machine learning model into Surrea
- -p / --pass + `-p / --pass` @@ -54,7 +54,7 @@ The ML import command is used to import a new machine learning model into Surrea
- --ns + `--ns` @@ -63,7 +63,7 @@ The ML import command is used to import a new machine learning model into Surrea
- -t / --token + `-t` / `--token` @@ -72,7 +72,7 @@ The ML import command is used to import a new machine learning model into Surrea
- --db + `--db` @@ -94,7 +94,7 @@ The ML import command is used to import a new machine learning model into Surrea
- file + `file` diff --git a/src/content/doc-surrealdb/cli/sql.mdx b/src/content/doc-surrealdb/cli/sql.mdx index 98b387022..5fd45bcb6 100644 --- a/src/content/doc-surrealdb/cli/sql.mdx +++ b/src/content/doc-surrealdb/cli/sql.mdx @@ -26,7 +26,7 @@ The SQL command starts a REPL for running or piping SurrealQL queries to a local
- -e / --endpoint / --conn + `-e` / `--endpoint` / `--conn` @@ -35,7 +35,7 @@ The SQL command starts a REPL for running or piping SurrealQL queries to a local
- -u / --user + `-u` / `--user` @@ -44,7 +44,7 @@ The SQL command starts a REPL for running or piping SurrealQL queries to a local
- -p / --pass + `-p` / `--pass` @@ -53,7 +53,7 @@ The SQL command starts a REPL for running or piping SurrealQL queries to a local
- --ns + `--ns` @@ -62,7 +62,7 @@ The SQL command starts a REPL for running or piping SurrealQL queries to a local
- --db + `--db` @@ -71,7 +71,7 @@ The SQL command starts a REPL for running or piping SurrealQL queries to a local
- --auth-level + `--auth-level` @@ -80,7 +80,7 @@ The SQL command starts a REPL for running or piping SurrealQL queries to a local
- -t / --token + `-t` / `--token` @@ -89,7 +89,7 @@ The SQL command starts a REPL for running or piping SurrealQL queries to a local
- --pretty + `--pretty` @@ -98,7 +98,7 @@ The SQL command starts a REPL for running or piping SurrealQL queries to a local
- --json + `--json` @@ -107,7 +107,7 @@ The SQL command starts a REPL for running or piping SurrealQL queries to a local
- --multi + `--multi` @@ -116,7 +116,7 @@ The SQL command starts a REPL for running or piping SurrealQL queries to a local
- -h / --help + `-h` / `--help` diff --git a/src/content/doc-surrealdb/cli/start.mdx b/src/content/doc-surrealdb/cli/start.mdx index 341f89da5..ecec089d1 100644 --- a/src/content/doc-surrealdb/cli/start.mdx +++ b/src/content/doc-surrealdb/cli/start.mdx @@ -31,7 +31,7 @@ The start command starts a SurrealDB server in memory, on disk, or in a distribu
- -b / --bind + `-b` / `--bind` @@ -40,7 +40,7 @@ The start command starts a SurrealDB server in memory, on disk, or in a distribu
- -l / --log + `-l` / `--log` @@ -49,7 +49,7 @@ The start command starts a SurrealDB server in memory, on disk, or in a distribu
- -u / --user + `-u` / `--user` @@ -58,7 +58,7 @@ The start command starts a SurrealDB server in memory, on disk, or in a distribu
- -p / --pass + `-p` / `--pass` @@ -67,7 +67,7 @@ The start command starts a SurrealDB server in memory, on disk, or in a distribu
- --auth + `--auth` @@ -76,7 +76,7 @@ The start command starts a SurrealDB server in memory, on disk, or in a distribu
- --no-identification-headers + `--no-identification-headers` @@ -85,7 +85,7 @@ The start command starts a SurrealDB server in memory, on disk, or in a distribu
- -s / --strict + `-s` / `--strict` @@ -107,7 +107,7 @@ The start command starts a SurrealDB server in memory, on disk, or in a distribu
- -b / --bind + `-b` / `--bind` @@ -116,7 +116,7 @@ The start command starts a SurrealDB server in memory, on disk, or in a distribu
- -l / --log + `-l` / `--log` @@ -125,7 +125,7 @@ The start command starts a SurrealDB server in memory, on disk, or in a distribu
- -u / --user + `-u` / `--user` @@ -134,7 +134,7 @@ The start command starts a SurrealDB server in memory, on disk, or in a distribu
- -p / --pass + `-p` / `--pass` @@ -143,7 +143,7 @@ The start command starts a SurrealDB server in memory, on disk, or in a distribu
- --unauthenticated + `--unauthenticated` @@ -152,7 +152,7 @@ The start command starts a SurrealDB server in memory, on disk, or in a distribu
- --no-identification-headers + `--no-identification-headers` @@ -161,7 +161,7 @@ The start command starts a SurrealDB server in memory, on disk, or in a distribu
- -s / --strict + `-s` / `--strict` @@ -194,7 +194,7 @@ file:// is deprecated, please use surrealkv:// or rocksdb://
- path + `path` @@ -208,12 +208,12 @@ file:// is deprecated, please use surrealkv:// or rocksdb://
    -
  • rocksdb for RocksDB
  • -
  • fdb for FoundationDB
  • -
  • indxdb for IndexedDB
  • -
  • memory (or no argument) for in-memory storage
  • -
  • surrealkv for SurrealKV
  • -
  • tikv for TiKV
  • +
  • `rocksdb` for RocksDB
  • +
  • `fdb` for FoundationDB
  • +
  • `indxdb` for IndexedDB
  • +
  • `memory` (or no argument) for in-memory storage
  • +
  • `surrealkv` for SurrealKV
  • +
  • `tikv` for TiKV
- --nightly + `--nightly` @@ -37,7 +37,7 @@ The upgrade command upgrades SurrealDB to the latest version, nightly or a speci
- --alpha + `--alpha` @@ -46,7 +46,7 @@ The upgrade command upgrades SurrealDB to the latest version, nightly or a speci
- --beta + `--beta` @@ -55,7 +55,7 @@ The upgrade command upgrades SurrealDB to the latest version, nightly or a speci
- --version + `--version` @@ -64,7 +64,7 @@ The upgrade command upgrades SurrealDB to the latest version, nightly or a speci
- --dry-run + `--dry-run` diff --git a/src/content/doc-surrealdb/cli/version.mdx b/src/content/doc-surrealdb/cli/version.mdx index 7ecf95802..02b657ee9 100644 --- a/src/content/doc-surrealdb/cli/version.mdx +++ b/src/content/doc-surrealdb/cli/version.mdx @@ -26,7 +26,7 @@ The version command outputs the current version of the installed command-line to
- -e / --endpoint + `-e` / `--endpoint` diff --git a/src/content/doc-surrealdb/embedding/rust.mdx b/src/content/doc-surrealdb/embedding/rust.mdx index 93b4ec6d5..fbf285d01 100644 --- a/src/content/doc-surrealdb/embedding/rust.mdx +++ b/src/content/doc-surrealdb/embedding/rust.mdx @@ -57,7 +57,7 @@ Open `src/main.rs` and replace everything in there with the following code to tr ```rust use serde::{Deserialize, Serialize}; -use surrealdb::sql::Thing; +use surrealdb::RecordId; use surrealdb::Surreal; // For an in memory database @@ -87,7 +87,7 @@ struct Responsibility { #[derive(Debug, Deserialize)] struct Record { #[allow(dead_code)] - id: Thing, + id: RecordId, } #[tokio::main]