update()
Update all or specific records in the database.
Method Syntaxdb.update(resource)
The .update()
method is followed by second method that refers to the type of update to use: an update with .content()
, .merge()
, or .patch()
.
.update().content()
Updates all records in a table, or a specific record, in the database.
Method Syntaxdb.update(resource).content(data)
NoteThis function replaces the current document / record data with the specified data.
Argument | Type | Description | |||
---|---|---|---|---|---|
resource | The table name or the specific record ID to create. | ||||
data | The document / record data to insert. |
use serde::{Deserialize, Serialize}; use surrealdb::engine::remote::ws::Ws; use surrealdb::opt::auth::Root; use surrealdb::Surreal; struct Person { name: Option<String>, company: Option<String>, settings: Option<Settings>, } struct Settings { active: bool, marketing: bool, } struct Company { company: String, } 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?; db.query("CREATE person:tobie, person:jaime").await?; // Update all records in a table let people: Vec<Person> = db .update("person") .content(Company { company: "SurrealDB".into(), }) .await?; dbg!(people); Ok(()) }
This function will run the following query in the database:
UPDATE $resource CONTENT $data;
.update().merge()
Modifies all records in a table, or a specific record, in the database.
Method Syntaxdb.update(resource).merge(data)
NoteThis function merges the current document / record data with the specified data.
Argument | Description | ||||
---|---|---|---|---|---|
resource | The table name or the specific record ID to create. | ||||
data | resource | The document / record data to insert. |
use serde::{Deserialize, Serialize}; use surrealdb::engine::remote::ws::Ws; use surrealdb::opt::auth::Root; use surrealdb::Surreal; struct Person { name: String, company: Option<String>, active: Option<bool>, marketing: Option<bool>, } struct Settings { active: bool, marketing: bool, } struct Company { company: String, } 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?; db.query("CREATE person:tobie SET name = 'Tobie'; CREATE person:jaime SET name = 'jaime';") .await?; // Update all records in a table let people: Vec<Person> = db .update("person") .merge(Company { company: "SurrealDB".into(), }) .await?; dbg!(people); // Update a single record let person: Option<Person> = db .update(("person", "jaime")) .merge(Settings { active: true, marketing: true, }) .await?; dbg!(person); Ok(()) }
This function will run the following query in the database:
UPDATE $resource MERGE $data;
.update().patch()
Applies JSON Patch changes to all records, or a specific record, in the database.
Method Syntaxdb.update(resource).patch(patch_op)
NoteThis function patches the current document / record data with the specified JSON Patch data.
Argument | Description | ||
---|---|---|---|
resource | The table name or the specific record ID to modify. | ||
data | The JSON Patch data with which to modify the records. |
The .patch()
method uses a struct called a PatchOp
that contains the four methods add()
, change()
, remove()
, and replace()
. Each of these methods takes different arguments depending on the operation. For example, PathOp::remove()
only takes a single argument (a path), while PathOp::replace()
takes a second value for the replacement value.
use serde::{Deserialize, Serialize}; use surrealdb::engine::remote::ws::Ws; use surrealdb::opt::auth::Root; use surrealdb::opt::PatchOp; use surrealdb::sql::Datetime; use surrealdb::Surreal; struct Person { name: String, company: Option<String>, settings: Option<Settings>, created_at: Option<Datetime>, tags: Option<Vec<String>>, } struct Settings { active: bool, } 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?; db.query( " CREATE person:tobie SET name = 'Tobie', company = 'SurrealDB'; CREATE person:jaime SET name = 'jaime', company = 'SurrealDB';", ) .await?; // Update all records in a table let people: Vec<Person> = db .update("person") .patch(PatchOp::replace("/created_at", Datetime::default())) .await?; dbg!(people); // Update a record with a specific ID let person: Option<Person> = db .update(("person", "tobie")) .patch(PatchOp::replace("/settings/active", false)) .patch(PatchOp::add("/tags", &["developer", "engineer"])) .patch(PatchOp::remove("/company")) .await?; dbg!(person); Ok(()) }
This function will run the following query in the database:
UPDATE $resource PATCH $data;