surrealdb / surrealdb.rs

SurrealDB driver for Rust
https://surrealdb.com
Apache License 2.0
64 stars 7 forks source link

Add support for embedding the server #10

Closed rushmorem closed 1 year ago

rushmorem commented 1 year ago

What is the motivation?

See https://discord.com/channels/902568124350599239/1014970959461105664/1044110778527141948 and the ensuing discussion.

What does this change do?

It adds support for embedding the server in the client itself. This makes switching between embedded and remote instances easy.

What is your testing strategy?

Is this related to any issues?

No

Oyelowo commented 1 year ago

Great job @rushmorem 💯

I tried to build the client from your branch but getting cylic dependency error. Have you run into similar issue? 🤔

 cargo check                                                                                 
    Updating crates.io index
    Updating git repository `https://github.com/rushmorem/surrealdb.rs`
    Updating git repository `https://github.com/rushmorem/surrealdb`
error: cyclic package dependency: package `getrandom v0.2.8` depends on itself. Cycle:
package `getrandom v0.2.8`
    ... which satisfies dependency `getrandom = "^0.2.3"` of package `ahash v0.7.6`
    ... which satisfies dependency `ahash = "^0.7.0"` of package `hashbrown v0.12.3`
    ... which satisfies dependency `hashbrown = "^0.12"` of package `indexmap v1.9.2`
    ... which satisfies dependency `indexmap = "^1.5.2"` of package `serde_json v1.0.89`
    ... which satisfies dependency `serde_json = "^1.0"` of package `wasm-bindgen v0.2.83`
    ... which satisfies dependency `wasm-bindgen = "^0.2.83"` of package `js-sys v0.3.60`
    ... which satisfies dependency `js-sys = "^0.3"` of package `getrandom v0.2.8`
    ... which satisfies dependency `getrandom = "^0.2"` of package `rand_core v0.6.4`
    ... which satisfies dependency `rand_core = "^0.6"` of package `crypto-common v0.1.6`
    ... which satisfies dependency `crypto-common = "^0.1.4"` of package `aead v0.5.1`

The packages

surrealdb = "1.0.0-beta.8"
surrealdb-rs = { git = "https://github.com/rushmorem/surrealdb.rs", branch = "embedded-db" }
rushmorem commented 1 year ago

Thanks @Oyelowo. I haven't run into this. Can you try

surrealdb = { git = "https://github.com/rushmorem/surrealdb", branch = "suppress-warnings" }

instead of surrealdb = "1.0.0-beta.8"? Currently this library uses that fork instead of the crates.io package so cargo treats them as different packages since they are from different sources.

Oyelowo commented 1 year ago

Awesome! @rushmorem

Both now work! i.e the main and your fork Thanks for the quick fix! 👍

rushmorem commented 1 year ago

Awesome! @rushmorem

Both now work! i.e the main and your fork Thanks for the quick fix! +1

I'm glad to hear that but that rebase was just to update the fork against main.

Oyelowo commented 1 year ago

@rushmorem Running into the same issue again 🤔

rushmorem commented 1 year ago

@rushmorem Running into the same issue again thinking

I'm sorry to hear that @Oyelowo. Are you able to post your Cargo.toml and Cargo.lock?

Oyelowo commented 1 year ago

No worries @rushmorem. I have attached both files. Had to use .txt to make it work with GitHub.

Cargo.lock.txt Cargo.toml.txt

rushmorem commented 1 year ago

Cargo.lock.txt Cargo.toml.txt

Thanks! This is not directly introduced by surrealdb.rs. For example, running the examples included by this PR does not trigger this. It's how the extra dependencies you have are interacting with those from surrealdb.rs. In your case, in particular, enabling surrealdb/kv-indxdb by default on WASM is what's triggering this even when you are not targeting WASM. I will sort this out right away.

rushmorem commented 1 year ago

@Oyelowo This should be fixed now. surrealdb/kv-indxdb is no longer enabled by default on WASM.

Oyelowo commented 1 year ago

@rushmorem This works now! Thanks for taking care of it 💯

rushmorem commented 1 year ago

@rushmorem This works now! Thanks for taking care of it 100

Awesome! Glad to hear that.

Oyelowo commented 1 year ago

Hi there!

I tried running your query with an in-memory database, but I'm getting an error. It looks like the query is returning a hashMap instead of a vector. Is this expected behavior? Here is my code for reference:

let users: Vec<User> = db.select("user").await?;

use serde::Deserialize;
use surrealdb_rs::storage::Mem;
use surrealdb_rs::Surreal;

#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct User {
    id: String,
    name: String,
    company: String,
}

#[tokio::main]
async fn main() -> surrealdb_rs::Result<()> {
    tracing_subscriber::fmt::init();

    let db = Surreal::connect::<Mem>(()).await?;

    db.use_ns("namespace").use_db("database").await?;

    #[rustfmt::skip]
    let results = db
        .query("
            CREATE user
            SET name = $name,
                company = $company
        ")
        .bind("name", "John Doe")
        .bind("company", "ACME Corporation")
        .await?;

    // print the created user:
    let user: Option<User> = results.get(0, 0)?;
    tracing::info!("{user:?}");

    // print all users:
    let users: Vec<User> = db.select("user").await?; // The line with the issue
    tracing::info!("{users:?}");

    Ok(())
}

The error I get is:

Error: Error { kind: Deserialization, message: "invalid type: map, expected a sequence" }

Let me know if you need more information or if there's anything I can do to help. Thanks!

rushmorem commented 1 year ago
Error { kind: Deserialization, message: "invalid type: map, expected a sequence" }

Fixed. Thank you for the detailed report and for testing this PR.

Oyelowo commented 1 year ago
Error { kind: Deserialization, message: "invalid type: map, expected a sequence" }

Fixed. Thank you for the detailed report and for testing this PR.

@rushmorem Wonderful! Works now! Thanks for taking care of it, you're doing a great job 💯

rushmorem commented 1 year ago

@rushmorem Wonderful! Works now! Thanks for taking care of it, you're doing a great job 100

Thank you.

prabirshrestha commented 1 year ago

Was hoping to use embedded server with surrealdb.rs. Curious if there were any reason for closing it. Are there plans to support this in the near future?

huang12zheng commented 1 year ago

https://github.com/surrealdb/surrealdb/pull/1514 @prabirshrestha Maybe you need

prabirshrestha commented 1 year ago

Awesome. That is even better.