Connects to a local or remote database endpoint.
Method Syntax
Surreal::new::<T>(address)
Arguments
| Argument | Description |
|---|
endpoint | The database endpoint to connect to. |
Example usage
Basic example
use surrealdb::engine::remote::ws::Ws;
use surrealdb::Surreal;
#[tokio::main]
async fn main() -> surrealdb::Result<()> {
let db = Surreal::new::<Ws>("127.0.0.1:8000").await?;
Ok(())
}
Configuring the database
The new() function takes an argument of impl IntoEndpoint, which is implemented not only for strings and string-like structs like PathBuf and SocketAddr, but also a tuple of one of these types for the address along with a second Config struct for the configuration.
Example with all capabilities enabled except one function
use surrealdb::{Error, engine::any::connect, opt::{Config, capabilities::Capabilities}};
#[tokio::main]
async fn main() -> Result<(), Error> {
let mut capabilities = Capabilities::all();
capabilities.deny_function("math::abs").unwrap();
let config = Config::default()
.capabilities(capabilities);
let db = connect(("mem://", config)).await?;
db.use_ns("main").use_db("main").await?;
println!("{:?}", db.query("math::abs(-10)").await?);
Ok(())
}
Using a backend with versioning
To make a new connection that includes SurrealKV versioning, add the “kv-surrealkv” feature flag to the surrealdb dependency in Cargo.toml, add the path to the folder containing the database inside new(), and call the .versioned() method. Versioning is also available with the memory backend.
use surrealdb::{Surreal, engine::local::{Mem, SurrealKv}};
#[tokio::main]
async fn main() -> surrealdb::Result<()> {
let db = Surreal::new::<SurrealKv>("path/to/database-folder")
.versioned()
.await?;
let mem_db = Surreal::new::<Mem>(())
.versioned()
.await?;
Ok(())
}
See also
Connects to a local or remote database endpoint.
Method Syntax
Surreal::new::<T>(address)
Arguments
| Argument | Description |
|---|
endpoint | The database endpoint to connect to. |
Example usage
Basic example
use surrealdb::engine::remote::ws::Ws;
use surrealdb::Surreal;
#[tokio::main]
async fn main() -> surrealdb::Result<()> {
let db = Surreal::new::<Ws>("127.0.0.1:8000").await?;
Ok(())
}
Configuring the database
The new() function takes an argument of impl IntoEndpoint, which is implemented not only for strings and string-like structs like PathBuf and SocketAddr, but also a tuple of one of these types for the address along with a second Config struct for the configuration.
Example with all capabilities enabled except one function
#[tokio::main]
async fn main() -> Result<(), Error> {
let config = Config::default()
.capabilities(Capabilities::all().with_deny_function("math::abs")?);
let db = connect(("mem://", config)).await?;
db.use_ns("ns").use_db("db").await?;
println!("{:?}", db.query("math::abs(-10)").await?);
println!("{:?}", db.run::<i32>("math::abs").args(-10).await);
Ok(())
}
Using SurrealKV with versioning
To make a new connection that includes SurrealKV versioning, add the “surreal-kv” feature flag to the surrealdb dependency in Cargo.toml, add the path to the folder containing the database inside new(), and call the .versioned() method.
use surrealdb::engine::local::SurrealKv;
use surrealdb::Surreal;
#[tokio::main]
async fn main() -> surrealdb::Result<()> {
let db = Surreal::new::<SurrealKv>("path/to/database-folder").versioned().await?;
Ok(())
}
See also