function create_dbo_treatments
Creates a new node labeled 'dbo_Treatments' in a Neo4j graph database with the specified properties and returns the created node.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
1141 - 1150
simple
Purpose
This function is designed to insert a new treatment record into a Neo4j graph database. It dynamically constructs a Cypher CREATE query based on the provided properties dictionary, executes the query using a helper function 'run_query', and returns the newly created node. This is useful for applications managing medical treatments, clinical data, or healthcare information systems where treatments need to be stored as graph nodes.
Source Code
def create_dbo_treatments(properties):
"""Create a new dbo_Treatments node"""
props_list = ', '.join([f"n.{prop} = ${prop}" for prop in properties.keys()])
query = f"""
CREATE (n:dbo_Treatments)
SET {props_list}
RETURN n
"""
result = run_query(query, properties)
return result[0] if result else None
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
properties |
- | - | positional_or_keyword |
Parameter Details
properties: A dictionary containing key-value pairs representing the properties to be set on the new dbo_Treatments node. Keys should be valid property names (strings) and values can be any data type supported by Neo4j (strings, numbers, booleans, dates, etc.). Example: {'treatment_id': '12345', 'name': 'Chemotherapy', 'duration_days': 90}
Return Value
Returns the first element from the query result (the created node object) if the query execution is successful and returns results. Returns None if the query fails or returns an empty result set. The returned node object typically contains all properties set during creation along with Neo4j internal identifiers.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
from neo4j import GraphDatabase
# Assuming run_query function is defined
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()
# Create a new treatment node
treatment_properties = {
'treatment_id': 'TRT001',
'name': 'Radiation Therapy',
'duration_days': 30,
'cost': 15000.00,
'approved': True
}
new_treatment = create_dbo_treatments(treatment_properties)
if new_treatment:
print(f'Successfully created treatment node: {new_treatment}')
else:
print('Failed to create treatment node')
Best Practices
- Validate the 'properties' dictionary before passing it to ensure all required fields are present and properly formatted
- Be cautious with SQL/Cypher injection - the current implementation uses parameterized queries for values but constructs property names dynamically. Ensure property keys come from trusted sources
- Handle the None return value appropriately in calling code to detect creation failures
- Consider adding error handling and logging within the function for better debugging
- Ensure the 'run_query' function properly manages database connections and handles exceptions
- Consider adding constraints or indexes on the dbo_Treatments label in Neo4j for better performance
- Validate that property names in the dictionary are valid Neo4j property identifiers (no special characters that could break the query)
- Consider implementing transaction management for consistency when creating multiple related nodes
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_dbo_tnv 79.2% similar
-
function get_dbo_treatments_by_id 75.8% similar
-
function create_dbo_houses 75.2% similar
-
function create_dbo_product 74.8% similar
-
function get_all_dbo_treatments 73.1% similar