4 releases

0.1.2 Nov 10, 2022
0.1.1 Nov 10, 2022
0.1.0 Nov 10, 2022
0.0.1 Nov 10, 2022

#5 in #type-db

Apache-2.0 and AGPL-3.0-or-later

110KB
1.5K SLoC

TypeDB Client for Rust (under development)

Discord Discussion Forum Stack Overflow Stack Overflow

Project Status

This is a work in progress and is not yet suitable for production usage.

It can connect to TypeDB, run read and write queries, and return answers. Concept API methods are not available yet.

Client Architecture

To learn about the mechanism that a TypeDB Client uses to set up communication with databases running on the TypeDB Server, refer to TypeDB > Client API > Overview.

The TypeDB Client for Rust provides a fully async API that supports the tokio multi-threaded runtime.

Quickstart

  1. Import typedb-client through Cargo:
typedb-client = "0.1.2"
  1. Make sure the TypeDB Server is running.
  2. Import typedb_client::TypeDBClient, instantiate a TypeDB Core client, open a session to a database, and run basic insertion and retrieval queries:
use typedb_client::concept::{Concept, Thing};
use typedb_client::session::Type::Data;
use typedb_client::transaction::Type::{Read, Write};
use typedb_client::TypeDBClient;
let mut client = TypeDBClient::new("http://0.0.0.0:1729").await?;
let session = client.session("social_network", Data).await?;
{
    // Transactions (and sessions) get closed on drop, or can be manually closed by calling close()
    let mut tx = session.transaction(Write).await?;
    tx.query.insert("insert $x isa person, has email \"x@email.com\";");
    // To persist changes, a write transaction must always be committed. This also closes the transaction.
    tx.commit().await?;
}
{
    let mut tx = session.transaction(Read).await?;
    let mut answer_stream = tx.query.match_("match $p isa person, has email $e; limit 10;");
    while let Some(result) = answer_stream.next().await {
        match result {
            Ok(answer) => {
                match answer.get("e") {
                    // The Concept type hierarchy is represented by the Concept enum
                    Concept::Thing(Thing::Attribute(Attribute::String(value))) => { println!("email: {}", value); }
                    _ => { panic!(); }
                }
            }
            Err(err) => panic!("An error occurred fetching answers of a Match query: {}", err)
        }
    }
}

Examples

More code examples can be found in tests/queries.rs.

Build from Source

Note: You don't need to compile TypeDB Client from source if you just want to use it in your code. See the "Quickstart" section above.

  1. Make sure you have Bazel installed on your machine.

  2. Build the library:

    a) to build the native/raw rlib:

    bazel build //:typedb_client
    

    The rlib will be produced at: bazel-bin/libtypedb_client-{hash}.rlib.

    b) to build the crate for a Cargo project:

    bazel build //:assemble_crate
    

    The Cargo crate will be produced at:

    bazel-bin/assemble_crate.crate
    

    You can then unzip this crate to retrieve Cargo.toml. Please note: this process has not yet been thoroughly tested. The generated Cargo.toml may not be fully correct. See the Cargo.toml of the typedb-client crate for reference.

Dependencies

~12–24MB
~318K SLoC