Crates.io | typedb-client |
lib.rs | typedb-client |
version | 0.1.2 |
source | src |
created_at | 2022-11-10 12:19:18.139052 |
updated_at | 2022-11-10 14:00:55.068859 |
description | TypeDB Client API for Rust |
homepage | https://github.com/vaticle/typedb-client-rust |
repository | https://github.com/vaticle/typedb-client-rust |
max_upload_size | |
id | 711079 |
size | 136,244 |
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.
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.
typedb-client
through Cargo:typedb-client = "0.1.2"
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)
}
}
}
More code examples can be found in tests/queries.rs
.
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.
Make sure you have Bazel installed on your machine.
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.