Skip to main content

NovaGraphDb

Struct NovaGraphDb 

Source
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

Source

pub fn new(store: Arc<dyn GraphStore>) -> Self

Construct a new wrapper around the provided store adapter.

Source

pub fn in_memory() -> Self

Create an in-memory instance useful for testing and examples.

Examples found in repository?
examples/simple.rs (line 7)
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}
Source

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).

Source

pub fn surreal( endpoint: impl Into<String>, namespace: impl Into<String>, database: impl Into<String>, ) -> Self

Create a SurrealDB-backed wrapper.

Source

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.

Source

pub async fn execute( &self, query: GraphQuery, ) -> Result<JsonValue, GraphDbError>

Execute a GraphQuery against the backend.

Source

pub async fn upsert_node(&self, node: GraphNode) -> Result<(), GraphDbError>

Upsert a node.

Examples found in repository?
examples/simple.rs (line 17)
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}
Source

pub async fn upsert_edge(&self, edge: GraphEdge) -> Result<(), GraphDbError>

Upsert an edge.

Examples found in repository?
examples/simple.rs (line 37)
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}
Source

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?
examples/simple.rs (line 39)
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

Source§

fn clone(&self) -> NovaGraphDb

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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.

Source§

fn name(&self) -> &'static str

Human-readable plugin name used in logs.
Source§

fn on_init<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Initialization hook called during application startup.
Source§

fn extend_router(&self, router: Router<()>) -> Router<()>

Extend the provided Router<()> with plugin routes or layers.
Source§

fn on_shutdown<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Shutdown hook called during application shutdown. Default is noop.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,