insert()
Method Syntaxdb.insert(resource).content(data); db.insert(resource).relation(data);
Argument | Description | ||
---|---|---|---|
resource | The table name or the specific record ID to create. | ||
data | The document / record data to insert. | ||
data | The relation table data to insert. |
Inserting a record with a specific ID:
use serde::{Deserialize, Serialize}; use surrealdb::engine::any::connect; use surrealdb::opt::auth::Root; struct Settings { active: bool, marketing: bool, } struct Data<'a> { name: &'a str, settings: Settings, } struct Person { name: String, settings: Settings, } async fn main() -> surrealdb::Result<()> { let db = connect("ws://localhost:8000").await?; db.signin(Root { username: "root", password: "root", }) .await?; db.use_ns("ns").use_db("db").await?; let person: Option<Person> = db .insert(("person", "tobie")) .content(Data { name: "Tobie", settings: Settings { active: true, marketing: true, }, }) .await?; dbg!(person); Ok(()) }
Inserting multiple records into a table:
use serde::{Deserialize, Serialize}; use surrealdb::engine::any::connect; use surrealdb::opt::auth::Root; struct Settings { active: bool, marketing: bool, } struct Data<'a> { name: &'a str, settings: Settings, } struct Person { name: String, settings: Settings, } async fn main() -> surrealdb::Result<()> { let db = connect("ws://localhost:8000").await?; db.signin(Root { username: "root", password: "root", }) .await?; db.use_ns("ns").use_db("db").await?; let people: Vec<Person> = db .insert("person") .content(vec![ Data { name: "Tobie", settings: Settings { active: true, marketing: false, }, }, Data { name: "Jaime", settings: Settings { active: true, marketing: true, }, }, ]) .await?; dbg!(people); Ok(()) }
The .insert()
method can take an empty tuple instead of a table ID if the following method contains a record ID.
use serde::{Deserialize, Serialize}; use surrealdb::engine::any::connect; use surrealdb::opt::auth::Root; use surrealdb::RecordId; struct Settings { active: bool, marketing: bool, } struct Data<'a> { id: RecordId, name: &'a str, } struct Person { name: String, id: RecordId, } async fn main() -> surrealdb::Result<()> { let db = connect("ws://localhost:8000").await?; db.signin(Root { username: "root", password: "root", }) .await?; db.use_ns("ns").use_db("db").await?; let people: Vec<Person> = db .insert(()) .content(vec![ Data { id: RecordId::from(("person", "tobie")), name: "Tobie", }, Data { id: RecordId::from(("person", "jaime")), name: "Jaime", }, ]) .await?; dbg!(people); Ok(()) }
An example of two person
records and one company
record, followed by .insert().relation()
to create a relation between them. Note the usage of the #[serde(rename)]
attribute to interface between the Rust struct Founded
and the original relation table, which must have an in
and an out
field.
use serde::{Deserialize, Serialize}; use surrealdb::engine::any::connect; use surrealdb::opt::auth::Root; use surrealdb::RecordId; struct Settings { active: bool, marketing: bool, } struct Data<'a> { id: RecordId, name: &'a str, } struct Record { name: String, id: RecordId, } struct Founded { founder: RecordId, company: RecordId, } async fn main() -> surrealdb::Result<()> { let db = connect("ws://localhost:8000").await?; db.signin(Root { username: "root", password: "root", }) .await?; db.use_ns("ns").use_db("db").await?; let records: Vec<Record> = db .insert(()) .content(vec![ Data { id: RecordId::from(("person", "tobie")), name: "Tobie", }, Data { id: RecordId::from(("person", "jaime")), name: "Jaime", }, Data { id: RecordId::from(("company", "surrealdb")), name: "SurrealDB", }, ]) .await?; dbg!(records); let founded: Vec<Founded> = db .insert("founded") .relation(vec![ Founded { founder: RecordId::from(("person", "tobie")), company: RecordId::from(("company", "surrealdb")), }, Founded { founder: RecordId::from(("person", "jaime")), company: RecordId::from(("company", "surrealdb")), }, ]) .await?; dbg!(founded); Ok(()) }
The equivalent SurrealQL statements to create and query the relations are:
RELATE [person:jaime, person:tobie]->founded->company:surrealdb; SELECT ->founded->company FROM person;