2022-12-30 08:23:19 +00:00
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Serialize;
|
2023-01-07 08:32:18 +00:00
|
|
|
use surrealdb::engine::remote::ws::Ws;
|
2022-12-30 08:23:19 +00:00
|
|
|
use surrealdb::opt::auth::Root;
|
2023-03-30 10:41:44 +00:00
|
|
|
use surrealdb::sql::thing;
|
|
|
|
use surrealdb::sql::Thing;
|
2022-12-30 08:23:19 +00:00
|
|
|
use surrealdb::Surreal;
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
struct User {
|
2023-03-30 10:41:44 +00:00
|
|
|
id: Thing,
|
2022-12-30 08:23:19 +00:00
|
|
|
name: String,
|
|
|
|
company: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> surrealdb::Result<()> {
|
|
|
|
let db = Surreal::new::<Ws>("localhost:8000").await?;
|
|
|
|
|
|
|
|
db.signin(Root {
|
|
|
|
username: "root",
|
|
|
|
password: "root",
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
db.use_ns("namespace").use_db("database").await?;
|
|
|
|
|
2023-02-09 10:28:06 +00:00
|
|
|
let sql = "CREATE user SET name = $name, company = $company";
|
2022-12-30 08:23:19 +00:00
|
|
|
|
|
|
|
let mut results = db
|
|
|
|
.query(sql)
|
|
|
|
.bind(User {
|
2023-03-30 10:41:44 +00:00
|
|
|
id: thing("user:john")?,
|
2022-12-30 08:23:19 +00:00
|
|
|
name: "John Doe".to_owned(),
|
|
|
|
company: "ACME Corporation".to_owned(),
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// print the created user:
|
|
|
|
let user: Option<User> = results.take(0)?;
|
|
|
|
println!("{user:?}");
|
|
|
|
|
2023-02-09 10:28:06 +00:00
|
|
|
let mut response = db.query("SELECT * FROM user WHERE name.first = 'John'").await?;
|
2022-12-30 08:23:19 +00:00
|
|
|
|
|
|
|
// print all users:
|
|
|
|
let users: Vec<User> = response.take(0)?;
|
|
|
|
println!("{users:?}");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|