😽

ArangoDBにarangors_liteでアクセスする

2022/06/17に公開

Rustまじなんもわからん…

サンプルデータ

https://github.com/arangodb/example-datasets
にある
https://github.com/arangodb/example-datasets/blob/master/RandomUsers/names_100.json

Rustプロジェクト作成

cargo new arango-rust-test

各ファイル

Cargo.toml

[package]
name = "arango-rust-test"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1"
arangors_lite = { version = "0.2" }
serde_json = "1"
tokio = { version = "1", features = ["full"] }
use anyhow::Error;

use arangors_lite::{AqlQuery, Connection};

use serde_json::value::Value;

async fn wk() -> Result<(), Error> {
    // (Recommended) Handy functions
    let conn = Connection::establish_jwt("http://localhost:8529", "ユーザーID", "パスワード")
    .await
    .unwrap();

    let database = conn.db("test").await.unwrap();
    let aql = AqlQuery::new("FOR u IN User LIMIT 3 RETURN u");
    println!("{:?}", aql);
    println!("{:?}", serde_json::to_string(&aql).unwrap());

    let resp: Vec<Value> = database.aql_query(aql).await.unwrap();
    println!("{:?}", resp);
    Ok(())
}

#[tokio::main]
async fn main() {
    println!("Hello, world!");
    let ret:Result<(), Error> = wk()
    .await;

    println!("{:?}", ret);
}

ビルド

cargo build

実行

.\target\debug\arango-rust-test.exe

結果

AqlQuery { query: "FOR u IN User LIMIT 3 RETURN u", bind_vars: {}, count: None, batch_size: None, cache: None, memory_limit: None, ttl: None, options: None }
"{\"query\":\"FOR u IN User LIMIT 3 RETURN u\"}"
[Object({"_id": String("User/1839"), "_key": String("1839"), "_rev": String("_eVBWryS---"), "birthday": String("1950-08-06"), "contact": Object({"address": Object({"city": String("Bartelso"), "state": String("IL"), "street": String("10 17th St"), "zip": String("62218")}), "email": Array([String("shawna.matheney@nosql-matters.org"), String("shawna@nosql-matters.org")]), "phone": Array([String("618-9063984"), String("618-8108742")]), "region": String("618")}), "gender": String("female"), "likes": Array([String("driving"), String("skiing")]), "memberSince": String("2010-07-07"), "name": Object({"first": String("Shawna"), "last": String("Matheney")})}), Object({"_id": String("User/1840"), "_key": String("1840"), "_rev": String("_eVBWryS--_"), "birthday": String("1941-05-16"), "contact": Object({"address": Object({"city": String("Rochester"), "state": String("NY"), "street": String("3 Spring garden Aly"), "zip": String("14608")}), "email": Array([String("burton.siaperas@nosql-matters.org"), String("siaperas@nosql-matters.org")]), "phone": Array([String("716-5428351")]), "region": String("716")}), "gender": String("male"), "likes": Array([String("shopping")]), "memberSince": String("2009-02-02"), "name": Object({"first": String("Burton"), "last": String("Siaperas")})}), Object({"_id": String("User/1841"), "_key": String("1841"), "_rev": String("_eVBWryS--A"), "birthday": String("1955-07-25"), "contact": Object({"address": Object({"city": String("Warren"), "state": String("OH"), "street": String("9 Neumann Pl"), "zip": String("44482")}), "email": Array([String("ernie.levinson@nosql-matters.org")]), "phone": Array([String("330-2486637")]), "region": String("330")}), "gender": String("male"), "likes": Array([]), "memberSince": String("2008-08-05"), "name": Object({"first": String("Ernie"), "last": String("Levinson")})})]
Ok(())

Discussion