Skip to content

Commit

Permalink
Ensure examples run
Browse files Browse the repository at this point in the history
  • Loading branch information
rushmorem committed Oct 10, 2024
1 parent 9cddd9b commit 8362fc9
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 27 deletions.
6 changes: 6 additions & 0 deletions src/content/doc-sdk-rust/methods/create.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/content/doc-sdk-rust/methods/delete.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/content/doc-sdk-rust/methods/export.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/content/doc-sdk-rust/methods/import.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
7 changes: 5 additions & 2 deletions src/content/doc-sdk-rust/methods/init.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -30,18 +31,20 @@ async fn main() -> surrealdb::Result<()> {
}
```

`Surreal::init()` by default will create an instance of `Surreal<Any>`, allowing you to choose at runtime which way to connect.
`Surreal::init()` can also be used to create an instance of `Surreal<Any>`, 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<Surreal<Client>> = LazyLock::new(Surreal::init);
static DB: LazyLock<Surreal<Any>> = 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(())
Expand Down
30 changes: 29 additions & 1 deletion src/content/doc-sdk-rust/methods/insert.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Person> = db
Expand All @@ -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 {
Expand All @@ -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<Person> = db
Expand Down Expand Up @@ -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)]
Expand All @@ -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<Person> = db
Expand All @@ -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)]
Expand Down Expand Up @@ -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<Record> = db
Expand Down
46 changes: 34 additions & 12 deletions src/content/doc-sdk-rust/methods/query.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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 {
Expand All @@ -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#"
Expand Down Expand Up @@ -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)]
Expand All @@ -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<Person> = result.take(0)?;
Expand Down
19 changes: 19 additions & 0 deletions src/content/doc-sdk-rust/methods/run.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand All @@ -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 };")
Expand All @@ -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 {
Expand All @@ -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(
Expand Down
10 changes: 1 addition & 9 deletions src/content/doc-sdk-rust/methods/select.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -90,7 +83,6 @@ async fn main() -> surrealdb::Result<()> {

Ok(())
}

```

### Translated query
Expand Down
Loading

0 comments on commit 8362fc9

Please sign in to comment.