pub struct NovaGraphDb { /* private fields */ }Expand description
High-level graph database wrapper used by application handlers.
NovaGraphDb composes a backend GraphStore implementation and exposes a
small, ergonomic API for executing queries, upserting nodes/edges, and
traversing subgraphs. The struct is Clone and intended to be injected into
request extensions by the plugin.
Implementations§
Source§impl NovaGraphDb
impl NovaGraphDb
Sourcepub fn new(store: Arc<dyn GraphStore>) -> Self
pub fn new(store: Arc<dyn GraphStore>) -> Self
Construct a new wrapper around the provided store adapter.
Sourcepub fn in_memory() -> Self
pub fn in_memory() -> Self
Create an in-memory instance useful for testing and examples.
Examples found in repository?
5async fn main() {
6 // Construct an in-memory graph store and upsert a couple of nodes/edges.
7 let graph = NovaGraphDb::in_memory();
8
9 let mut props = HashMap::new();
10 props.insert("name".to_string(), serde_json::json!("Alice"));
11 let node = GraphNode {
12 id: "n1".to_string(),
13 labels: vec!["Person".to_string()],
14 properties: props,
15 };
16
17 graph.upsert_node(node).await.unwrap();
18
19 let mut props2 = HashMap::new();
20 props2.insert("name".to_string(), serde_json::json!("Bob"));
21 let node2 = GraphNode {
22 id: "n2".to_string(),
23 labels: vec!["Person".to_string()],
24 properties: props2,
25 };
26
27 graph.upsert_node(node2).await.unwrap();
28
29 let edge = GraphEdge {
30 id: "e1".to_string(),
31 from: "n1".to_string(),
32 to: "n2".to_string(),
33 rel_type: "FRIEND".to_string(),
34 properties: HashMap::new(),
35 };
36
37 graph.upsert_edge(edge).await.unwrap();
38
39 let sub = graph.traverse_json("n1", 2).await.unwrap();
40 println!("subgraph: {}", sub);
41}Sourcepub fn neo4j(
uri: impl Into<String>,
user: impl Into<String>,
password: impl Into<String>,
) -> Self
pub fn neo4j( uri: impl Into<String>, user: impl Into<String>, password: impl Into<String>, ) -> Self
Create a Neo4j-backed wrapper (adapter lives in neo4j.rs).
Sourcepub fn surreal(
endpoint: impl Into<String>,
namespace: impl Into<String>,
database: impl Into<String>,
) -> Self
pub fn surreal( endpoint: impl Into<String>, namespace: impl Into<String>, database: impl Into<String>, ) -> Self
Create a SurrealDB-backed wrapper.
Sourcepub fn surreal_with_auth(
endpoint: impl Into<String>,
namespace: impl Into<String>,
database: impl Into<String>,
username: impl Into<String>,
password: impl Into<String>,
) -> Self
pub fn surreal_with_auth( endpoint: impl Into<String>, namespace: impl Into<String>, database: impl Into<String>, username: impl Into<String>, password: impl Into<String>, ) -> Self
Create a SurrealDB-backed wrapper with basic auth credentials.
Sourcepub async fn execute(
&self,
query: GraphQuery,
) -> Result<JsonValue, GraphDbError>
pub async fn execute( &self, query: GraphQuery, ) -> Result<JsonValue, GraphDbError>
Execute a GraphQuery against the backend.
Sourcepub async fn upsert_node(&self, node: GraphNode) -> Result<(), GraphDbError>
pub async fn upsert_node(&self, node: GraphNode) -> Result<(), GraphDbError>
Upsert a node.
Examples found in repository?
5async fn main() {
6 // Construct an in-memory graph store and upsert a couple of nodes/edges.
7 let graph = NovaGraphDb::in_memory();
8
9 let mut props = HashMap::new();
10 props.insert("name".to_string(), serde_json::json!("Alice"));
11 let node = GraphNode {
12 id: "n1".to_string(),
13 labels: vec!["Person".to_string()],
14 properties: props,
15 };
16
17 graph.upsert_node(node).await.unwrap();
18
19 let mut props2 = HashMap::new();
20 props2.insert("name".to_string(), serde_json::json!("Bob"));
21 let node2 = GraphNode {
22 id: "n2".to_string(),
23 labels: vec!["Person".to_string()],
24 properties: props2,
25 };
26
27 graph.upsert_node(node2).await.unwrap();
28
29 let edge = GraphEdge {
30 id: "e1".to_string(),
31 from: "n1".to_string(),
32 to: "n2".to_string(),
33 rel_type: "FRIEND".to_string(),
34 properties: HashMap::new(),
35 };
36
37 graph.upsert_edge(edge).await.unwrap();
38
39 let sub = graph.traverse_json("n1", 2).await.unwrap();
40 println!("subgraph: {}", sub);
41}Sourcepub async fn upsert_edge(&self, edge: GraphEdge) -> Result<(), GraphDbError>
pub async fn upsert_edge(&self, edge: GraphEdge) -> Result<(), GraphDbError>
Upsert an edge.
Examples found in repository?
5async fn main() {
6 // Construct an in-memory graph store and upsert a couple of nodes/edges.
7 let graph = NovaGraphDb::in_memory();
8
9 let mut props = HashMap::new();
10 props.insert("name".to_string(), serde_json::json!("Alice"));
11 let node = GraphNode {
12 id: "n1".to_string(),
13 labels: vec!["Person".to_string()],
14 properties: props,
15 };
16
17 graph.upsert_node(node).await.unwrap();
18
19 let mut props2 = HashMap::new();
20 props2.insert("name".to_string(), serde_json::json!("Bob"));
21 let node2 = GraphNode {
22 id: "n2".to_string(),
23 labels: vec!["Person".to_string()],
24 properties: props2,
25 };
26
27 graph.upsert_node(node2).await.unwrap();
28
29 let edge = GraphEdge {
30 id: "e1".to_string(),
31 from: "n1".to_string(),
32 to: "n2".to_string(),
33 rel_type: "FRIEND".to_string(),
34 properties: HashMap::new(),
35 };
36
37 graph.upsert_edge(edge).await.unwrap();
38
39 let sub = graph.traverse_json("n1", 2).await.unwrap();
40 println!("subgraph: {}", sub);
41}Sourcepub async fn traverse_json(
&self,
start: &str,
max_depth: usize,
) -> Result<JsonValue, GraphDbError>
pub async fn traverse_json( &self, start: &str, max_depth: usize, ) -> Result<JsonValue, GraphDbError>
Traverse the graph and return JSON representing the subgraph.
Examples found in repository?
5async fn main() {
6 // Construct an in-memory graph store and upsert a couple of nodes/edges.
7 let graph = NovaGraphDb::in_memory();
8
9 let mut props = HashMap::new();
10 props.insert("name".to_string(), serde_json::json!("Alice"));
11 let node = GraphNode {
12 id: "n1".to_string(),
13 labels: vec!["Person".to_string()],
14 properties: props,
15 };
16
17 graph.upsert_node(node).await.unwrap();
18
19 let mut props2 = HashMap::new();
20 props2.insert("name".to_string(), serde_json::json!("Bob"));
21 let node2 = GraphNode {
22 id: "n2".to_string(),
23 labels: vec!["Person".to_string()],
24 properties: props2,
25 };
26
27 graph.upsert_node(node2).await.unwrap();
28
29 let edge = GraphEdge {
30 id: "e1".to_string(),
31 from: "n1".to_string(),
32 to: "n2".to_string(),
33 rel_type: "FRIEND".to_string(),
34 properties: HashMap::new(),
35 };
36
37 graph.upsert_edge(edge).await.unwrap();
38
39 let sub = graph.traverse_json("n1", 2).await.unwrap();
40 println!("subgraph: {}", sub);
41}Trait Implementations§
Source§impl Clone for NovaGraphDb
impl Clone for NovaGraphDb
Source§fn clone(&self) -> NovaGraphDb
fn clone(&self) -> NovaGraphDb
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl NovaPlugin for NovaGraphDb
Plugin wiring for graph database support.
impl NovaPlugin for NovaGraphDb
Plugin wiring for graph database support.
NovaGraphDb implements NovaPlugin so it can be registered with
NovaApp. The plugin injects a cloned NovaGraphDb into request
extensions to enable the NovaGraph extractor in handlers.