def add_person(driver, name): with _driver.session() as session: # Caller for transactional unit of work return session.write_transaction(create_person_node, name)
# Create a person node. @classmethod def create_person(cls, tx, name): tx.run("CREATE (:Person {name: $name})", name=name)
# Create an employment relationship to a pre-existing company node. # This relies on the person first having been created. @classmethod def employ(cls, tx, person_name, company_name): tx.run("MATCH (person:Person {name: $person_name}) " "MATCH (company:Company {name: $company_name}) " "CREATE (person)-[:WORKS_FOR]->(company)", person_name=person_name, company_name=company_name)
# Create a friendship between two people. @classmethod def create_friendship(cls, tx, name_a, name_b): tx.run("MATCH (a:Person {name: $name_a}) " "MATCH (b:Person {name: $name_b}) " "MERGE (a)-[:KNOWS]->(b)", name_a=name_a, name_b=name_b)
# Match and display all friendships. @classmethod def print_friendships(cls, tx): result = tx.run("MATCH (a)-[:KNOWS]->(b) RETURN a.name, b.name") for record in result: print("{} knows {}".format(record["a.name"] ,record["b.name"]))
def main(self): saved_bookmarks = [] # To collect the session bookmarks
# Create the first person and employment relationship. with self._driver.session() as session_a: session_a.write_transaction(self.create_person, "Alice") session_a.write_transaction(self.employ, "Alice", "Wayne Enterprises") saved_bookmarks.append(session_a.last_bookmark())
# Create the second person and employment relationship. with self._driver.session() as session_b: session_b.write_transaction(self.create_person, "Bob") session_b.write_transaction(self.employ, "Bob", "LexCorp") saved_bookmarks.append(session_b.last_bookmark())
# Create a friendship between the two people created above. with self._driver.session(bookmarks=saved_bookmarks) as session_c: session_c.write_transaction(self.create_friendship, "Alice", "Bob") session_c.read_transaction(self.print_friendships)