28 lines
1.0 KiB
Python
28 lines
1.0 KiB
Python
"""Bounded Neo4j adapter used only behind RelationshipService."""
|
|||
|
|
|
||
|
|
import asyncio
|
||
|
|
from collections.abc import Sequence
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
from neo4j import AsyncGraphDatabase
|
||
|
|
|
||
|
|
from app.core.config import Settings
|
||
|
|
|
||
|
|
|
||
|
|
class Neo4jGraphDriver:
|
||
|
|
def __init__(self, settings: Settings, *, timeout_seconds: float = 2.0) -> None:
|
||
|
|
self.settings = settings
|
||
|
|
self.timeout_seconds = timeout_seconds
|
||
|
|
self._driver: Any = None
|
||
|
|
|
||
|
|
async def execute_query(self, query: str, **parameters: Any) -> Sequence[Any]:
|
||
|
|
if self._driver is None:
|
||
|
|
self._driver = AsyncGraphDatabase.driver(
|
||
|
|
self.settings.neo4j_uri,
|
||
|
|
auth=(self.settings.neo4j_username, self.settings.neo4j_password),
|
||
|
|
)
|
||
|
|
async with asyncio.timeout(self.timeout_seconds):
|
||
|
|
async with self._driver.session(database=self.settings.neo4j_database) as session:
|
||
|
|
result = await session.run(query, parameters)
|
||
|
|
return [record.data() async for record in result]
|