use toolkit_db::secure::ScopeError;
use toolkit_db::secure::pgq::{Endpoint, GraphDeclaration, PropertyGraph, VertexOf};
use super::entity::{edge, node};
pub struct KnowledgeGraph;
pub const NODE_LABEL: &str = "node";
pub const EDGE_LABEL: &str = "edge";
impl PropertyGraph for KnowledgeGraph {
const GRAPH_NAME: &'static str = "kb";
fn declaration() -> Result<GraphDeclaration, ScopeError> {
GraphDeclaration::new::<Self>()
.vertex::<Self, node::Entity>(&["tenant_id", "id"])?
.edge::<Self, edge::Entity>(
&["tenant_id", "id"],
Endpoint {
key: vec!["tenant_id".into(), "src_node_id".into()],
table: "node".into(),
references: vec!["tenant_id".into(), "id".into()],
},
Endpoint {
key: vec!["tenant_id".into(), "dst_node_id".into()],
table: "node".into(),
references: vec!["tenant_id".into(), "id".into()],
},
)
}
}
impl VertexOf<KnowledgeGraph> for node::Entity {
const LABEL: &'static str = NODE_LABEL;
}
impl toolkit_db::secure::pgq::EdgeOf<KnowledgeGraph> for edge::Entity {
const LABEL: &'static str = EDGE_LABEL;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_declaration_builds() {
let declaration = match KnowledgeGraph::declaration() {
Ok(d) => d,
Err(e) => panic!("declaration must build: {e}"),
};
let ddl = match declaration.create_statement() {
Ok(s) => s,
Err(e) => panic!("DDL must render: {e}"),
};
assert!(ddl.contains("CREATE PROPERTY GRAPH"), "{ddl}");
assert!(ddl.contains("\"kb\""), "{ddl}");
assert!(ddl.contains("\"tenant_id\""), "{ddl}");
}
}