function create_dbo_houses
Creates a new node with label 'dbo_Houses' in a Neo4j graph database with the specified properties and returns the created node.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
907 - 916
simple
Purpose
This function is designed to insert a new house 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 real estate data, housing databases, or any system that needs to store house-related information in a graph structure.
Source Code
def create_dbo_houses(properties):
"""Create a new dbo_Houses node"""
props_list = ', '.join([f"n.{prop} = ${prop}" for prop in properties.keys()])
query = f"""
CREATE (n:dbo_Houses)
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_Houses node. Keys should be valid property names (strings) and values can be any data type supported by Neo4j (strings, numbers, booleans, lists, etc.). Example: {'address': '123 Main St', 'price': 500000, 'bedrooms': 3}
Return Value
Returns the first element from the query result (the newly created node object) if the creation was successful, or None if the query returned no results or failed. The returned node object typically contains all the properties that were set, along with Neo4j metadata like node ID and labels.
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 house node
house_properties = {
'address': '123 Main Street',
'city': 'Springfield',
'price': 450000,
'bedrooms': 3,
'bathrooms': 2,
'square_feet': 2000
}
new_house = create_dbo_houses(house_properties)
if new_house:
print(f'Successfully created house node: {new_house}')
else:
print('Failed to create house node')
Best Practices
- Ensure the 'properties' dictionary does not contain None values or invalid Neo4j data types
- Validate property names to avoid Cypher injection vulnerabilities
- The function depends on an external 'run_query' function which must be properly implemented with error handling
- Consider adding input validation to check if properties dictionary is empty before creating the node
- Use parameterized queries (as this function does) to prevent injection attacks
- Handle potential exceptions from Neo4j connection failures or query execution errors in the calling code
- Consider adding transaction management for consistency when creating multiple nodes
- Ensure property keys follow Neo4j naming conventions (alphanumeric and underscores)
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_dbo_concepthouses 90.9% similar
-
function create_dbo_product 78.2% similar
-
function create_references_houses_relationship_v1 77.1% similar
-
function create_dbo_tnv 76.9% similar
-
function create_dbo_treatments 75.2% similar