function create_dbo_product
Creates a new dbo_Product node in a Neo4j graph database with the specified properties and returns the created node.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
1024 - 1033
simple
Purpose
This function is designed to insert a new product node 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 programmatically add product entities to a graph database, such as e-commerce systems, inventory management, or product catalog applications.
Source Code
def create_dbo_product(properties):
"""Create a new dbo_Product node"""
props_list = ', '.join([f"n.{prop} = ${prop}" for prop in properties.keys()])
query = f"""
CREATE (n:dbo_Product)
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_Product 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': 'Laptop', 'price': 999.99, 'stock': 50}
Return Value
Returns the first element from the query result (the newly created dbo_Product node) if the creation was successful, or None if the result is empty or the creation failed. The returned node is typically a Neo4j node object containing all the properties that were set during creation.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
from neo4j import GraphDatabase
# Assume 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 product
product_properties = {
'product_id': 'P12345',
'name': 'Wireless Mouse',
'price': 29.99,
'category': 'Electronics',
'in_stock': True
}
new_product = create_dbo_product(product_properties)
if new_product:
print(f'Product created successfully: {new_product}')
else:
print('Failed to create product')
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 ensure the run_query function properly implements this
- Consider adding error handling to catch and handle database connection issues or query execution failures
- Ensure unique constraints or indexes are set up in Neo4j if product_id or other properties should be unique
- The function assumes run_query returns a list; verify this assumption matches your implementation
- Consider adding validation to ensure properties dictionary is not empty before attempting to create a node
- Be aware that this function does not check for duplicate products; implement uniqueness constraints at the database level if needed
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_dbo_tnv 79.1% similar
-
function create_dbo_houses 78.2% similar
-
function create_dbo_usergroup 78.1% similar
-
function create_dbo_concepts 76.0% similar
-
function create_dbo_concepthouses 75.0% similar