set()
Assigns a value as a parameter for this connection.
Method Syntaxdb.set(key, value)
This is equivalent to using a LET
statement in SurrealQL, such as this one.
LET $name = { first: "Tobie", last: "Morgan Hitchcock", };
Argument | Description | ||
---|---|---|---|
key | Specifies the name of the variable. | ||
val | Assigns the value to the variable name. |
use serde::Serialize; use surrealdb::engine::remote::ws::Ws; use surrealdb::opt::auth::Root; use surrealdb::Surreal; struct Name<'a> { first: &'a str, last: &'a str, } async fn main() -> surrealdb::Result<()> { let db = Surreal::new::<Ws>("127.0.0.1:8000").await?; db.signin(Root { username: "root", password: "root", }) .await?; db.use_ns("ns").use_db("db").await?; // Assign the variable on the connection db.set( "name", Name { first: "Tobie", last: "Morgan Hitchcock", }, ) .await?; // Use the variable in a subsequent query let create = db.query("CREATE person SET name = $name").await?; dbg!(create); // Use the variable in a subsequent query let select = db .query("SELECT * FROM person WHERE name.first = $name.first") .await?; dbg!(select); Ok(()) }