Created
March 12, 2024 16:46
-
-
Save RGGH/d23d14d2df7a3909ec266406cd770d3f to your computer and use it in GitHub Desktop.
surreal_rust_function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env; | |
use serde::Serialize; | |
use surrealdb::engine::remote::ws::Ws; | |
use surrealdb::opt::auth::Root; | |
use surrealdb::Surreal; | |
#[derive(Debug, Serialize)] | |
struct Name<'a> { | |
first: &'a str, | |
last: &'a str, | |
} | |
#[derive(Debug, Serialize)] | |
struct Person<'a> { | |
title: &'a str, | |
name: Name<'a>, | |
} | |
#[tokio::main] | |
async fn main() -> surrealdb::Result<()> { | |
let args: Vec<String> = env::args().collect(); | |
// Check if there are enough arguments | |
if args.len() < 4 { | |
println!("Not enough arguments, supply first name, last name, job"); | |
} | |
// Access individual arguments | |
let fname = &args[1]; | |
let lname = &args[2]; | |
let user_secret = &args[3]; | |
// Connect to the server | |
let db = Surreal::new::<Ws>("127.0.0.1:8000").await?; | |
// Signin as a namespace, database, or root user | |
db.signin(Root { | |
username: "root", | |
password: "root", | |
}) | |
.await?; | |
// Select a specific namespace / database | |
db.use_ns("test").use_db("test2").await?; | |
db.set( | |
"name", | |
Name { | |
first: fname, | |
last: lname, | |
}, | |
) | |
.await?; | |
// example to take user input and process with a SurrealDB function | |
db.set("job",user_secret.to_string()).await?; | |
// Use the variable in a query | |
db.query("CREATE person SET name = $name, hashed=crypto::sha256($job)").await?; | |
let ress = db | |
.query("SELECT * FROM person WHERE name.first = $name.first") | |
.await?; | |
println!("{:?}", ress); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment