• Start

Languages

/

Rust

/

Methods

connect

The .connect() method for the SurrealDB Rust SDK connects to a local or remote database endpoint.

Connects to a local or remote database endpoint.

Method Syntax
db.connect(address)

Argument

Description

endpoint

The database endpoint to connect to.

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

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(())
}

Was this page helpful?