Selects all records in a table, or a specific record, from the database.
Method Syntax
db.select(resource)
Arguments
| Argument | Description |
|---|
resource | The table name or a record ID to select. |
Example usage
let people: Vec<Person> = db.select("person").await?;
let person: Option<Person> = db.select(("person", "h5wxrf2ewk8xjxosxtyc")).await?;
Example usage: Retrieve unique id of a record
use surrealdb::Surreal;
use surrealdb::engine::remote::ws::Ws;
use surrealdb::opt::auth::Root;
use surrealdb_types::{RecordId, SurrealValue};
#[derive(Debug, SurrealValue)]
struct Person {
id: RecordId,
name: String,
age: u8,
}
#[tokio::main]
async fn main() -> surrealdb::Result<()> {
let db = Surreal::new::<Ws>("localhost:8000").await?;
db.signin(Root {
username: "root".to_string(),
password: "secret".to_string(),
})
.await?;
db.use_ns("main").use_db("main").await?;
db.query("CREATE person:john SET name = 'John Doe', age = 25")
.await?
.check()?;
let john: Option<Person> = db.select(("person", "john")).await?;
dbg!(john);
Ok(())
}
Translated query
This function will run the following query in the database:
SELECT * FROM $resource;
See also
Selects all records in a table, or a specific record, from the database.
Method Syntax
db.select(resource)
Arguments
| Argument | Description |
|---|
resource | The table name or a record ID to select. |
Example usage
let people: Vec<Person> = db.select("person").await?;
let person: Option<Person> = db.select(("person", "h5wxrf2ewk8xjxosxtyc")).await?;
Example usage: Retrieve unique id of a record
use serde::Deserialize;
use surrealdb::engine::remote::ws::Ws;
use surrealdb::opt::auth::Root;
use surrealdb::RecordId;
use surrealdb::Surreal;
#[derive(Debug, Deserialize)]
struct Person {
id: RecordId,
name: String,
age: u8,
}
#[tokio::main]
async fn main() -> surrealdb::Result<()> {
let db = Surreal::new::<Ws>("localhost:8000").await?;
db.signin(Root {
username: "root",
password: "secret",
})
.await?;
db.use_ns("namespace").use_db("database").await?;
db.query("CREATE person:john SET name = 'John Doe', age = 25").await?.check()?;
let john: Option<Person> = db.select(("person", "john")).await?;
dbg!(john);
Ok(())
}
Translated query
This function will run the following query in the database:
SELECT * FROM $resource;
See also