connect()

Connects to a local or remote database endpoint.

Method Syntax

db.connect(address)

Arguments

ArgumentDescription
endpoint The database endpoint to connect to.

Example usage

The .connect() method will usually take a String or a type that implements Into<String>. Note that the final .connect() with a Config is possible because of the implementation impl<T> IntoEndpoint for (T, Config) where T: Into<String>.

use std::sync::LazyLock;
use std::time::Duration;
use surrealdb::engine::remote::ws::{Client, Ws, Wss};
use surrealdb::opt::Config;
use surrealdb::Surreal;

static DB: LazyLock<Surreal<Client>> = LazyLock::new(Surreal::init);

#[tokio::main]
async fn main() -> surrealdb::Result<()> {
// Connect to a local endpoint
DB.connect::<Ws>("127.0.0.1:8000").await?;
// Connect to a remote endpoint
DB.connect::<Wss>("cloud.surrealdb.com").await?;
// A tuple with a Config struct can also be passed in for fine tuning of the connection
let config = Config::default().query_timeout(Duration::from_millis(1500));
DB.connect::<Ws>(("127.0.0.1:8000", config)).await?;
Ok(())
}

See also