function get_dbo_treatments_by_uid
Retrieves a single dbo_Treatments node from a Neo4j graph database by matching its unique identifier (UID).
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
1132 - 1139
simple
Purpose
This function queries a Neo4j database to fetch a specific treatment record identified by its UID. 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 node or None if no match is found, making it useful for lookup operations in medical or healthcare data systems.
Source Code
def get_dbo_treatments_by_uid(uid):
"""Get a dbo_Treatments node by its UID"""
query = """
MATCH (n:dbo_Treatments {UID: $uid})
RETURN n
"""
result = run_query(query, {"uid": uid})
return result[0] if result else None
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
uid |
- | - | positional_or_keyword |
Parameter Details
uid: The unique identifier (UID) of the dbo_Treatments node to retrieve. Expected to be a string or integer value that matches the UID property of a node in the Neo4j database. This parameter is used in a Cypher query to perform an exact match lookup.
Return Value
Returns a dictionary-like object representing the dbo_Treatments node if found, containing all properties of the matched node. Returns None if no node with the specified UID exists. The return type is typically a Neo4j Node object or dictionary representation of the node's properties, depending on the implementation of the run_query function.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
from neo4j import GraphDatabase
# Assuming run_query is defined elsewhere
def run_query(query, params):
driver = GraphDatabase.driver('bolt://localhost:7687', auth=('neo4j', 'password'))
with driver.session() as session:
result = session.run(query, params)
return [record['n'] for record in result]
driver.close()
# Use the function
treatment_uid = '12345'
treatment_node = get_dbo_treatments_by_uid(treatment_uid)
if treatment_node:
print(f'Found treatment: {treatment_node}')
else:
print('Treatment not found')
Best Practices
- Ensure the run_query function is properly implemented with error handling and connection management
- Validate the uid parameter before passing it to prevent injection attacks or invalid queries
- Consider adding error handling for database connection failures
- The function assumes run_query returns a list; ensure this contract is maintained
- Consider adding type hints for better code documentation (e.g., def get_dbo_treatments_by_uid(uid: str) -> Optional[Dict])
- Index the UID property in Neo4j for better query performance
- Consider adding logging for debugging and monitoring purposes
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_dbo_treatments_by_id 88.1% similar
-
function get_all_dbo_treatments 76.3% similar
-
function get_dbo_tnv_by_uid 75.5% similar
-
function get_dbo_houses_by_uid 73.3% similar
-
function get_dbo_usergroup_by_uid 72.8% similar