GraphQLite Rust
Rust bindings for GraphQLite, a SQLite extension that adds graph database capabilities using Cypher.
Installation
Add to your Cargo.toml:
[]
= "0.1.0-beta"
Quick Start
High-Level Graph API (Recommended)
The Graph struct provides an ergonomic interface for common graph operations:
use Graph;
Low-Level Cypher API
For complex queries or when you need full control:
use Connection;
API Reference
Graph
use ;
// Constructor
let g = open?;
let g = open?;
let g = open_in_memory?;
// Or use the factory function
let g = graph?;
Node Operations
| Method | Description |
|---|---|
upsert_node(id, props, label) |
Create or update a node |
get_node(id) |
Get node by ID |
has_node(id) |
Check if node exists |
delete_node(id) |
Delete node and its edges |
get_all_nodes(label) |
Get all nodes, optionally by label |
Edge Operations
| Method | Description |
|---|---|
upsert_edge(src, dst, props, type) |
Create edge between nodes |
get_edge(src, dst) |
Get edge properties |
has_edge(src, dst) |
Check if edge exists |
delete_edge(src, dst) |
Delete edge |
get_all_edges() |
Get all edges |
Graph Queries
| Method | Description |
|---|---|
node_degree(id) |
Count edges connected to node |
get_neighbors(id) |
Get adjacent nodes |
stats() |
Get node/edge counts |
query(cypher) |
Execute raw Cypher query |
Graph Algorithms
Centrality
| Method | Description |
|---|---|
pagerank(damping, iterations) |
PageRank importance scores |
degree_centrality() |
In/out/total degree for each node |
betweenness_centrality() |
Betweenness centrality scores |
closeness_centrality() |
Closeness centrality scores |
eigenvector_centrality(iterations) |
Eigenvector centrality scores |
Community Detection
| Method | Description |
|---|---|
community_detection(iterations) |
Label propagation communities |
louvain(resolution) |
Louvain modularity optimization |
Connected Components
| Method | Description |
|---|---|
wcc() |
Weakly connected components |
scc() |
Strongly connected components |
Path Finding
| Method | Description |
|---|---|
shortest_path(src, dst, weight) |
Dijkstra's shortest path |
astar(src, dst, lat, lon) |
A* with optional heuristic |
apsp() |
All-pairs shortest paths (Floyd-Warshall) |
Traversal
| Method | Description |
|---|---|
bfs(start, max_depth) |
Breadth-first search |
dfs(start, max_depth) |
Depth-first search |
Similarity
| Method | Description |
|---|---|
node_similarity(n1, n2, threshold, k) |
Jaccard similarity |
knn(node, k) |
K-nearest neighbors |
triangle_count() |
Triangle counts and clustering coefficients |
Batch Operations
// Batch insert nodes
g.upsert_nodes_batch?;
// Batch insert edges
g.upsert_edges_batch?;
Connection
use Connection;
let conn = open?;
let conn = open?;
let conn = open_in_memory?;
// Wrap existing rusqlite connection
let sqlite_conn = open_in_memory?;
let conn = from_rusqlite?;
Methods
| Method | Description |
|---|---|
cypher(query) |
Execute Cypher query, return results |
execute(sql) |
Execute raw SQL |
sqlite_connection() |
Access underlying rusqlite connection |
CypherResult
let results = conn.cypher?;
results.len; // Number of rows
results.is_empty; // Check if empty
results.columns; // Column names
results; // First row
for row in &results
Row
Type-safe value extraction:
let name: String = row.get?;
let age: i64 = row.get?;
let score: f64 = row.get?;
let active: bool = row.get?;
let maybe: = row.get?;
Utility Functions
use ;
// Escape strings for Cypher queries
let safe = escape_string; // "it\\'s"
// Sanitize relationship types
let rel = sanitize_rel_type; // "has_items"
let rel = sanitize_rel_type; // "REL_CREATE"
// Check reserved keywords
if CYPHER_RESERVED.contains
Extension Path
The extension is located automatically. To specify a custom path:
let conn = open_with_extension?;
Or set the GRAPHQLITE_EXTENSION_PATH environment variable.
License
MIT