File size: 1,391 Bytes
ca7a2c2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
"""Neo4j client for graph database operations."""
from neo4j import AsyncGraphDatabase
from app.core.config import settings
class Neo4jClient:
"""Async Neo4j client for spatial and graph queries."""
def __init__(self, uri: str, user: str, password: str):
"""Initialize Neo4j driver."""
self._driver = AsyncGraphDatabase.driver(uri, auth=(user, password))
async def close(self) -> None:
"""Close the driver connection."""
await self._driver.close()
async def run_cypher(
self,
query: str,
params: dict | None = None,
) -> list[dict]:
"""
Execute a Cypher query and return results.
Args:
query: Cypher query string
params: Optional query parameters
Returns:
List of result records as dictionaries
"""
async with self._driver.session() as session:
result = await session.run(query, params or {})
return await result.data()
async def verify_connectivity(self) -> bool:
"""Verify connection to Neo4j."""
try:
await self._driver.verify_connectivity()
return True
except Exception:
return False
# Global Neo4j client instance
neo4j_client = Neo4jClient(
settings.neo4j_uri,
settings.neo4j_username,
settings.neo4j_password,
)
|