2 个版本
0.1.2 | 2023年1月14日 |
---|---|
0.1.1 |
|
0.1.0 | 2021年5月12日 |
#2421 在 数据库接口
562 每月下载次数
350KB
7.5K SLoC
ruarango
lib.rs
:
ruarango
为 ArangoDB
数据库编写的 Rust 数据库驱动程序。
虽然 ruarango
的 API 是异步的,但 ArangoDB
支持 3 种操作模式。它们是阻塞、存储和立即忘记。后两种模式是异步的,而第一种是同步的。更多详细信息请参阅此处。以下是一些使用驱动程序在这些不同模式下的示例。
同步阻塞连接
使用 Blocking
模式下的驱动程序
// Use `ConnectionBuilder` to build a connection and pull in the
// traits for operations you wish to use
use ruarango::{ConnectionBuilder, Database};
#
// Setup a synchronous connection to the database
let conn = ConnectionBuilder::default()
.url(url) // The normal url for ArangoDB running locally is https://127.0.0.1:8529
.username("root")
.password("")
.database("test_db")
.build()
.await?;
// Use the connection to query information about the current database
let res = conn.current().await?;
// Get the sync results out of the right side of the `Either`
assert!(res.is_right());
let contents = res.right_safe()?;
assert!(!contents.error());
assert_eq!(*contents.code(), 200);
assert_eq!(contents.result().name(), "test");
assert_eq!(contents.result().id(), "123");
assert!(!contents.result().is_system());
异步存储连接
使用 Store
模式下的驱动程序
// Use `ConnectionBuilder` to build a connection and pull in the
// traits for operations you wish to use
use ruarango::{ConnectionBuilder, Database, Job};
#
// Setup a asynchronous store connection to the database
let conn = ConnectionBuilder::default()
.url(url) // The normal url for ArangoDB running locally is https://127.0.0.1:8529
.username("root")
.password("")
.database("test_db")
.async_kind(AsyncKind::Store)
.build()
.await?;
// Use the connection to spawn a job for information about the current database
// This will return immediately with a 202 code and job information if the job
// was accepted into the queue.
let res = conn.current().await?;
// Get the async job info results out of the left side of the `Either`
assert!(res.is_left());
let contents = res.left_safe()?;
assert_eq!(*contents.code(), 202);
assert!(contents.id().is_some());
let job_id = contents.id().as_ref().ok_or_else(|| anyhow!("invalid job_id"))?;
assert_eq!(job_id, "123456");
// Check status until we get 200 (or error out on 404)
let mut status = conn.status(job_id).await?;
assert!(status == 200 || status == 204);
while status != 200 {
std::thread::sleep(std::time::Duration::from_millis(500));
status = conn.status(job_id).await?;
}
// Fetch the results (this has the side effect of removing the job off of the server)
let res: Response<Current> = conn.fetch(job_id).await?;
assert!(!res.error());
assert_eq!(*res.code(), 200);
assert_eq!(res.result().name(), "test");
assert_eq!(res.result().id(), "123");
assert!(!res.result().is_system());
异步立即忘记连接
使用 Fire & Forget
模式下的驱动程序
// Use `ConnectionBuilder` to build a connection and pull in the
// traits for operations you wish to use
use ruarango::{ConnectionBuilder, Database, Job};
#
// Setup a asynchronous store connection to the database
let conn = ConnectionBuilder::default()
.url(url) // The normal url for ArangoDB running locally is https://127.0.0.1:8529
.username("root")
.password("")
.database("test_db")
.async_kind(AsyncKind::FireAndForget)
.build()
.await?;
// Use the connection to spawn a job for information about the current database
// In this case, fire and forget isn't useful, but for other operations it
// may be. Fire and Forget jobs run on the server and do not store results.
let res = conn.current().await?;
// Check that the job was accepted into the queue.
assert!(res.is_left());
let contents = res.left_safe()?;
assert_eq!(*contents.code(), 202);
assert!(contents.id().is_none());
依赖项
~12–29MB
~409K SLoC