Commit ea5a99b5 authored by Youp's avatar Youp
Browse files

- Created a basic setup for the importing of notes

- Included an example showing how to serialize and deserialize with Serde and DGraph
parent 43752f28
Showing with 145 additions and 1 deletion
+145 -1
target/
.idea
\ No newline at end of file
.idea
data
\ No newline at end of file
......@@ -351,6 +351,12 @@ dependencies = [
"wasi",
]
[[package]]
name = "glob"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
[[package]]
name = "grpcio"
version = "0.4.7"
......@@ -873,6 +879,7 @@ dependencies = [
"config",
"dgraph",
"env_logger",
"glob",
"lazy_static",
"log 0.4.8",
"serde 1.0.106",
......
......@@ -19,3 +19,5 @@ serde_json = "1.0.51"
tokio = { version = "0.2.18", features = ["macros"] }
warp = { version = "0.2.1", features = ["tls"] }
config = { version = "0.10.1" }
glob = "0.3.0"
\ No newline at end of file
pub mod note_importer;
\ No newline at end of file
extern crate glob;
use glob::glob;
use dgraph::Dgraph;
use std::str;
use std::collections::HashMap;
use serde::{Serialize, Deserialize};
use std::fs;
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "dgraph.type")]
struct Note {
notebooks: Vec<String>,
creator: String,
id: String,
title: String,
content: String,
created: Option<u64>,
modified: Option<u64>,
deleted: Option<u64>,
tags: Vec<String>,
resources: Vec<String>
}
pub fn import_notes(dgraph: &Dgraph) {
for file in glob("data/notes/*.json").expect("Failed to read glob pattern") {
let content = fs::read_to_string(file.unwrap()).unwrap();
let deserialized: Note = serde_json::from_str(&content).unwrap();
//println!("The resulting note : {:?}", deserialized);
let mut txn = dgraph.new_txn();
let mut mutation = dgraph::Mutation::new();
mutation.set_set_json(serde_json::to_vec(&deserialized).unwrap());
txn.mutate(mutation).unwrap();
let result = txn.commit();
assert_eq!(result.is_ok(), true);
println!("Imported note : {}", deserialized.title);
}
for resource in glob("data/resources/*").expect("Failed to read glob pattern") {
println!("Found resource : {:?}", resource.unwrap().display());
}
}
pub fn query_notes(dgraph: &Dgraph) {
let q = r#"{
all(func: type(Note)) {
notebooks
title
}
}"#.to_string();
let resp = dgraph.new_readonly_txn().query(q).expect("query");
println!("{:?}", &resp);
}
#[derive(Serialize, Deserialize, Default, Debug)]
struct Root {
all: Vec<Person>,
}
#[derive(Serialize, Deserialize, Default, Debug)]
#[serde(tag = "dgraph.type")]
struct Person {
name: String,
phone: Option<String>,
}
pub fn simple_example(dgraph: &Dgraph) {
let mut txn = dgraph.new_txn();
let mut mutation = dgraph::Mutation::new();
// Create Klaas from struct > JSON
let person_klaas = Person {name: "klaas".to_string(), phone: Some("456".to_string())};
let json_klaas = serde_json::to_vec(&person_klaas).unwrap();
println!("json_klaas is : {}", serde_json::to_string(&person_klaas).unwrap());
mutation.set_set_json(json_klaas);
txn.mutate(mutation).unwrap();
// Create Kees directly from JSON
let mut mutation = dgraph::Mutation::new();
let json_kees = r#"{"name": "kees", "phone": "400", "dgraph.type":"Person"}"#;
println!("json_kees is : {}", &json_kees);
mutation.set_set_json(json_kees.as_bytes().to_vec());
txn.mutate(mutation).unwrap();
// Not completely sure, can we include two mutations in one transaction?
// Or should we check result in between?
let result = txn.commit();
assert_eq!(result.is_ok(), true);
// Request all objects of type Person from DGraph
let q = r#"{
all(func: type(Person)) {
name
phone
}
}"#.to_string();
let resp = dgraph.new_readonly_txn().query(q).expect("query");
println!("Raw JSON-response from DGraph is : {:?}", &resp);
let root: Root = serde_json::from_slice(&resp.json).expect("parsing");
println!("When we turn the JSON-response into structs : {:#?}", root.all);
// Request all things called "klaas" from DGraph, this does not work yet as
// "Predicate name is not indexed"
// let q = r#"query all($a: string) {
// all(func: eq(name, $a)) {
// name
// phone
// }
// }"#.to_string();
// let mut vars = HashMap::new();
// vars.insert("$a".to_string(), "klaas".to_string());
//
// let resp = dgraph.new_readonly_txn().query_with_vars(q, vars).expect("query");
// let root: Root = serde_json::from_slice(&resp.json).expect("parsing");
// println!("When we turn the JSON-response into structs : {:#?}", root.all);
}
\ No newline at end of file
......@@ -4,6 +4,8 @@ mod internal_api;
mod sync_state;
mod warp_api;
mod importers;
use chrono::Utc;
use env_logger::Env;
use std::io::Write;
......@@ -35,11 +37,17 @@ async fn main() {
if settings.get_str("drop").unwrap().eq("true") {
dgraph_database::drop_schema_and_all_data_irreversibly(&dgraph);
}
// Set schema only in SET mode.
// Add "SCHEMA_SET=true" before executable, default is FALSE.
if settings.get_str("set").unwrap().eq("true") {
dgraph_database::set_schema(&dgraph);
}
importers::note_importer::import_notes(&dgraph);
importers::note_importer::query_notes(&dgraph);
//importers::note_importer::simple_example(&dgraph);
// Start web framework warp.
warp_api::run_server(env!("CARGO_PKG_NAME").to_uppercase(), dgraph).await;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment