Getting Started: OGM Track¶
Best for: Applications with complex data models, interactive queries, and relationship traversals.
Prerequisites¶
-
Neo4j Database: Running locally or remotely
-
Install Graphio:
Step 1: Set Up Connection¶
from graphio import NodeModel, Relationship, Base
from neo4j import GraphDatabase
# Connect to Neo4j
driver = GraphDatabase.driver('neo4j://localhost:7687', auth=('neo4j', 'password'))
Base.set_driver(driver)
# Optional: Set a specific database (Enterprise Edition only)
# Base.set_database('mydb') # All operations will use 'mydb' database
Step 2: Define Your Models¶
class Person(NodeModel):
_labels = ['Person']
_merge_keys = ['email'] # Unique identifier
name: str
email: str
age: int
# Define relationships
works_at: Relationship = Relationship('Person', 'WORKS_AT', 'Company')
class Company(NodeModel):
_labels = ['Company']
_merge_keys = ['name']
name: str
industry: str
# Bidirectional relationship - same definition, queryable from Company
employees: Relationship = Relationship('Person', 'WORKS_AT', 'Company')
Step 3: Create and Query Data¶
# Create companies
acme = Company(name='ACME Corp', industry='Technology')
acme.merge()
# Create people and relationships
alice = Person(name='Alice Smith', email='alice@example.com', age=30)
alice.works_at.add(acme, {'position': 'Developer', 'since': '2023'})
alice.merge()
# Query data both directions
developers = Person.match(Person.age > 25).all()
alice_company = alice.works_at.match().first()
# Bidirectional querying (new feature!)
acme_employees = acme.employees.match().all() # Query from Company to Person
print(f"ACME has {len(acme_employees)} employees")
Step 4: Create Indexes (Performance)¶
What You've Learned¶
✅ How to define Pydantic-based models with Graphio
✅ How to create nodes and relationships
✅ How to query data using intuitive syntax
✅ How to optimize with indexes
Next Steps¶
- Ready for bulk data? → Bulk Loading Track
- Want to combine both? → Hybrid Approach
- Deep dive into OGM → Complete OGM Guide