Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Rust examples #932

Merged
merged 5 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 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,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<String>,
Expand All @@ -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
Expand Down
25 changes: 16 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,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<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
33 changes: 16 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,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<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::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<Person> = db.create("person").await?;
db.create(Resource::from("person")).await?;

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

Expand Down
8 changes: 7 additions & 1 deletion src/content/doc-sdk-rust/methods/import.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
24 changes: 13 additions & 11 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 @@ -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<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(())
}
```

`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<Any>> = 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?;
};
// 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(())
}
```
Loading