2

I am trying to figure out how to connect my Rust app to a surrealdb database that is running inside a docker container.

The docs on the SurrealDB website only specify three ways to connect to the database, these are: memory, file, and tikv.

I am running surrealdb on docker as indicated on their website:

docker run --rm -p 8000:8000 surrealdb/surrealdb:latest start

I tried doing something like the following:

let ds = Datastore::new("http://0.0.0.0:8000").await?;

But I am getting the following error:

value: Ds("Unable to load the specified datastore")'

Perhaps it has not been implemented yet?

Gixty
  • 202
  • 4
  • 13

3 Answers3

2

There is a new rust library for handling connections to surrealdb. This is built and maintained by the same authors of SurrealDB.

https://github.com/surrealdb/surrealdb.rs

Quote from the repo:

This library enables simple and advanced querying of a remote database from server-side and client-side (via Wasm) code. By default, all connections to SurrealDB are made over WebSockets, and automatically reconnect when the connection is terminated. Connections are automatically closed when they get dropped.

Gixty
  • 202
  • 4
  • 13
1

According to the IANA 0.0.0.0 may only serve as a source address, thus your code cannot be correct. If the container is running on the same host, you may need to use localhost (127.0.0.1).

Update

Until recently, it was not possible to connect to a remote SurrealDB instance from Rust, only run an embedded one. Now there is a client library for rust. You can connect to your database over WebSockets like this:

use surrealdb_rs::{Surreal, protocol::Ws};

let client = Surreal::connect::<Ws>("localhost:8000").await?;
F0X
  • 370
  • 4
  • 13
  • 1
    While checking the Rust docs for surrealdb, it seems `http` is not allowed. As of now, I believe `tikv` is the only way. – Gixty Nov 15 '22 at 20:41
  • @Gixty yep it looks like you are right, I didn't notice before. As far as i can tell the `surrealdb` crate cannot connect to a remote Surreal instance, only run an embedded one (which may use a remote TiKV datastore) – F0X Nov 16 '22 at 22:18
  • As of today, from the [docs](https://docs.rs/surrealdb/1.0.0-beta.8/surrealdb/index.html) for 1.0.0-beta.8: "This library can be used to start an embedded in-memory datastore, an embedded datastore persisted to disk, a browser-based embedded datastore backed by IndexedDB, or for connecting to a distributed TiKV key-value store." :( I'm considering the HTTP&Rest integration, or sticking with mongodb – Martin Schaer Nov 17 '22 at 14:21
  • 1
    It is now possible to connect through websockets and to a docker container https://github.com/surrealdb/surrealdb/discussions/1468 – Gixty Nov 19 '22 at 16:08
  • Wow perfect timing, I edited my answer to include the new client library @Gixty – F0X Nov 20 '22 at 16:20
1

You are using the wrong library! The crate surrealdb can only be used to start a database instance of SurrealDB. But it is not a client library.

The official client library crate is called surrealdb-rs.

You will be able to add this to your project with:

(⚠️ The crate is not published yet on crates.io. You can not download it with cargo add yet. ⚠️)

cargo add surrealdb-rs

I would also recommend setting a password for the root user when you start the database. Otherwise, you may run into authentication issues.

docker run --rm -p 8000:8000 surrealdb/surrealdb:latest start --pass root

You would now connect to your running database with Surreal::connect().

use surrealdb_rs::{Result, Surreal};
use surrealdb_rs::param::Root;
use surrealdb_rs::protocol::Ws;

#[tokio::main]
async fn main() -> Result<()> {
    let client = Surreal::connect::<Ws>("localhost:8000").await?;

    // Signin as a namespace, database, or root user
    client.signin(Root {
        username: "root",
        password: "root",
    }).await?;
}

Until the official library is available on crates.io, you may look into surreal-simple-client. But I would recommend to wait for the official library.

Tobias S.
  • 21,159
  • 4
  • 27
  • 45
  • 1
    ok, So I would use `surrealdb-rs` crate instead of `surrealdb`, even if I decide not use a websocket connection, right? I am only doing backend stuff at the moment, the apps would communicate via graphql. I managed to work with the `surrealdb` crate and tikv as the store, working great so far. Amazing project by the way, I have very high hopes for it. Keep it up Tobias and co! – Gixty Nov 20 '22 at 01:44
  • 1
    @Gixty - you can choose the protocol when using `surrealdb-rs`. In the code snippet I choose `WS`, but http can be used too. Your current approach might work fine for the moment, but be aware that you are running a full surrealdb instance in each client. Maybe you want to have more than one client in the future then you would have multiple surrealdb instances running at the same time. – Tobias S. Nov 20 '22 at 01:51
  • I see that in `surrealdb-rs` we have static clients so no need for a `lazy_static` approach; however, if I use `lazy_static` or `one_cell` in `surrealdb` wouldn't that help me in regards with having only one surrealdb instance instead of multiple? I know this is out of scope from the main question, sorry! – Gixty Nov 20 '22 at 05:40
  • Update! you can now use `surrealdb` crate as client: https://surrealdb.com/docs/integration/sdks/rust – eminfedar Jul 19 '23 at 10:09