function create_dbo_usergroup
Creates a new dbo_UserGroup node in a Neo4j graph database with specified properties and returns the created node.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
1180 - 1189
simple
Purpose
This function is designed to insert a new user group 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 managing user group entities in graph-based applications where relationships and hierarchies need to be represented.
Source Code
def create_dbo_usergroup(properties):
"""Create a new dbo_UserGroup node"""
props_list = ', '.join([f"n.{prop} = ${prop}" for prop in properties.keys()])
query = f"""
CREATE (n:dbo_UserGroup)
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_UserGroup 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': 'Admins', 'created_at': '2024-01-01', 'active': True}
Return Value
Returns the first node from the query result if the creation was successful, typically a dictionary-like object representing the created dbo_UserGroup node with all its properties. Returns None if the query execution fails or returns no results. The exact return type depends on the implementation of the run_query function, but typically it would be a Neo4j Node object or a dictionary representation of the node.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
# Assuming run_query is defined and Neo4j connection is configured
# Define properties for the new user group
user_group_properties = {
'name': 'Administrators',
'description': 'System administrators group',
'created_at': '2024-01-15',
'active': True,
'member_count': 5
}
# Create the user group node
new_group = create_dbo_usergroup(user_group_properties)
if new_group:
print(f"Successfully created user group: {new_group}")
else:
print("Failed to create user group")
Best Practices
- Ensure the properties dictionary does not contain None values or invalid data types that Neo4j cannot handle
- Validate property names to avoid Cypher injection vulnerabilities, especially if properties come from user input
- The function depends on an external run_query function which must handle database connections, error handling, and query execution properly
- Consider adding error handling for cases where the properties dictionary is empty or contains invalid keys
- Be aware that this function uses string formatting to build Cypher queries; ensure the run_query function uses parameterized queries to prevent injection attacks
- The function assumes the run_query function returns a list-like object; verify this assumption matches your implementation
- Consider adding validation to ensure required properties are present before attempting to create the node
- Use transactions appropriately in the run_query implementation to ensure data consistency
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_dbo_product 78.1% similar
-
function create_dbo_houses 75.0% similar
-
function get_dbo_usergroup_by_id 73.8% similar
-
function get_all_dbo_usergroup 72.6% similar
-
function create_dbo_concepthouses 71.9% similar