Quickstart

This page shows the shortest path from an empty database to a useful query.

Create a Graph

from pygraphdb.graphdb import Edge, GraphDB, Node
from pygraphdb.kvstores import LMDBStore
from pygraphdb.serializers import PickleSerializer

graph_db = GraphDB(LMDBStore(path="quickstart_lmdb"), PickleSerializer())

graph_db.put_node(Node(
    node_id="drug-1",
    labels=["Drug"],
    properties={"name": "Aspirin"},
))
graph_db.put_node(Node(
    node_id="protein-1",
    labels=["Protein"],
    properties={"name": "PTGS1"},
))
graph_db.put_edge(Edge(
    edge_id="drug-1-protein-1",
    source="drug-1",
    target="protein-1",
    properties={"type": "binds", "score": 0.9},
))

Nodes and Edges

Fetch records directly when you know their IDs:

drug = graph_db.get_node(b"drug-1")
edge = graph_db.get_edge(b"drug-1-protein-1")

print(drug.properties["name"])
print(edge.properties["score"])

Labels and Indexes

Labels are indexed automatically. Property indexes are explicit; create them for properties you query frequently.

graph_db.create_node_property_index("name")
graph_db.create_edge_property_index("score")

drugs = graph_db.nodes_by_label("Drug")
aspirin = graph_db.nodes_by_property("name", "Aspirin")
strong_edges = graph_db.edges_by_property_range("score", 0.8, None)

Cypher Query

Use GraphDB.query for read-only Cypher queries over labels, typed relationships, filters, ordering, and chained MATCH clauses.

result = graph_db.query(
    'MATCH (d:Drug {name: "Aspirin"}) '
    'MATCH (d)-[r:binds]->(p) '
    'WHERE r.score >= 0.8 '
    'RETURN d.id AS drug, p.name AS protein, r.score AS score'
)

for record in result:
    print(record)

Typed Traversal

Relationship types are stored in edge.properties["type"] and maintained in typed adjacency indexes.

neighbors = graph_db.neighbors_by_edge_type("drug-1", "binds", direction="out")
print(neighbors)

Bulk Inserts

Use bulk writes when loading many records.

graph_db.put_nodes([
    Node(node_id="drug-2", labels=["Drug"], properties={"name": "Ibuprofen"}),
    Node(node_id="protein-2", labels=["Protein"], properties={"name": "PTGS2"}),
])
graph_db.put_edges_bulk([
    Edge(edge_id="drug-2-protein-2", source="drug-2", target="protein-2", properties={"type": "binds"}),
])

Close the Store

Close the database when a script or notebook cell is finished.

graph_db.close()

Next Steps