function get_dbo_usergroup_by_id
Retrieves a single dbo_UserGroup node from a Neo4j graph database by its unique ID.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
1162 - 1169
simple
Purpose
This function queries a Neo4j database to fetch a specific user group node identified by its ID. It executes a Cypher query to match nodes with the label 'dbo_UserGroup' and returns the first matching result or None if no match is found. This is useful for retrieving user group information in applications that use Neo4j for storing organizational or access control data.
Source Code
def get_dbo_usergroup_by_id(id):
"""Get a dbo_UserGroup node by its ID"""
query = """
MATCH (n:dbo_UserGroup {id: $id})
RETURN n
"""
result = run_query(query, {"id": id})
return result[0] if result else None
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
id |
- | - | positional_or_keyword |
Parameter Details
id: The unique identifier of the dbo_UserGroup node to retrieve. Expected to be a string or integer value that matches the 'id' property of a node in the Neo4j database. This parameter is used in the Cypher query to filter and locate the specific user group.
Return Value
Returns the first dbo_UserGroup node that matches the provided ID, or None if no matching node is found. The return value is typically a dictionary-like object representing the node's properties (depending on the implementation of run_query). If the query returns multiple results, only the first one is returned. If the result set is empty, None is returned.
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()
# Use the function
user_group = get_dbo_usergroup_by_id('group_123')
if user_group:
print(f'Found user group: {user_group}')
else:
print('User group not found')
Best Practices
- Ensure the run_query function is properly defined and handles Neo4j connection management, including proper session cleanup
- Consider adding error handling for database connection failures or query execution errors
- Validate the 'id' parameter before passing it to the query to prevent injection attacks or invalid queries
- Consider adding type hints to the function signature for better code documentation (e.g., def get_dbo_usergroup_by_id(id: str) -> Optional[Dict])
- If multiple nodes with the same ID exist (which shouldn't happen with proper constraints), consider using LIMIT 1 in the Cypher query or handling multiple results explicitly
- Add database constraints or indexes on the 'id' property of dbo_UserGroup nodes for better query performance
- Consider implementing caching for frequently accessed user groups to reduce database load
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_dbo_usergroup_by_uid 90.5% similar
-
function get_all_dbo_usergroup 84.0% similar
-
function create_dbo_usergroup 73.8% similar
-
function get_dbo_product_by_id 73.6% similar
-
function get_user_by_id 71.4% similar