create()
Creates one or more records in the database.
Method Syntax
db.create(resource).content(data)
Arguments
| Argument | Type | Description |
|---|
resource | The table name or the specific record ID to create. |
data | The document / record data to insert. |
Example usage
use surrealdb::engine::any::connect;
use surrealdb::opt::auth::Root;
use surrealdb_types::{RecordId, SurrealValue};
#[derive(Debug, SurrealValue)]
struct Person {
name: Option<String>,
marketing: Option<bool>,
}
#[derive(Debug, SurrealValue)]
struct Record {
id: RecordId,
}
#[tokio::main]
async fn main() -> surrealdb::Result<()> {
let db = connect("ws://localhost:8000").await?;
db.signin(Root {
username: "root".to_string(),
password: "secret".to_string(),
})
.await?;
db.use_ns("main").use_db("main").await?;
let person: Option<Person> = db.create("person").await?;
dbg!(person);
let record: Option<Record> = db
.create(("person", "tobie"))
.content(Person {
name: Some("Tobie".into()),
marketing: Some(true),
})
.await?;
dbg!(record);
Ok(())
}
Creates one or more records in the database.
Method Syntax
db.create(resource).content(data)
Arguments
| Argument | Type | Description |
|---|
resource | The table name or the specific record ID to create. |
data | The document / record data to insert. |
Example usage
use serde::{Deserialize, Serialize};
use surrealdb::engine::any::connect;
use surrealdb::opt::auth::Root;
use surrealdb::RecordId;
#[derive(Debug, Serialize, Deserialize)]
struct Person {
name: Option<String>,
marketing: Option<bool>
}
#[derive(Debug, Deserialize)]
struct Record {
id: RecordId,
}
#[tokio::main]
async fn main() -> surrealdb::Result<()> {
let db = connect("ws://localhost:8000").await?;
db.signin(Root {
username: "root",
password: "secret",
})
.await?;
db.use_ns("ns").use_db("db").await?;
let person: Option<Person> = db.create("person").await?;
dbg!(person);
let record: Option<Record> = db
.create(("person", "tobie"))
.content(Person {
name: Some("Tobie".into()),
marketing: Some(true),
})
.await?;
dbg!(record);
Ok(())
}
Translated query
This function will run the following query in the database:
CREATE $resource CONTENT $data;
See also