How to use rust sdk to write a transaction? #4090
-
Same as the title. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
There's no dedicated method for a transaction but you can put multiple statements into a single query, including inside a transaction:
Is that enough for your use case? |
Beta Was this translation helpful? Give feedback.
-
I saw a transaction method in rust, but it doesn't seem to work. Even if I don't run commit, the data is still written to DB. I tried to do transaction with JS in one transaction by concatenating the query strings into one because the js library doesn't have transaction method. https://gist.github.com/HuakunShen/4530b36a5934c22084cd44f0f16f021d#file-surreal-rocks-rs-L49 let tx = db.transaction().await?;
// Process each record
for result in rdr.records() {
let record = result?;
let birth_year = match &record[2] {
"\\N" => None,
year => Some(year.parse::<i32>()?),
};
let death_year = match &record[3] {
"\\N" => None,
year => Some(year.parse::<i32>()?),
};
// Create record in SurrealDB
let name = NameBasic {
nconst: record[0].to_string(),
primary_name: record[1].to_string(),
birth_year,
death_year,
primary_profession: record[4].to_string(),
known_for_titles: record[5].to_string(),
};
// Insert using the nconst as the record ID
(&tx)
.create::<Option<NameBasic>>("name_basic")
.content(name)
.await?;
count += 1;
if count % 100_000 == 0 {
println!("Processed {} NameBasics", count);
}
}
println!("Starting database insert...");
tx.commit().await?; |
Beta Was this translation helpful? Give feedback.
There's no dedicated method for a transaction but you can put multiple statements into a single query, including inside a transaction:
Is that enough for your use case?