function get_dbo_treatments_by_id
Retrieves a single dbo_Treatments node from a Neo4j graph database by its unique ID.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
1123 - 1130
simple
Purpose
This function queries a Neo4j database to fetch a specific treatment record identified by its ID. It's designed for retrieving individual treatment nodes from a graph database schema where treatments are stored as nodes with the label 'dbo_Treatments'. The function returns the first matching result or None if no treatment with the given ID exists.
Source Code
def get_dbo_treatments_by_id(id):
"""Get a dbo_Treatments node by its ID"""
query = """
MATCH (n:dbo_Treatments {id: $id})
RETURN n
"""
result = run_query(query, {"id": id})
return result[0] if result else None
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
id |
- | - | positional_or_keyword |
Parameter Details
id: The unique identifier of the dbo_Treatments node to retrieve. Expected to be a string or integer that matches the 'id' property of a treatment node in the Neo4j database. This parameter is used in a parameterized Cypher query to prevent injection attacks.
Return Value
Returns a single dbo_Treatments node object (typically a dictionary or Neo4j node object) if a matching treatment is found, or None if no treatment exists with the specified ID. The returned object contains all properties of the matched node. The function accesses the first element of the result list (result[0]) assuming unique IDs.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
# Assuming run_query is already defined and configured
# Example setup:
# from neo4j import GraphDatabase
# driver = GraphDatabase.driver('bolt://localhost:7687', auth=('neo4j', 'password'))
# def run_query(query, params):
# with driver.session() as session:
# result = session.run(query, params)
# return [record['n'] for record in result]
# Retrieve a treatment by ID
treatment_id = '12345'
treatment = get_dbo_treatments_by_id(treatment_id)
if treatment:
print(f'Found treatment: {treatment}')
else:
print(f'No treatment found with ID: {treatment_id}')
Best Practices
- Ensure the run_query function is properly implemented with error handling and connection management
- Validate the ID parameter before calling this function to ensure it's in the expected format
- Handle the None return value appropriately in calling code to avoid NoneType errors
- Consider adding type hints to the function signature for better code documentation
- The function assumes IDs are unique; ensure database constraints enforce this
- Close Neo4j driver connections properly when the application terminates
- Consider adding logging for debugging database queries
- For production use, implement proper exception handling for database connection failures
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_dbo_treatments_by_uid 88.1% similar
-
function get_all_dbo_treatments 83.8% similar
-
function get_dbo_treatments_with_references_tnv_dbo_tnv 77.3% similar
-
function get_dbo_tnv_by_id 77.1% similar
-
function get_dbo_treatments_with_references_product_dbo_product 75.9% similar