function create_dbo_concepthouses
Creates a new node with label 'dbo_ConceptHouses' in a Neo4j graph database with the specified properties and returns the created node.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
673 - 682
simple
Purpose
This function provides a programmatic way to insert new dbo_ConceptHouses nodes 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 populating a knowledge graph with concept house entities, likely part of a DBpedia ontology-based data model.
Source Code
def create_dbo_concepthouses(properties):
"""Create a new dbo_ConceptHouses node"""
props_list = ', '.join([f"n.{prop} = ${prop}" for prop in properties.keys()])
query = f"""
CREATE (n:dbo_ConceptHouses)
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_ConceptHouses 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': 'Victorian House', 'year_built': 1890, 'location': 'London'}
Return Value
Returns the created Neo4j node object (first element from the result list) if the creation was successful, or None if the query returned no results or failed. The node object typically contains all properties set during creation 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 dbo_ConceptHouses node
properties = {
'name': 'Modern Apartment',
'year_built': 2020,
'location': 'New York',
'price': 500000
}
node = create_dbo_concepthouses(properties)
if node:
print(f'Created node with properties: {dict(node)}')
else:
print('Failed to create node')
Best Practices
- Ensure the properties dictionary does not contain None values or invalid data types that Neo4j cannot handle
- Validate property keys to avoid Cypher injection vulnerabilities, especially if properties come from user input
- Consider adding error handling around the run_query call to catch database connection issues or query execution failures
- Use parameterized queries (as done here with $prop syntax) to prevent injection attacks
- Ensure the run_query function properly manages database connections and sessions to avoid resource leaks
- Consider adding constraints or indexes on frequently queried properties in the Neo4j database schema
- The function assumes run_query returns a list; verify this assumption matches your implementation
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_dbo_houses 90.9% similar
-
function create_dbo_concepts 87.0% similar
-
function create_references_houses_relationship_v1 80.0% similar
-
function create_references_concepts_relationship 78.4% similar
-
function get_all_dbo_concepthouses 76.7% similar