Skip to content

Commit

Permalink
Update Rust examples
Browse files Browse the repository at this point in the history
  • Loading branch information
rushmorem committed Oct 10, 2024
1 parent 5c2344f commit 0e17fe0
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 72 deletions.
10 changes: 5 additions & 5 deletions src/content/doc-sdk-rust/methods/create.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ db.create(resource).content(data)
### Example usage

```rust
use serde::{Deserialize, Serialize};
use surrealdb::engine::any::connect;
use surrealdb::RecordId;

#[derive(Debug, Serialize, Deserialize)]
struct Person {
name: Option<String>,
Expand All @@ -54,13 +58,9 @@ 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?;
Expand Down
19 changes: 10 additions & 9 deletions src/content/doc-sdk-rust/methods/delete.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,29 @@ db.delete(resource)
### Example usage

```rust
use serde::{Deserialize, Serialize};
use surrealdb::engine::any::connect;
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.use_ns("ns").use_db("db").await?;

// Create three `person` records
let _: Option<Person> = db.create(("person", "one")).await?;
let _: Option<Person> = db.create(("person", "two")).await?;
let _: Option<Person> = 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<Person> = db.delete(("person", "one")).await?;
let deleted_rest: Vec<Person> = db.delete("person").await?;
dbg!(deleted_one);
let deleted_rest: Vec<Person> = db.delete("person").await?;
dbg!(deleted_rest);
Ok(())
}
Expand Down
21 changes: 4 additions & 17 deletions src/content/doc-sdk-rust/methods/export.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,49 +42,36 @@ 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::Resource;

#[tokio::main]
async fn main() -> surrealdb::Result<()> {
let db = connect("http://localhost:8000").await?;
db.use_ns("ns").use_db("db").await?;

// Create a `person` record
let _: Option<Person> = 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::Resource;

#[tokio::main]
async fn main() -> surrealdb::Result<()> {
let db = connect("http://localhost:8000").await?;
db.use_ns("ns").use_db("db").await?;

// Create a `person` record
let _: Option<Person> = db.create("person").await?;
db.create(Resource::from("person")).await?;

let mut stream = db.export(()).await?;

Expand Down
2 changes: 1 addition & 1 deletion src/content/doc-sdk-rust/methods/import.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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
Expand Down
19 changes: 9 additions & 10 deletions src/content/doc-sdk-rust/methods/init.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ Surreal::init()
### Example usage

```rust
use std::sync::LazyLock;
use surrealdb::engine::remote::ws::{Client, Ws};
use surrealdb::Surreal;

static DB: LazyLock<Surreal<Client>> = LazyLock::new(Surreal::init);

#[tokio::main]
async fn main() -> surrealdb::Result<()> {
let db: Surreal<Client> = Surreal::init();
// Connect to the database
db.connect::<Ws>("127.0.0.1:8000").await?;
DB.connect::<Ws>("127.0.0.1:8000").await?;
Ok(())
}
```
Expand All @@ -32,19 +34,16 @@ async fn main() -> surrealdb::Result<()> {

```rust
use std::env;

use std::sync::LazyLock;
use surrealdb::engine::any::Any;
use surrealdb::Surreal;

static DB: LazyLock<Surreal<Client>> = LazyLock::new(Surreal::init);

#[tokio::main]
async fn main() -> surrealdb::Result<()> {
let db: Surreal<Any> = Surreal::init();

if let Ok(path) = env::var("DB_ENDPOINT") {
db.connect(path).await?;
} else {
db.connect("mem://").await?;
};
let endpoint = env::var("DB_ENDPOINT").unwrap_or_else(|_| "mem://".to_owned());
DB.connect(endpoint).await?;
Ok(())
}
```
42 changes: 21 additions & 21 deletions src/content/doc-sdk-rust/methods/insert.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Inserting a record with a specific ID:

```rust
se serde::{Deserialize, Serialize};
use surrealdb::{engine::any::connect, sql::Thing};
use surrealdb::engine::any::connect;

#[derive(Debug, Serialize, Deserialize)]
struct Settings {
Expand All @@ -73,7 +73,6 @@ struct Data<'a> {
struct Person {
name: String,
settings: Settings,
id: Thing,
}

#[tokio::main]
Expand Down Expand Up @@ -101,7 +100,7 @@ Inserting multiple records into a table:

```rust
use serde::{Deserialize, Serialize};
use surrealdb::{engine::any::connect, sql::Thing};
use surrealdb::engine::any::connect;

#[derive(Debug, Serialize, Deserialize)]
struct Settings {
Expand All @@ -119,7 +118,6 @@ struct Data<'a> {
struct Person {
name: String,
settings: Settings,
id: Thing,
}

#[tokio::main]
Expand Down Expand Up @@ -156,7 +154,8 @@ 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, sql, sql::Thing};
use surrealdb::engine::any::connect;
use surrealdb::RecordId;

#[derive(Debug, Serialize, Deserialize)]
struct Settings {
Expand All @@ -166,14 +165,14 @@ 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]
Expand All @@ -186,11 +185,11 @@ async fn main() -> surrealdb::Result<()> {
.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",
},
])
Expand All @@ -204,7 +203,8 @@ 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::RecordId;

#[derive(Debug, Serialize, Deserialize)]
struct Settings {
Expand All @@ -214,22 +214,22 @@ 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]
Expand All @@ -242,15 +242,15 @@ async fn main() -> surrealdb::Result<()> {
.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",
},
])
Expand All @@ -261,12 +261,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?;
Expand Down
11 changes: 6 additions & 5 deletions src/content/doc-sdk-rust/methods/select.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,19 @@ let person: Option<Person> = 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::RecordIdKey;
use surrealdb::Surreal;

// defining your own custom Thing struct and using that in place of `Thing` in the `Id` struct
// Defining your own custom RecordId struct and using that in place of `RecordId`
#[derive(Debug, Deserialize)]
struct CustomThing {
id: Id,
struct CustomRecordId {
id: RecordIdKey,
}

#[derive(Debug, Deserialize)]
struct Person {
id: CustomThing,
id: CustomRecordId,
name: String,
age: u8,
}
Expand Down
4 changes: 2 additions & 2 deletions src/content/doc-sdk-rust/methods/select_live.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ 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::{Notification, Surreal};
use surrealdb::RecordId;

#[derive(Debug, Deserialize)]
struct Person {
id: Thing,
id: RecordId,
}

// Handle the result of the live query notification
Expand Down
4 changes: 2 additions & 2 deletions src/content/doc-sdk-rust/methods/upsert.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ db.upsert(resource).content(data)
```rust
use serde::{Deserialize, Serialize};
use surrealdb::engine::remote::ws::Ws;
use surrealdb::sql::Thing;
use surrealdb::RecordId;
use surrealdb::Surreal;

#[derive(Debug, Serialize, Deserialize)]
struct Person {
id: Thing,
id: RecordId,
name: Option<String>,
company: Option<String>,
settings: Option<Settings>,
Expand Down

0 comments on commit 0e17fe0

Please sign in to comment.