• Start

Languages

/

Rust

/

Methods

create

The .create() method for the SurrealDB Rust SDK creates one or more records in the database.

Creates one or more records in the database.

Method Syntax

db.create(resource).content(data)
ArgumentTypeDescription
resource The table name or the specific record ID to create.
data The document / record data to insert.
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?;

// Create a record with a random ID
let person: Option<Person> = db.create("person").await?;
dbg!(person);
// Create a record with a specific ID
let record: Option<Record> = db
.create(("person", "tobie"))
.content(Person {
name: Some("Tobie".into()),
marketing: Some(true),
})
.await?;
dbg!(record);
Ok(())
}

This function will run the following query in the database:

CREATE $resource CONTENT $data;

Was this page helpful?