Skip to content

Instantly share code, notes, and snippets.

@aanastasiou
Created August 11, 2023 14:18
Show Gist options
  • Save aanastasiou/b89aa67869daaa0c9e135d0128ae086c to your computer and use it in GitHub Desktop.
Save aanastasiou/b89aa67869daaa0c9e135d0128ae086c to your computer and use it in GitHub Desktop.
Minor demo for neomodel's #716
from neomodel import (config, StructuredNode, StringProperty, IntegerProperty,
UniqueIdProperty, RelationshipTo, db)
import os
class Country(StructuredNode):
code = StringProperty(unique_index=True, required=True)
class City(StructuredNode):
name = StringProperty(required=True)
country = RelationshipTo(Country, 'FROM_COUNTRY')
class Person(StructuredNode):
uid = UniqueIdProperty()
name = StringProperty(unique_index=True)
age = IntegerProperty(index=True, default=0)
# traverse outgoing IS_FROM relations, inflate to Country objects
country = RelationshipTo(Country, 'IS_FROM')
# traverse outgoing LIVES_IN relations, inflate to City objects
city = RelationshipTo(City, 'LIVES_IN')
def build_up_some_data():
c1=Country(code="GR").save()
c2=Country(code="FR").save()
ct1 = City(name="Athens", country = c1).save()
ct2 = City(name="Paris", country = c2).save()
p1 = Person(name="Bill", age=22).save()
p1.country.connect(c1)
p1.city.connect(ct1)
p2 = Person(name="Jean", age=28).save()
p2.country.connect(c2)
p2.city.connect(ct2)
p3 = Person(name="Bo", age=32).save()
p3.country.connect(c1)
p3.city.connect(ct2)
p4 = Person(name="Drop", age=16).save()
p4.country.connect(c1)
p4.city.connect(ct2)
if __name__ == "__main__":
db.set_connection(os.environ["NEO4J_BOLT_URL"])
# build_up_some_data()
# q = db.cypher_query("match p = (a:City)<-[r:LIVES_IN]-(b:Person) return p", resolve_objects=True)
q = db.cypher_query("match p = (a:City)<-[r:LIVES_IN]-(b:Person)-[:IS_FROM]->(c:Country) return p", resolve_objects=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment