From 8362fc9b125f190190c4d1d8eb5f80a837c72d09 Mon Sep 17 00:00:00 2001 From: Rushmore Mushambi Date: Thu, 10 Oct 2024 16:47:30 +0100 Subject: [PATCH] Ensure examples run --- src/content/doc-sdk-rust/methods/create.mdx | 6 +++ src/content/doc-sdk-rust/methods/delete.mdx | 6 +++ src/content/doc-sdk-rust/methods/export.mdx | 12 ++++++ src/content/doc-sdk-rust/methods/import.mdx | 6 +++ src/content/doc-sdk-rust/methods/init.mdx | 7 +++- src/content/doc-sdk-rust/methods/insert.mdx | 30 +++++++++++++- src/content/doc-sdk-rust/methods/query.mdx | 46 +++++++++++++++------ src/content/doc-sdk-rust/methods/run.mdx | 19 +++++++++ src/content/doc-sdk-rust/methods/select.mdx | 10 +---- src/content/doc-sdk-rust/methods/set.mdx | 7 ++++ src/content/doc-sdk-rust/methods/unset.mdx | 7 ++++ src/content/doc-sdk-rust/methods/update.mdx | 7 ++++ src/content/doc-sdk-rust/methods/upsert.mdx | 21 ++++++++++ src/content/doc-sdk-rust/methods/use.mdx | 2 +- src/content/doc-sdk-rust/setup.mdx | 7 +++- 15 files changed, 166 insertions(+), 27 deletions(-) diff --git a/src/content/doc-sdk-rust/methods/create.mdx b/src/content/doc-sdk-rust/methods/create.mdx index 04f30f968..91b91dcc2 100644 --- a/src/content/doc-sdk-rust/methods/create.mdx +++ b/src/content/doc-sdk-rust/methods/create.mdx @@ -48,6 +48,7 @@ db.create(resource).content(data) ```rust use serde::{Deserialize, Serialize}; use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; use surrealdb::RecordId; #[derive(Debug, Serialize, Deserialize)] @@ -64,6 +65,11 @@ struct Record { #[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 ebefb73c4..9d3e1436c 100644 --- a/src/content/doc-sdk-rust/methods/delete.mdx +++ b/src/content/doc-sdk-rust/methods/delete.mdx @@ -38,6 +38,7 @@ db.delete(resource) ```rust use serde::{Deserialize, Serialize}; use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; use surrealdb::opt::Resource; use surrealdb::RecordId; @@ -49,6 +50,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?; // Create three `person` records diff --git a/src/content/doc-sdk-rust/methods/export.mdx b/src/content/doc-sdk-rust/methods/export.mdx index f1067103c..fe259c53e 100644 --- a/src/content/doc-sdk-rust/methods/export.mdx +++ b/src/content/doc-sdk-rust/methods/export.mdx @@ -43,11 +43,17 @@ The `.export()` method can be used to save the contents of a database to a file. ```rust use surrealdb::engine::any::connect; +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 @@ -63,11 +69,17 @@ If an empty tuple is passed in for the file name, the `.export()` method will in ```rust use futures::StreamExt; use surrealdb::engine::any::connect; +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 diff --git a/src/content/doc-sdk-rust/methods/import.mdx b/src/content/doc-sdk-rust/methods/import.mdx index 599ddd20e..372378371 100644 --- a/src/content/doc-sdk-rust/methods/import.mdx +++ b/src/content/doc-sdk-rust/methods/import.mdx @@ -20,10 +20,16 @@ db.import(source) ```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/init.mdx b/src/content/doc-sdk-rust/methods/init.mdx index 74374f571..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() @@ -30,7 +31,7 @@ async fn main() -> surrealdb::Result<()> { } ``` -`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; @@ -38,10 +39,12 @@ use std::sync::LazyLock; use surrealdb::engine::any::Any; use surrealdb::Surreal; -static DB: LazyLock> = LazyLock::new(Surreal::init); +static DB: LazyLock> = LazyLock::new(Surreal::init); #[tokio::main] async fn main() -> surrealdb::Result<()> { + // 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(()) diff --git a/src/content/doc-sdk-rust/methods/insert.mdx b/src/content/doc-sdk-rust/methods/insert.mdx index b11eb0f79..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 serde::{Deserialize, Serialize}; use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; #[derive(Debug, Serialize, Deserialize)] struct Settings { @@ -79,6 +80,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 person: Option = db @@ -101,6 +108,7 @@ Inserting multiple records into a table: ```rust use serde::{Deserialize, Serialize}; use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; #[derive(Debug, Serialize, Deserialize)] struct Settings { @@ -124,6 +132,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 people: Vec = db @@ -155,6 +169,7 @@ The `.insert()` method can take an empty tuple instead of a table ID if the foll ```rust use serde::{Deserialize, Serialize}; use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; use surrealdb::RecordId; #[derive(Debug, Serialize, Deserialize)] @@ -179,6 +194,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 people: Vec = db @@ -204,6 +225,7 @@ An example of two `person` records and one `company` record, followed by `.inser ```rust use serde::{Deserialize, Serialize}; use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; use surrealdb::RecordId; #[derive(Debug, Serialize, Deserialize)] @@ -236,6 +258,12 @@ struct Founded { 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 diff --git a/src/content/doc-sdk-rust/methods/query.mdx b/src/content/doc-sdk-rust/methods/query.mdx index 41994cd9b..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#" @@ -135,6 +150,7 @@ And then apply the `.bind()` method to pass the parameter in. ```rust use serde::Deserialize; use surrealdb::engine::any::connect; +use surrealdb::opt::auth::Root; use surrealdb::RecordId; #[derive(Debug, Deserialize)] @@ -146,12 +162,18 @@ 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 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 8a60a5a41..0d5df76d1 100644 --- a/src/content/doc-sdk-rust/methods/select.mdx +++ b/src/content/doc-sdk-rust/methods/select.mdx @@ -50,18 +50,11 @@ use serde::Deserialize; use surrealdb::engine::remote::ws::Ws; use surrealdb::opt::auth::Root; use surrealdb::RecordId; -use surrealdb::RecordIdKey; use surrealdb::Surreal; -// Defining your own custom RecordId struct and using that in place of `RecordId` -#[derive(Debug, Deserialize)] -struct CustomRecordId { - id: RecordIdKey, -} - #[derive(Debug, Deserialize)] struct Person { - id: CustomRecordId, + id: RecordId, name: String, age: u8, } @@ -90,7 +83,6 @@ async fn main() -> surrealdb::Result<()> { Ok(()) } - ``` ### Translated query 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..2d9093eeb 100644 --- a/src/content/doc-sdk-rust/methods/update.mdx +++ b/src/content/doc-sdk-rust/methods/update.mdx @@ -258,6 +258,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 +281,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 2a2c5943d..12c8b7218 100644 --- a/src/content/doc-sdk-rust/methods/upsert.mdx +++ b/src/content/doc-sdk-rust/methods/upsert.mdx @@ -60,6 +60,7 @@ db.upsert(resource).content(data) ```rust use serde::{Deserialize, Serialize}; use surrealdb::engine::remote::ws::Ws; +use surrealdb::opt::auth::Root; use surrealdb::RecordId; use surrealdb::Surreal; @@ -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 cd9771c37..f635fd217 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. --- -# `use_ns()`, `use_ds()` +# `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 1755c7e60..a1f813574 100644 --- a/src/content/doc-sdk-rust/setup.mdx +++ b/src/content/doc-sdk-rust/setup.mdx @@ -152,6 +152,7 @@ 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::opt::Resource; use surrealdb::RecordId; use surrealdb::Surreal; use surrealdb::Value; @@ -206,8 +207,10 @@ async fn main() -> surrealdb::Result<()> { 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?;