function create_dbo_establishment
Creates a new dbo_Establishment node in a Neo4j graph database with the specified properties and returns the created node.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
751 - 760
simple
Purpose
This function is designed to insert a new establishment entity into a Neo4j graph database. It dynamically constructs a Cypher CREATE query based on the provided properties dictionary, executes the query using a run_query helper function, and returns the newly created node. This is useful for applications that need to store and manage establishment data in a graph database structure, such as business directories, location-based services, or organizational hierarchies.
Source Code
def create_dbo_establishment(properties):
"""Create a new dbo_Establishment node"""
props_list = ', '.join([f"n.{prop} = ${prop}" for prop in properties.keys()])
query = f"""
CREATE (n:dbo_Establishment)
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_Establishment node. Keys should be valid property names (strings) and values can be any data type supported by Neo4j (strings, numbers, booleans, lists, etc.). Example: {'name': 'Restaurant ABC', 'address': '123 Main St', 'rating': 4.5}
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 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()
# Create a new establishment
establishment_props = {
'name': 'The Coffee Shop',
'address': '456 Oak Avenue',
'city': 'Springfield',
'rating': 4.7,
'established_year': 2015
}
new_establishment = create_dbo_establishment(establishment_props)
if new_establishment:
print(f'Created establishment: {new_establishment}')
else:
print('Failed to create establishment')
Best Practices
- Ensure the properties dictionary does not contain keys with special characters that could break the Cypher query syntax
- Validate and sanitize input properties before passing them to this function to prevent injection attacks
- The function relies on parameterized queries which helps prevent Cypher injection, but property keys are directly interpolated into the query string
- Consider adding error handling around the run_query call to catch and handle database connection or query execution errors
- Ensure the run_query function properly handles database connections and closes them to prevent connection leaks
- Consider adding validation to ensure required properties are present before attempting to create the node
- Be aware that this function will create duplicate nodes if called multiple times with the same properties; consider adding uniqueness constraints or MERGE logic if duplicates should be avoided
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_dbo_establishmentcycles 88.9% similar
-
function create_references_establishment_relationship_v4 77.7% similar
-
function create_references_establishment_relationship_v2 76.2% similar
-
function get_dbo_establishment_by_id 75.7% similar
-
function create_references_establishment_relationship_v3 74.4% similar