Crates.io | arangors_lite |
lib.rs | arangors_lite |
version | 0.2.0 |
source | src |
created_at | 2021-10-21 09:28:12.021297 |
updated_at | 2022-05-24 13:34:56.382719 |
description | Rust driver for ArangoDB |
homepage | |
repository | https://github.com/ManevilleF/arangors |
max_upload_size | |
id | 468388 |
size | 243,253 |
arangors_lite
is a fork of arangors by fMeow.
arangors
is an intuitive rust client for ArangoDB,
inspired by pyArango.
arangors
enables you to connect with ArangoDB server, access to database,
execute AQL query, manage ArangoDB in an easy and intuitive way,
both async
and plain synchronous code with any HTTP ecosystem you love.
arangors
is targeted at ergonomic, intuitive and OOP-like API for
ArangoDB, both top level and low level API for users' choice.
Overall architecture of ArangoDB:
databases -> collections -> documents/edges
In fact, the design of arangors
just mimic this architecture, with a
slight difference that in the top level, there is a connection object on top
of databases, containing a HTTP client with authentication information in
HTTP headers.
Hierarchy of arangors:
connection -> databases -> collections -> documents/edges
By now, the available features of arangors are:
async
and sync[dependencies]
## This one is async
arangors_lite = { version = "0.2" }
## This one is synchronous
arangors_lite = { version = "0.2", features = ["blocking"] }
Thanks to maybe_async
, arangors
can unify sync and async API and toggle
with a feature gate. Arangors adopts async first policy.
By default reqwest
uses OpenSSL. To use rustls
you may disable default features and use the rustls
feature:
[dependencies]
## This one uses openssl
arangors_lite = { version = "0.2" }
## This one rustls
arangors_lite = { version = "0.2", features = ["rustls"], default-features = false }
There is three way to establish connections:
So are the arangors
API.
Example:
use arangors_lite::Connection;
// (Recommended) Handy functions
let conn = Connection::establish_jwt("http://localhost:8529", "username", "password")
.await
.unwrap();
let conn = Connection::establish_basic_auth("http://localhost:8529", "username", "password")
.await
.unwrap();
let conn = Connection::establish_without_auth("http://localhost:8529").await.unwrap();
use arangors_lite::Connection;
let db = conn.db("test_db").await.unwrap();
let collection = db.collection("test_collection").await.unwrap();
All AQL query related functions are associated with database, as AQL query is performed at database level.
There are several way to execute AQL query, and can be categorized into two classes:
batch query with cursor
aql_query_batch
aql_next_batch
query to fetch all results
aql_str
aql_bind_vars
aql_query
This later ones provide a convenient high level API, whereas batch queries offer more control.
Note that results from ArangoDB server, e.x. fetched documents, can be
strong typed given deserializable struct, or arbitrary JSON object with
serde::Value
.
#[derive(Deserialize, Debug)]
struct User {
pub username: String,
pub password: String,
}
// Typed
let resp: Vec<User> = db
.aql_str("FOR u IN test_collection RETURN u")
.await
.unwrap();
// Not typed: Arbitrary JSON objects
let resp: Vec<serde_json::Value> = db
.aql_str("FOR u IN test_collection RETURN u")
.await
.unwrap();
arangors
offers a way to manually handle batch query.
Use aql_query_batch
to get a cursor, and use aql_next_batch
to fetch
next batch and update cursor with the cursor.
let aql = AqlQuery::new("FOR u IN @@collection LIMIT 3 RETURN u")
.bind_var("@collection", "test_collection")
.batch_size(1)
.count(true);
// fetch the first cursor
let mut cursor = db.aql_query_batch(aql).await.unwrap();
// see metadata in cursor
println!("count: {:?}", cursor.count);
println!("cached: {}", cursor.cached);
let mut results: Vec<serde_json::Value> = Vec::new();
loop {
if cursor.more {
let id = cursor.id.unwrap().clone();
// save data
results.extend(cursor.result.into_iter());
// update cursor
cursor = db.aql_next_batch(id.as_str()).await.unwrap();
} else {
break;
}
}
println!("{:?}", results);
There are three functions for AQL query that fetch all results from ArangoDB. These functions internally fetch batch results one after another to get all results.
The functions for fetching all results are listed as bellow:
aql_str
This function only accept a AQL query string.
Here is an example of strong typed query result with aql_str
:
#[derive(Deserialize, Debug)]
struct User {
pub username: String,
pub password: String,
}
let result: Vec<User> = db
.aql_str(r#"FOR i in test_collection FILTER i.username=="test2" return i"#)
.await
.unwrap();
aql_bind_vars
This function can be used to start a AQL query with bind variables.
use arangors_lite::{Connection, Document};
#[derive(Serialize, Deserialize, Debug)]
struct User {
pub username: String,
pub password: String,
}
let mut vars = HashMap::new();
let user = User {
username: "test".to_string(),
password: "test_pwd".to_string(),
};
vars.insert("user", serde_json::value::to_value(&user).unwrap());
let result: Vec<Document<User>> = db
.aql_bind_vars(r#"FOR i in test_collection FILTER i==@user return i"#, vars)
.await
.unwrap();
aql_query
This function offers all the options available to tweak a AQL query.
Users have to construct a AqlQuery
object first. And AqlQuery
offer all
the options needed to tweak AQL query. You can set batch size, add bind
vars, limit memory, and all others
options available.
use arangors_lite::{AqlQuery, Connection, Cursor, Database};
use serde_json::value::Value;
let aql = AqlQuery::new("FOR u IN @@collection LIMIT 3 RETURN u")
.bind_var("@collection", "test_collection")
.batch_size(1)
.count(true);
let resp: Vec<Value> = db.aql_query(aql).await.unwrap();
println!("{:?}", resp);
Contributions and feed back are welcome following Github workflow.
arangors
is provided under the MIT license. See LICENSE.
An ergonomic ArangoDB client for rust.