connect()
Connects to a local or remote database endpoint.
Method Syntax
db.connect(address)
Arguments
| Argument | Description |
|---|
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<()> {
DB.connect::<Ws>("127.0.0.1:8000").await?;
DB.connect::<Wss>("cloud.surrealdb.com").await?;
let config = Config::default().query_timeout(Duration::from_millis(1500));
DB.connect::<Ws>(("127.0.0.1:8000", config)).await?;
Ok(())
}
See also