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;
|
|
|
|
use surrealdb::sql::statements::BeginStatement;
|
|
|
|
use surrealdb::sql::statements::CommitStatement;
|
|
|
|
use surrealdb::Surreal;
|
|
|
|
|
|
|
|
#[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?;
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let response = db
|
|
|
|
|
|
|
|
// Start transaction
|
2024-04-02 20:12:08 +00:00
|
|
|
.query(BeginStatement::default())
|
2022-12-30 08:23:19 +00:00
|
|
|
|
|
|
|
// Setup accounts
|
|
|
|
.query("
|
|
|
|
CREATE account:one SET balance = 135605.16;
|
|
|
|
CREATE account:two SET balance = 91031.31;
|
|
|
|
")
|
|
|
|
|
|
|
|
// Move money
|
|
|
|
.query("
|
|
|
|
UPDATE account:one SET balance += 300.00;
|
|
|
|
UPDATE account:two SET balance -= 300.00;
|
|
|
|
")
|
|
|
|
|
|
|
|
// Finalise
|
2024-04-02 20:12:08 +00:00
|
|
|
.query(CommitStatement::default())
|
2022-12-30 08:23:19 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
// See if any errors were returned
|
|
|
|
response.check()?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|