4 个版本
| 0.1.2 | 2022 年 11 月 10 日 | 
|---|---|
| 0.1.1 | 2022 年 11 月 10 日 | 
| 0.1.0 | 2022 年 11 月 10 日 | 
| 0.0.1 | 2022 年 11 月 10 日 | 
#5 in #type-db
110KB
 1.5K  SLoC
Rust 的 TypeDB 客户端(开发中)
项目状态
这是一个 正在进行中 的项目,目前不适合用于生产。
它可以连接到 TypeDB,运行读取和写入查询,并返回答案。概念 API 方法目前不可用。
客户端架构
要了解 TypeDB 客户端用于与在 TypeDB 服务器上运行的数据库建立通信的机制,请参阅 TypeDB > 客户端 API > 概述。
Rust 的 TypeDB 客户端提供了一个完全异步的 API,支持 tokio 的 多线程 运行时。
快速入门
- 通过 Cargo 导入 typedb-client
typedb-client = "0.1.2"
- 确保 TypeDB 服务器 正在运行。
- 导入 typedb_client::TypeDBClient,实例化一个 TypeDB 核心客户端,打开到 数据库 的会话,并运行基本的插入和检索查询
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)
        }
    }
}
示例
更多代码示例可以在 tests/queries.rs 中找到。
从源码构建
注意:如果您只想在代码中使用 TypeDB 客户端,不需要从源码编译。请参阅上面的 "快速入门" 部分。
- 
确保您的机器上已安装 Bazel。 
- 
构建库 a) 构建原生/原始 rlib bazel build //:typedb_clientrlib 将生成在: bazel-bin/libtypedb_client-{hash}.rlib。b) 为 Cargo 项目构建包 bazel build //:assemble_crate包将生成在 bazel-bin/assemble_crate.crate然后您可以解压此包以检索 Cargo.toml。 请注意:此过程尚未经过彻底测试。生成的Cargo.toml可能不完整。请参阅typedb-client包的Cargo.toml作为参考。
依赖关系
~12–22MB
~311K SLoC