Files
natureinpots_community/app/neo4j_utils.py
2025-06-04 01:40:12 -05:00

33 lines
1.0 KiB
Python

from neo4j import GraphDatabase
from flask import current_app
class Neo4jHandler:
def __init__(self, uri, user, password):
self.driver = GraphDatabase.driver(uri, auth=(user, password))
def close(self):
self.driver.close()
def create_plant_node(self, uuid, name):
with self.driver.session() as session:
session.run(
"MERGE (p:Plant {uuid: $uuid}) "
"SET p.name = $name",
uuid=uuid, name=name
)
def create_lineage(self, child_uuid, parent_uuid):
with self.driver.session() as session:
session.run(
"MATCH (child:Plant {uuid: $child_uuid}), (parent:Plant {uuid: $parent_uuid}) "
"MERGE (parent)-[:PARENT_OF]->(child)",
child_uuid=child_uuid, parent_uuid=parent_uuid
)
def get_neo4j_handler():
uri = current_app.config['NEO4J_URI']
user = current_app.config['NEO4J_USER']
password = current_app.config['NEO4J_PASSWORD']
return Neo4jHandler(uri, user, password)